Until about 4,000 years ago the sea covered this part of the Red River plain, and people have lived here ever since it drained. Around the 3rd century BC a fortified capital went up at Cổ Loa, with an outer earthwork wrapping 600 hectares. It was the first political centre of Vietnamese civilisation. Few visitors make it out there, which is a shame, since it is just north-east of town.
The area was annexed by Nanyue in 179 BC and conquered by the Han in 111 BC. In 40 AD two sisters from Mê Linh, Trưng Trắc and Trưng Nhị, led a revolt that drew in about 65 towns. In 42 the Han sent a general with 32,000 men to put it down. Under the Tang, the city of Đại La held some 25,000 people, Persians, Arabs and Javanese among them, until a mutiny in 880 ended real Chinese control.
The city's own story starts in 1010. Lý Thái Tổ, first king of the Lý dynasty, moved his court to Đại La and, saying he had seen a dragon rising from the river, named it Thăng Long, 'ascending dragon'. His successors built the One Pillar Pagoda in 1049 and the Temple of Literature in 1070. In 2004, part of their 900-year-old citadel turned up near Ba Đình Square.
Ming armies occupied the country in 1408. Twenty years later Lê Lợi threw them out, founded the Lê dynasty and renamed the capital Đông Kinh, which Europeans turned into 'Tonkin'. He is the king of the Hoàn Kiếm sword legend. By the 17th century, Western envoys guessed the population at around 100,000.
In 1802 the new Nguyễn dynasty took the court south to Huế, and in 1831 Emperor Minh Mạng gave the city the name Hà Nội. French troops seized it in 1873. From 1902 it was the capital of French Indochina, and Governor-General Paul Doumer had a hand in planning it. That era left the tree-lined boulevards, the villas and the Gothic St Joseph's Cathedral of 1886.
Japan occupied the city in 1940. On 2 September 1945 Ho Chi Minh declared independence here, but the French came back in 1947. In 1954, when the country split at the 17th parallel, Hanoi became capital of North Vietnam. American and South Vietnamese air forces then struck the north. On 19 December 1972 a B-52 was shot down into a small pond in Ba Đình, and its wreck still sits in the water.
Hanoi became capital of a reunited country in 1976. The Đổi Mới market reforms of 1986 were meant to attract foreign money, though towers took another decade to appear. In 2008 the city absorbed Hà Tây province and grew to 3,345 square kilometres. Public protest won a low-rise rule around Hoàn Kiếm Lake. As of 2026, about 8.7 million people live in the city.