Latvia had probably been inhabited for at least a thousand years by some tribes called Livonians, who were related to the Finns or Estonians before the ancestors of present-day Latvians arrived. These latter groups were related to Lithuanians, Prussians and other groups in northern Europe. The 11th century saw German communities being established in Latvia, calling the territory Livonia, and founding the city Riga in around 1200. Livonia society became increasingly dominated by German rulers and landowners, and the original non-German inhabitants had few privileges and were largely landless peasants.
As the nations around the Baltic region emerged as powerful states in the next few centuries, which included Denmark, Sweden, Prussia, Poland and Russia, Livonia was invaded or colonised by them in succession. The major change came in 1721 when Russia defeated Sweden and took over Livonia. Latvian society by then was made up of the Russian authorities, the ethnic German landowner class and the landless indigenous population.
The 19th century saw the development of industries in Russia which established factories and developed Riga into a major port, employing a large work force. This environment, coinciding with the socialist and nationalist movements of the time in Europe, gave rise to an awakening of Latvian identify against Russian and German domination and a movement to promote the Latvian language and culture. The Russian Revolution of 1905 triggered a major uprising in Latvia against the Russian authorities and the landowner class, resulting in the subsequent repression, deaths and exile of many to Siberia.
During WW I, some Latvian factions sided with the Russian Red Army to resist German occupation of eastern Latvia and later formed a Soviet Latvian republic. However, a Latvian national and democratic coalition opposed the Soviet republic, declared independence in 1918, formed an army and defeated the Red Army decisively. In 1920, Latvia signed a peace treaty with Soviet Russia, which recognised Latvia sovereignty.
Soviet presence in Latvia was revived when it annexed Latvia and the other Baltic states in 1940 after the German-Soviet Nonaggression Pact of 1939 put these states under the Soviet sphere of influence. This was soon followed by German occupation of the territory. In total over 600,000 Latvians died during WW II. The Soviets resumed control over Latvia after WW II, deported another 42,000 Latvians and brought Russians into the country.
Following Soviet reforms in the late 1980s and changes its foreign policies, Latvia declared independence in 1991, and saw most of the Soviet troops withdraw in 1994.