Illyrians lived here, and nobody knows when the first settlement began. Kotor appears in writing in 168 BC, as Acruvium in Roman times, part of the province of Dalmatia. In 535 Emperor Justinian built a fortress above the town after driving out the Ostrogoths, and the Byzantine ruler Constantine VII extended the defences towards the peak of Saint Ivan in the 10th century.
In the Middle Ages Kotor was a Dalmatian city-state that changed overlords often and kept its own institutions. Until the 11th century people here still spoke Dalmatian, a Romance language. Stefan Nemanja of Serbia took the city in 1185, and under his dynasty it kept republican institutions and traded so well in the 14th century that it rivalled Dubrovnik, then called Ragusa. Dominican and Franciscan monasteries were founded in the 13th century. After 1371 the city passed between Hungary, Venice and Bosnia, whose king Tvrtko minted coins here, and then governed itself until fear of the Ottomans pushed it to ask Venice for protection in 1420.
Venice ruled from 1420 to 1797, and its builders gave Kotor the look it has now. The sea republic's defences are why the walls count as a World Heritage Site, alongside the older listing of the bay itself. The centuries were hard: Ottoman sieges in 1538 and 1657, plague in 1572, and earthquakes in 1563 and 1667 that nearly destroyed the town. Over the entrance to the old town, a stone relief of the Yugoslav emblem replaced the Venetian lion of St Mark after the socialist revolution.
After Venice fell, Kotor passed to the Habsburgs in 1797, to Napoleon's client kingdom and then the French Empire, held for a while by a Russian squadron. The British took it in 1814. When a British ship could not move in the windless bay, residents on the shore pulled it along with ropes, and the French garrison surrendered on 5 January 1814. The Habsburgs got the town back at the Congress of Vienna and held it until 1918 as Cattaro.
In the First World War the bay was a main base of the Austro-Hungarian navy. Sailors mutinied here on 1 February 1918; it lasted three days, and four men were executed. After 1918 the town became part of Yugoslavia and was officially called Kotor. Italy annexed the area from 1941 to 1943, and the remaining Italian community, about 300 people, left after the war. Italian had been the main language of the town until the 19th century.
Tourism took off in the early 2000s, helped by cruise ships. The old town and the surrounding bay were listed by UNESCO in 1979, and the Venetian walls were added in 2017. By 2019 over 250,000 tourists were arriving. Since then Kotor has added an aquarium of local marine life in 2021 and a cable car in 2024. Montenegro has been independent since 2006.