Located at the middle of a trade route between the northern Adriatic Sea and the Danube region, it was the historical capital of Carniola, a Slovene-inhabited part of Austria-Hungary, and it has been the cultural, educational, economic, political, and administrative center of independent Slovenia since 1991. Its central geographic location within Slovenia, transport connections, concentration of industry, scientific and research institutions and cultural tradition are contributing factors to its leading position.
Around 2000 BC, the Ljubljana Marshes in the immediate vicinity of Ljubljana were settled by people living in pile dwellings. These lake-dwelling people lived through hunting, fishing and primitive agriculture. To get around the marshes, they used dugout canoes made by cutting out the inside of tree trunks. Their archeological remains, nowadays in the Municipality of Ig, have been designated a UNESCO World Heritage Site since June 2011, in the common nomination of six Alpine states.
Later, the area remained a transit point for numerous tribes and peoples, among them Illyrians, followed by a mixed nation of Celts and Illyrians called the Iapydes, and then in the 3rd century BC a Celtic tribe, the Taurisci.
Around 50 BC, the Romans built a military encampment that later became a permanent settlement called Iulia Aemona. This entrenched fort was occupied by the Legio XV Apollinaris. In 452, it was destroyed by the Huns under Attila's orders, and later by the Ostrogoths and the Lombards. Emona housed 5,000–6,000 inhabitants and played an important role during numerous battles. Its plastered brick houses, painted in different colors, were already connected to a drainage system. In the 6th century, the ancestors of the Slovenes moved in. In the 9th century, the Slovenes fell under Frankish domination, while experiencing frequent Magyar raids. Not much is known about the area during the settlement of Slavs in the period between the downfall of Emona and the Early Middle Ages.
The medieval Ljubljana's oldest mentioning was found in 2000 at the occasion of 500 anniversary of House of Gorizia's dissolution in a document from the Udine Cathedral archive, dating from 1112 to 1125, that cited Ljubljana Castle (castrum Leibach) and twenty farms surrounding it as a gift received by Patriarchate of Aquileia from a nobleman Rudolf of Tarcento. Whereas at the time, Ljubljana Castle was in ownership of the Spanheim family, the surrounding agrarian estate belonged to a number of noblemen.
When exactly Ljubljana acquired its town rights is not known, but it was no later than 1220.
At around 1200, the right to hold a market was granted to the Old Square, which was at the time one of the three districts Ljubljana originated from, that additionally included area called "Town" built around the predecessor of present-day Ljubljana Cathedral on one side of Ljubljanica river, and New Square at the other side. The Franciscan Bridge, a predecessor of present-day Triple Bridge, and the Butchers' Bridge connected the walled areas with wood-made buildings. Seven fires erupted in the city during the Middle Ages. Artisans organized themselves into guilds. The Teutonic Knights, the Conventual Franciscans, and the Franciscans settled in the town.
In 1327, the Ljubljana's "Jewish Quarter"—now only the name of Ljubljana "Jewish street" is a remainder of it—with a synagogue was established, until Emperor Maximilian I in 1515 succumbed to medieval antisemitism and expelled Jews from Ljubljana, for which he demanded a certain payment from the town.
Ruled by King Ottokar II of Bohemia from 1270, Ljubljana was— together with Carniola region the city belonged to—conquered in 1278 by Rudolph of Habsburg and administered by the Counts of Gorizia from 1279 until 1335, when it became the capital city of Carniola. Renamed Laibach, it would be owned by the House of Habsburg until 1797.
In the 15th century, Ljubljana became recognized for its art, particularly painting and sculpture. The Roman Rite Catholic Diocese of Ljubljana was established in 1461 and the Church of St. Nicholas became the diocesan cathedral. After an earthquake in 1511, the city was rebuilt in Renaissance style and a new wall was built around it. Wooden buildings were forbidden after a large fire at New Square in 1524.
In the 16th century, the population of Ljubljana numbered 5,000, 70% of whom spoke Slovene as their first language, with most of the rest using German. The first secondary school, public library and printing house opened in Ljubljana. Ljubljana became an important educational center.
From 1529 to 1599, Ljubljana had an active Slovene Protestant community until their expulsion after which Catholic Bishop Tomaž Hren ordered the burning of eight cartloads of Protestant books in public marking the beginning of the Counter-Reformation.
In 1597, Jesuits arrived in the city, followed in 1606 by Capuchins, to eradicate Protestantism. Only 5% of all the residents of Ljubljana at the time were of Catholic confession, so it took quite a while to make it again Catholic. Jesuits organized the first theatrical productions in the town, fostered the development of Baroque music and established Catholic schools. In the middle and the second half of the 17th century, foreign architects built and renovated numerous monasteries, churches, and palaces in Ljubljana and introduced the Baroque architecture. In 1702, the Ursulines settled in the town, where, the following year, they opened the first public school for girls in the Slovene Lands. Some years later, the construction of the Ursuline Church of the Holy Trinity started. In 1779, St. Christopher's Cemetery replaced the cemetery at St. Peter's Church as the main Ljubljana cemetery.
During the Napoleonic interlude, Ljubljana (under the name Laybach) was the capital of the Illyrian Provinces from 1809 to 1813. In 1815, the city became Austrian again and from 1816 to 1849 was the administrative center of the Kingdom of Illyria in the Austrian Empire. In 1821 it hosted the Congress of Laibach, which fixed European political borders for years to come. The first train arrived in 1849 from Vienna and in 1857 the line was extended to Trieste.
In 1895, Ljubljana, then a city of 31,000, suffered a serious earthquake measuring 6.1 degrees Richter and 8–9 degrees MCS. Some 10% of its 1,400 buildings were destroyed, although casualties were light. During the reconstruction that followed, a number of districts were rebuilt in the Vienna Secession style. Public electric lighting appeared in the city in 1898. The rebuilding period between 1896 and 1910 is referred to as the "revival of Ljubljana" because of architectural changes from which a great deal of the city dates back to today and for reform of urban administration, health, education and tourism that followed. The rebuilding and quick modernization of the city were led by the mayor Ivan Hribar.
In 1918, following the end of World War I and the dissolution of Austria-Hungary, the region joined the Kingdom of Serbs, Croats and Slovenes. In 1929, Ljubljana became the capital of the Drava Banovina, a Yugoslav province.
In 1941, during World War II, Fascist Italy occupied the city, and on 3 May 1941 made Lubiana the capital of an Italian Provincia di Lubiana with the former Yugoslav general Leon Rupnik as mayor. After the Italian capitulation, Nazi Germany with SS-general Erwin Rösener and Friedrich Rainer took control in 1943 but formally the city remained the capital of an Italian province until 9 May 1945. In Ljubljana, the occupying forces established strongholds and command centers of Quisling organisations, the Anti-Communist Volunteer Militia under Italy and the Home Guard under German occupation. The city was surrounded by over of barbed wire to prevent co-operation between the resistance movement that operated within and outside the fence. Since 1985, a commemorative path has ringed the city where this iron fence once stood. Postwar reprisals resulted in a number of mass graves in Ljubljana.