BEIRUT, Lebanon — Michel Aoun, a charismatic retired general, polarizing Christian politician and ally to Hezbollah, was chosen president of Lebanon on Monday morning, ending a two-and-a-half-year vacuum that had tested the country’s ability to function without political leadership.

Mr. Aoun, 81, has developed a fervent political base of supporters who consider him a last hope for the country’s dwindling Maronite Christian community. But his detractors are just as passionate, blaming him for allying with his onetime enemies, the Syrian government, and with the militant group Hezbollah, which is backed by Iran and Syria and listed as a terrorist group by the United States.

The Lebanese Parliament met in a ceremonial session in Beirut on Monday to formally anoint Mr. Aoun, who secured the requisite number of ballots after four rounds of voting. Gunfire and honking broke out in East Beirut after Mr. Aoun passed the voting threshold in Parliament, and the proceedings were broadcast on every major TV network.

The voting itself made clear the condition of a legislature that failed on 45 previous occasions to even muster a quorum for a presidential ballot. On Monday, the speaker of Parliament had to cancel two rounds of voting simply because someone had slipped an extra ballot into the transparent box. The whole process took two hours and included votes cast for the pop star Myriam Klink and Zorba the Greek.