BEIRUT -- Lebanon’s parliament on Monday elected Michel Aoun, an 81-year-old former army commander and strong ally of the militant group Hezbollah, as the country’s president, ending a more than two-year vacuum in the top post and a political crisis that brought state institutions perilously close to collapse.

Aoun secured a simple majority of votes in the house after a chaotic session that saw several rounds of voting because extra ballots appeared in the ballot box each time. He garnered 83 votes out of 127 lawmakers present at the session.

He also failed to get elected by a two thirds majority in the first round, as had been widely expected.

Get Breaking News Delivered to Your Inbox

Members of parliament broke out in thunderous applause after Aoun finally was declared president by Parliament Speaker Nabih Berri. His supporters across the country erupted in cheers as they watched the proceedings on huge screens set up in the streets. Brief celebratory gunfire could also be heard in the capital.

Aoun’s election is seen by many as a clear victory for the pro-Iranian axis in the Middle East, giving a boost to Hezbollah and the Shiite Lebanese group’s ally, Syrian President Bashar Assad.

Aoun has a wide support base, mostly among Lebanon’s educated youth, but is a divisive figure in Lebanon for his role in the 1975-90 civil war. Still, there is cautious hope that his election would breathe some life into state institutions that have been paralyzed for too long.

Lebanon has been without a head of state for 29 months after President Michel Suleiman stepped down at the end of his term in May 2014. Since then, 45 sessions to elect a new leader have failed due to political infighting that led to of a lack of quorum as Aoun’s block and allied Hezbollah lawmakers boycotted the sessions because his election was not guaranteed.

In the end, it took an about-face by former Prime Minister Saad Hariri, Lebanon’s Saudi-backed main Sunni leader, who formally endorsed Aoun for president last week - reportedly in exchange for Aoun promising him the position of prime minister.

Aoun was quickly sworn in as Lebanon’s 13th president, pledging political and economic reform and urging a “real partnership” among notoriously divided Lebanese political factions.

Following the parliament session, Aoun is expected to drive to the presidential palace in the southeastern Beirut suburb of Baabda, returning exactly 26 years after he was forced out of it as army commander and interim premier by Syrian forces and Lebanese troops loyal to a rival commander.

Ahead of the vote, army helicopters flew over the city and cars were banned from entering most of central Beirut. Metal detectors were set up in the streets around Parliament.