Kenyans queued in huge numbers in the dark to vote in the knife-election election pitting President Uhuru Kenyatta, 55, the businessman son of Kenya's founding president, against Raila Odinga, a 72-year-old former political prisoner and son of Kenya's first vice-president.

Kenya election
Voters queue as they wait for a polling station to open in Nairobi's Kibera, one the largest slums in AfricaMarco Longari/AFP
Kenya election
A voter uses the light from a mobile phone to check voting lists for the general elections at a polling station in Kilimani Primary School in NairobiLuis Tato/AFP

The arch rivals are facing each other for the second time, and opinion polls have put them neck-and-neck after two months of campaigning marked by fiery rhetoric but public speeches largely free of the ethnic hate that has sullied previous contests.

Voters formed long lines at many polling stations before dawn, waiting for the chance to cast ballots in the tightly-contested race for the presidency as well as for more than 1,800 elected positions, including governors, legislative representatives and county officials.

Kenya election
People queue outside a polling station to cast their vote during the presidential election in the city centre in NairobiThomas Mukoya/Reuters
Kenya election
Thousands of Kenyans queue to cast their votes to vote at a polling station at Olympic Primary School in KiberaAndrew Renneisen/Getty Images
Kenya election
Kenyans wait in line to cast their ballot in the general elections at a polling station in KisumuFredrik Lerneryd/AFP
Kenya election
Independent Electoral and Boundaries Commission officials check voting lists as Kenyans wait in line to cast their ballot in the general elections at a polling station in KisumuFredrik Lerneryd/AFP

At the Mutomo primary school in Gatundu, north of Nairobi, where Kenyatta is due to vote, hundreds of people had waited in line since 2am, wrapped in jackets and blankets to protect themselves against the cold and drizzle. As in the last national election in 2013, Lydia Gathoni – 102 years old and a Kenyatta stalwart – was first in line to cast her ballot.

Before entering the polling booth, she led a short prayer for peace and for her political idol. "Let God share with him the wisdom of Solomon," she intoned, clutching a rosary in one hand and her voter card wrapped in a grimy handkerchief in the other. "Let God prevail. Let God govern the country,"

Kenya election
Lydia Gathoni, 102 years old, casts her vote during the presidential election at a polling station in GatunduBaz Ratner/Reuters

The winner of Kenya's presidential race must get more than 50 percent of the votes as well as one-quarter or more votes in at least 24 of Kenya's 47 counties. If the front-runner falls short of those benchmarks, the two top contenders will contest a run-off vote.

Polls close at 5pm, and first results are not expected before Wednesday, but a very close race might mean as long as three days before a winner emerges. Officially, election authorities have up to a week to declare the outcome

The razor-thin margins forecast have increased the chances of glitches – innocent or otherwise – giving grounds for the loser to complain about the result, as Odinga did in 2007 and in 2013. A decade ago, vote tallying was abruptly stopped and the incumbent president declared the winner, triggering an outcry from Odinga's camp followed by outbreaks of ethnic violence in which 1,200 people were killed and 600,000 displaced. International Criminal Court cases against Kenyatta and his now-deputy, William Ruto, collapsed for lack of evidence.

In 2013, electronic voting equipment suffered widespread failures, although Odinga's decision to limit his complaints to the courts prevented any unrest.

Kenya election
Kenya's incumbent President Uhuru Kenyatta votes in Gatundu, Kiambu countySimon Maina/AFP
Kenya election
Kenyan opposition leader Raila Odinga of the National Super Alliance (NASA) coalition arrives to cast his vote during the presidential election in Kibera primary schoolThomas Mukoya/Reuters

This time – probably Odinga's last tilt at the top job in East Africa's biggest economy – the opposition has repeatedly accused the government of trying to rig the polls. At two polling stations in Kisumu, an Odinga stronghold in western Kenya swept up in the 2007 unrest, a few complained about their names not being in the electronic voter register, a high-tech system introduced this year to combat fraud.

Kenya election
An electoral official checks a voter's identity at a polling station in Gatundu, Kiambu countySimon Maina/AFP
Kenya election
An electoral official marks a voter's finger at a polling station in Gatundu, Kiambu countySimon Maina/AFP

The government has deployed more than 150,000 security personnel, including wildlife rangers, to protect 41,000 polling stations. The torture and murder last week of a top election official and the deportation of two foreign Odinga advisers have fuelled wild online conspiracy theories and "fake news" items consumed voraciously by Kenya's tech-savvy population.

Kenyatta, the 55-year-old son of Kenya's first president after independence from British colonial rule, campaigned on a record of major infrastructure projects, many backed by China, and claimed strong economic growth. Odinga, 72, is also the son of a leader of the independence struggle and has cast himself as a champion of the poor and a harsh critic of endemic corruption in many state institutions.

Tens of thousands have returned to their ethnic strongholds ahead of the elections, fearful of a repeat of the 2007 violence. Most political affiliations in Kenya are tied to ethnicity. Many voters see Kenyatta as the candidate of the Kikuyu people, the country's largest ethnic group, while Odinga represents the Luo. Kenya has 44 official tribes and election candidates usually form alliances with politicians from other ethnic groups to broaden their appeal. Odinga has built his National Super Alliance (NASA) with leaders from four groups, stitching together support from politicians from three other ethnic backgrounds.

Kenya election
Turkana tribeswomen wait to vote in front of a polling station during elections in a village near BaragoyGoran Tomasevic/Reuters
Kenya election
Samburu tribeswomen wait to vote in front of a polling station during the election in a village near BaragoyGoran Tomasevic/Reuters
Kenya election
Maasai voters queue at a polling station in Ewaso Kendo, Kajiado West CountyCarl de Souza/AFP
Kenya election
Kenyan inmates wait in line to cast their vote in Kisumu maximum prison on Lake Victoria. This is the first time that inmates in Kenya are being allowed to voteKevin Midigo/AFP

Odinga dominates the west of the country and the coast, where residents feel neglected by the central government. Cosmopolitan urban centres are largely up for grabs, although Odinga's party controls the three big cities – Nairobi, Mombasa and his home town of Kisumu.

Kenya election
Women queue as they wait for a polling station to open to cast their votes in the presidential election in MombasaSiegfried Modola/Reuters

Kenyatta and his deputy William Ruto command central Kenya and the Rift Valley, home to their Kikuyu and Kalenjin ethnic groups, which have supplied all of Kenya's presidents since independence. The current government of 22 ministers has five Kikuyu and five Kalenjin members.

Kenya election
A Samburu warrior votes in Nkirish, in Kenya's Eastern ProvinceCyril Villemain/AFP