A young lion sits with Nairobi’s skyline in the background at Nairobi National Park in 2015. (TONY KARUMBA/AFP/Getty Images)

The last time the lions charged through Simon Saigilu’s village, he was ready. He jumped out of bed with a flashlight and a spear, emitting a high-pitched scream that was the closest thing anyone here had to an alarm.

He’d had time to practice. Every month or two, the lions appeared, after sneaking through the fence that runs between the village and Nairobi National Park, at the edge of this city of 3 million. The barrier was supposed to be electrified, but it wasn’t. The animals — including the park’s 35 lions — were supposed to remain in the park, but they didn’t.

The collision between humans and wildlife is nothing new in much of Africa, where millions of people have flocked to cities in recent decades and skylines rise in places that were once savanna or forest. But Nairobi has come to represent the extreme difficulty inherent in trying to preserve wildlife while an urban population booms. The number of residents on the outskirts of the reserve has grown more than tenfold since it was established in 1946 as the first national park in East Africa.

Saigilu’s hamlet, called Maasai Village after the tribe that lives there, provides a glimpse into the clash between the human and animal ecosystems. On one side of the smattering of huts and sheet-metal homes is the city’s industrial area, a neighborhood of warehouses and factories surrounded by near-endless traffic. On the other side is a 45-square-mile stretch of undisturbed greenery, the world’s only major game reserve within a capital city.

“We are caught in between,” said Jackson Prisitu, whose 96 sheep were slaughtered by a lion one night in March.

Both wildlife advocates and the Kenyan government see the park as a national treasure, a symbol of coexistence between humans and the big-game animals that have lived here for centuries. The park is also a massive tourist attraction, bringing in more than 100,000 visitors annually.

Urban development “makes this city like any other. But the park sets Nairobi apart. It plays a key role in defining the identity of the city,” said Paula Kahumbu, the director of WildlifeDirect, an international conservation group based in the Kenyan capital.

For years, the villagers’ proximity to the park’s wildlife was rarely a problem. Then a few things changed. At the southern edge of the park, which is unfenced for 12 miles and once bordered a vast savanna, developers started building houses and shopping centers. Suddenly, when animals moved south, they encountered people, often armed. Their domain had effectively been reduced.

That was a particular blow to the park’s lions. Male lions are very territorial, and each requires a swath of land that can be as large as 100 square miles. With less land, the lions began to look elsewhere. One of the places they looked was Maasai Village, where residents had been raising livestock for more than a half-century.

Meanwhile, alongside the village, a Chinese firm had begun to build a 300-mile railway line, one of the biggest infrastructure projects in Kenya’s history, that would stretch from Nairobi to the Indian Ocean. When that construction started more than a year ago, the electricity along stretches of the park’s fence suddenly disappeared, making it much easier for lions to escape.

And they did, over and over.

Jackson Prisitu walks past sheep in his village at the edge of Nairobi National Park. In March, 96 of his sheep were killed by a lion. (Kevin Sieff/The Washington Post)

“When they [lions] figure out how easy it is to kill livestock, they’re going to keep doing it,” said Kahumbu, of WildlifeDirect.

Kenyan wildlife officials said the power goes out only for short periods due to the railway construction and vandalism. But during repeated visits by a reporter to the village, the fence always lacked electricity.

Nairobi residents were unnerved by a spate of attacks that galvanized local media attention in the spring. One lion jumped the park fence and killed Prisitu’s 96 sheep in Maasai Village before running back into the reserve. Another lion, named Mohawk for his distinctive mane, escaped into nearby Kajiado town and was shot dead by Kenyan wildlife officials.

“The ease with which the lions are leaving the park is alarming,” said an editorial in Kenyan newspaper the Standard.

The media attention soon waned, but residents of Maasai Village watched as the lions kept coming. Kenyan officials distributed “lion lights” — high-beam lights that could be attached to poles and huts to scare off the animals. But they didn’t seem to work.

Lions killed one of Elena Nkuluto’s cows, she said, and another family’s three goats. Other times, residents say, lions got through the fence but didn’t attack any animals. No one in the village was hurt, but about 15 miles away, a man was killed by a lion in early August.

“In one of the fastest-growing cities in the world, it’s inevitable that humans and wildlife are going to increasingly come into contact,” said Kahumbu, the conservationist.

In 2013, the Kenyan government passed a law guaranteeing compensation for those whose relatives or livestock were killed by wild animals. Sometimes that compensation arrived, but often in Maasai Village, it did not.

“A lot of us are still waiting for it,” said Nkuluto.

Some officials dispute the number of lion attacks in the village.

“It’s a rare occurrence,” said Alfred Mutua, the governor of Machakos County, where Maasai Village is located. But he said he wasn’t sure how often the animals had stolen into the village. Other officials suggested villagers might exaggerate their claims to gain compensation.

Now, Kenyan officials are debating what to do to protect the humans and animals living on both sides of the fence.

Mutua is considering a program that would relocate residents of the villages along the park’s periphery.

“We’re seeing if we can move them to other land,” he said.

The residents of Maasai Village are outraged at that prospect.

“We’ve been here for so many years,” said Saigilu. “Why should we move?”

Other Kenyan officials are looking at possibly relocating at least some of the lions.

“There are efforts underway to decide whether lions should be moved to other parts of the country,” said Paul Uduto, a spokesman for Kenya Wildlife Service. “This frequency of lion attacks can’t be sustained.”

The Maasai people, who are concentrated in Kenya and Tanzania, have traditionally raised livestock and had a tense relationship with lions. As part of their warrior culture, Maasai boys have long been expected to track and kill a lion with a spear to prove their manhood.

That tradition has evolved as the lion population has diminished. Some Maasai now hunt in groups, killing fewer lions. Others have given up lion-hunting altogether, with some Maasai promoting running as a new way to establish their manhood.

But in Maasai Village, young men appear intent on killing lions to defend their livelihoods.

Each of them keeps a spear ready.

“When it becomes necessary, we will use them,” Saigilu said.

Rael Ombuor in Nairobi contributed to this article.