Kenyan voters waited in line for hours Tuesday to decide a fiercely contested election for president, as concerns about possible ethnic violence put much of the country on edge.

The race — pitting incumbent President Uhuru Kenyatta, 55, against former prime minister Raila Odinga, 72 — spurred a large turnout across one of Africa’s most vibrant democracies. But Kenya has been torn by tribal clashes after previous elections, and Odinga had told his supporters that he believed only fraud could stand in the way of victory.

The lines began forming before sunrise in Nairobi, snaking past the corrugated market stalls of its sprawling slums and the upscale apartment buildings in its ritzy suburbs. After months of nonstop campaigning, the country’s roads were lined with the billboards of political parties.

[Who are the men seeking to lead Kenya?]

Kenya’s next president will take the helm of an economy that has outpaced the vast majority of countries on the continent. Kenya serves as a base for diplomats, business executives and aid workers, who have come to see the country as an island of stability in a fragile region, bordering South Sudan and Somalia.

1 of 69 Full Screen Autoplay Close Skip Ad × Candidates campaign before Kenyan elections View Photos Kenyan presidential elections are scheuled for Tuesday. Caption Kenya's election commission announced on Aug. 11 that President Uhuru Kenyatta has won a second term as opposition candidate Raila Odinga claimed the vote was rigged. Protests and clashes with police followed voting. Aug. 12, 2017 A supporter of Kenyan opposition leader Raila Odinga stands in front of a burning barricade while engaging in battle with riot police officers during a protest in the Kibera slum of Nairobi. Since the announcement of Uhuru Kenyatta as Kenyan president on Aug. 11, supporters of Odinga, who reject the official result, started rioting in various areas in and outside of Nairobi. A number of people have been killed in violent protests, the media reports say. Dai Kurokawa/European Pressphoto Agency Buy Photo Wait 1 second to continue.

But it is nonetheless a country fraught with problems — corruption in the public sector, terrorism emanating from Somalia and tribalism that has led to sometimes dangerous political clashes. In 2007, post-election violence left about 1,400 people dead.

Although both Kenyatta and Odinga have produced lists of pledges and policy statements, “the race is almost exclusively about ethnic groups trying to hold on to power or take power,” said Murithi Mutiga, a researcher at the International Crisis Group.

Tensions have deepened in recent months, first with a torrent of incendiary, fake news stories and then, in late July, with the apparent murder of a top election official and rumors about vote rigging. Early in the campaign cycle, many assumed Kenyatta would win by a wide margin, but Odinga has inched closer, and recent polls show the candidates neck and neck.

The tight race, coupled with concerns about vote rigging, made the prospect of violence a worry for many of those waiting in line at polling sites. Already, large numbers of people had fled Nairobi by bus to avoid possible fighting.

“Of course it’s on my mind after what happened in 2007,” said Edith Oketch, 35, who voted with her 10-month-old daughter clinging to her shoulder.

[Inside the world’s largest refu­gee camp]

Oketch had avoided political rallies, fearing they might get out of hand, but on Tuesday, there was no question she would vote. She lined up at a polling center in a primary school near her home in Kibera, Kenya’s largest slum.

An electoral counting officer, center, counts votes while others hold the piles of ballots counted for each candidate, at a polling station in Nairobi. (Ben Curtis/AP)

Oketch has a university degree in business administration, but like many Kenyans, she has struggled to find work, even as the economy has grown by more than 5 percent annually, the tech sector has blossomed, and multinationals have opened offices in Nairobi. She now sells cereal and other food in the slum’s streets.

“You take your kid to school, you struggle, and in the end, there is nothing in return,” Oketch said.

Her solution: She would vote out Kenyatta, hoping that Odinga would bring change. He had even chosen to cast his vote in the slum, a good sign, she thought.

But with Kenyan politics dictated mostly by tribal alliances, other polling sites in areas dominated by Kenyatta’s Kikuyu tribe were filled with voters who longed to keep the president in power.

Under Kenyatta’s rule, Aiddah Mungai’s life improved dramatically. She started her own business — a bakery called the Cake Hub — and she watched as more and more customers came. Kenyatta had committed to modernizing the country, encouraging technological innovation, and Mungai has seen the results firsthand, she said.

“I registered my business online. I pay my yearly licensing fees online,” she said. “It’s so much easier than it was.”

But Mungai recognized that even her vote was about more than just Kenyatta’s leadership. Like the president, she, too, is Kikuyu.

“It’s a tribal thing here, no matter how much we say it’s not.”

It will likely take two or three days for the results of the election to be tabulated. Odinga has said that he believes he will lose only if the vote is rigged, and it remains unclear how he or his supporters would respond to such a defeat. Odinga is running for his fourth — and likely last — time, adding pressure on his campaign.

Kenyans also voted for a range of local candidates Tuesday, as well as members of parliament. The constitution established in 2010 transferred significant power from the central government to local offices, in part with the hope of empowering groups and regions that have traditionally been marginalized.

In a statement Monday, former U.S. president Barack Obama weighed in on Kenya’s tribal tension and the need for a peaceful vote.

“In Kenya’s election, we have already seen too much incitement and appeals based on fear from all sides,” he said. “But I also know that the Kenyan people as a whole will be the losers if there is a descent into violence. You can make clear that you will reject those that want to deal in tribal and ethnic hatred.”

As polls closed Tuesday evening, the streets of Nairobi, normally packed during rush hour, were mostly empty. International election observers have been positive about the vote.

“I’d give anything to have a turnout like this back home,” said Rep. Karen Bass (D-Calif.), who was part of the monitoring team for the National Democratic Institute, a U.S. nonprofit.

Read more:

Rangers just killed one of Kenya’s most famous lions. But they didn’t have to.

“Ivory is worthless unless it’s on our elephants,” Kenya says

Today’s coverage from Post correspondents around the world

Like Washington Post World on Facebook and stay updated on foreign news