Nobody knows exactly when Lahore began. Alexander's historians mention no city here in 326 BCE. Legend gives the founding to Loh, a son of Rama, and a temple said to be his lies under the fort. The first document that names the city is from 982 CE, and it describes temples, large markets and orchards.
Sultan Mahmud of Ghazni took the city between 1020 and 1027, and his governor Malik Ayaz rebuilt it with walls and a masonry fort. In 1206 Qutb ud-Din Aibak was crowned here as the first Muslim sultan of the subcontinent, and Lahore drew more Persian poets than any other city. Then came the Mongols. They sacked it in 1241, and by 1398 Timur judged it too poor to loot.
The Mughals made Lahore rich again, and they left more monuments here than in Delhi or Agra. Akbar ruled from the city from 1584, rebuilt its walls and founded the Dharampura quarter. Shah Jahan, born here in 1592, added white marble to the fort and laid out the Shalimar Gardens. Aurangzeb finished the Badshahi Mosque in 1673 and the fort's Alamgiri Gate a year later. Jahangir chose to be buried at Shahdara.
After Aurangzeb died in 1707, the city went through invasion after invasion. Nader Shah's raid in 1739 shifted the trade routes away from it, and the Afghan ruler Ahmad Shah Durrani took it four times in the 1740s and 1750s. By 1765 the city had lost all its former grandeur. In 1799 Ranjit Singh entered, by tradition through the Lohari Gate, and made Lahore capital of the Sikh Empire. He restored some splendour, but he also stripped Mughal tombs of their marble and used the Badshahi Mosque as an ammunition store and stable.
The British annexed Punjab in 1849 and largely left the Walled City alone, building their own town to the south around the Mall. They put the railway station just outside the old walls after the 1857 uprising and styled it as a castle. The Mall filled with Indo-Saracenic buildings, among them the Lahore Museum, and the cantonment grew on the south-eastern side. In 1921 Model Town was laid out as a garden suburb.
Lahore sat at the centre of both independence movements. Nehru's declaration of Indian independence passed here at midnight on 31 December 1929, and in 1940 the Muslim League passed the Lahore Resolution demanding Pakistan. Partition then hit the city hard. Riots in the spring of 1947 destroyed 6,000 of its 82,000 homes, and by late August two-thirds of its Hindus and Sikhs had gone. Shah Alami Bazaar, the old Hindu commercial quarter, burned to the ground. Three days after independence, the border award put Lahore in Pakistan.
Partition left Lahore with a weak economy, and Karachi, not Lahore, became the capital. The city recovered slowly. It became capital of West Pakistan in 1955, and it finished the Minar-e-Pakistan in 1968 on the spot where the 1940 resolution was passed. Lahore hosted the Islamic Summit in 1974 and the cricket World Cup final in 1996. A restoration of the Walled City began in 2009 with World Bank money, starting with the route from Akbari Gate to the fort.