The hill was already a town called Analamanga, "Blue Forest", settled by the Vazimba, whom Madagascar's oral history counts as the island's first occupants. Around 1610 the Merina king Andrianjaka drove them out. By the oral account, he posted a garrison of 1,000 soldiers to hold the site.
When Andriamasinavalona died in 1710, Imerina broke into four warring quadrants and Antananarivo became capital of only the southern one. The war lasted 77 years. It ended when Andrianampoinimerina of Ambohimanga took the city in 1793 and moved the political capital back to Antananarivo in 1794, while keeping Ambohimanga as the spiritual capital, a role it still has. He also created the big market at Analakely, the city's economic centre. The rice fields around the city come from the early Merina kings, who used forced labour to build the dikes and paddies.
Radama I opened the city to London Missionary Society craftsmen, who arrived in 1820 and opened the first public schools. The city was the largest on the island, with over 80,000 people. Manjakamiadana, the largest palace at the Rova, was built between 1839 and 1841.
Before 1867, only family tombs were built of stone. After a string of fires, Queen Ranavalona II issued a decree that year allowing stone and brick for other buildings. Four memorial churches went up late in the century for early Malagasy Christian martyrs. Prime Minister Rainilaiarivony governed beside three queens from 1864 to 1894, and in 1881 he brought back mandatory schooling.
French troops invaded in September 1894 and the queen surrendered after a shell blasted a hole in a building at the Rova. The hole was never repaired. France made the island a colony in 1897 and kept Antananarivo as capital. It planned boulevards like Avenue de l'Indépendance, with arcades on either side, ran tunnels through two hills, and opened a railway from Soarano station in 1897. Working class Malagasy neighbourhoods meanwhile grew without any planning.
The city had about 175,000 people in 1950. Severe floods in 1959 led to large embankments along the Betsimitatatra paddies. Independence came in 1960, and growth sped up. With its suburbs the city had almost 2.3 million people in 2018. The capital has also been the stage for political upheaval, from the 1972 rotaka that toppled President Tsiranana to the 2009 crisis that put Andry Rajoelina in power. Sprawl has outrun the infrastructure, leaving shortages of clean water and electricity and heavy traffic.