People lived here before 5000 BC, and coins from the 3rd century BC found below Montjuïc carry the Iberian name Bárkeno. Around 15 BC the Romans laid out a fortified camp on a low rise called Mons Taber. Their forum stood roughly where Plaça de Sant Jaume is now. The Catalan government and the city hall still face each other across that square, so power has stayed on the same spot for two thousand years.
Visigoths took the town in the early 5th century, and the Umayyads in the early 8th. In 801 Charlemagne's son Louis captured it after a siege. The Counts of Barcelona then became more and more independent and expanded their territory to take in much of Catalonia. In 1137 Ramon Berenguer IV married Petronilla of Aragon, joining Aragon and the County of Barcelona into one state that ruled the western Mediterranean.
Medieval Barcelona was not always kind. No town in the Crown of Aragon had more Jewish residents, who lived in the Call, until the massacre of 1391 cut their numbers; the name Montjuïc means Jewish mountain, after the cemetery on it. The city was also the Crown's main slave market until the 15th century. In 1401 its magistrates set up the Taula de canvi, often called Europe's oldest public bank.
Once Ferdinand and Isabella united the crowns in 1469, Madrid held the power. Catalonia backed the Habsburgs in the War of the Spanish Succession, and after the siege of 1714 the Bourbon king Philip V abolished its separate institutions. Industry made the city rich again from the late 18th century. Some of that money came from Catalan merchants carrying 700,000 enslaved Africans to the Caribbean between 1817 and 1867, and it paid for mansions along La Rambla.
The walled city was packed at 856 people per hectare, more than twice the density of Paris. The walls came down and the engineer Ildefons Cerdà drew the Eixample: a grid of wide streets and blocks with cut-off corners. The city council had picked another architect, but the Spanish government imposed Cerdà's plan. Rich families then raced to build the biggest, most attractive house on it, and Puig i Cadafalch's Casa Amatller is on Passeig de Gràcia. World fairs in 1888 and 1929 extended the city from Ciutadella to Barceloneta and around Plaça Espanya.
The city sided firmly with the Republic in the Civil War. Anarchist groups ran much of it for a while, as George Orwell described in Homage to Catalonia, and Italian bombers flying from Mallorca killed more than 1,000 people. Barcelona fell on 26 January 1939. Under Franco, Catalan was pushed out of public life, while migrants from Andalusia, Murcia and Galicia filled new neighbourhoods such as El Carmel.
After Franco died, more than a million people marched on 11 September 1977 for Catalan autonomy, and got it within a month. The 1992 Olympics remade the place: factories on the waterfront were torn down, three kilometres of beach appeared, and hotel rooms doubled between 1990 and 2004. Rising housing costs pushed families out to the suburbs in the late 20th century, and the police response at polling stations during the 2017 independence referendum was not peaceful.