→ Main article: History of Tanzania
The coastal region of East Africa was already part of a long-distance trading system from the beginning of our era, in which it was connected to the Red Sea by sailing ships. From about the 8th to 9th centuries, the Swahili culture spread along the coast, giving rise to a chain of Islamic-influenced cities along the coast from trading posts. These settlements extended as far as Mozambique. On Tanzanian territory, Kilwa Kisiwani was the main town from the 14th to the 16th century. The intrusion of the Portuguese from the south, who established intermediate stations in East Africa on their route of communication to India, brought about a considerable disturbance of this trade. After ousting the Portuguese from the Kenyan-Tanzanian coastal area, Oman became the dominant coastal power.
From the 18th century onwards, coastal civilisation exerted considerable influence on the inland area through the East African caravan trade and the associated slave trade.
In the 19th century, the Sultan of Oman moved his capital to Zanzibar, intensifying his influence on the coast and hinterland. From 1885, the Society for German Colonization acquired claims to parts of the interior and attempted to establish a colony. Their rule collapsed in 1888 in the uprising of the East African coastal population, whereupon the German Empire used military forces to conquer the territories, which then became the colony of German East Africa, which included Rwanda and Burundi in addition to what is now mainland Tanzania. During World War I, the German Schutztruppe, led by Paul von Lettow-Vorbeck, resisted Allied forces until the end of the war. The colony was conquered by British and Belgian troops from 1916 and subsequently divided up among the victors.
The Tanzanian mainland came under British rule as Tanganyika Territory and was administered as a League of Nations mandate (after World War II as a UN Trust Territory).
Tanganyika gained independence from the United Kingdom on 9 December 1961. Shortly after Zanzibar declared independence on 10 December 1963, Tanganyika (Tan) and Zanzibar (San) merged on 26 April 1964, initially under the name United Republic of Tanganyika and Zanzibar. The republic was then renamed the United Republic of Tanzania about six months later on November 1, 1964. Julius Kambarage Nyerere of the Tanganyika African National Union (TANU) became the first president. In 1977, at Nyerere's instigation, TANU and Zanzibar's Afro-Shirazi Party (ASP) merged to form Chama Cha Mapinduzi (CCM for short). Nyerere and his followers sought to build a socialist society in Tanzania, nationalized the banks, and carried out educational and land reforms.
Nyerere's goal was a specifically African socialism in distinction to the authoritarian socialism models after the Soviet Union. The model for Tanzania's socialist transformation was instead to be the "Ujamaa", the village community as a production and distribution collective. However, the extension of the Ujamaa model to larger production units failed, and with the deterioration of economic conditions, so did Nyerere's socialist vision. He resigned as state president in 1985 and as party leader in 1990. Nyerere died in 1999.
In 1992 the one-party system ended, and in 1995 democratic parliamentary and presidential elections were held for the first time since the 1970s, although the previous ruling party CCM was able to maintain its position. The president was Benjamin Mkapa, who constitutionally did not stand for re-election after ten years in office. After Jakaya Kikwete had asserted himself within the CCM party, he was elected president in 2005. Presidential and parliamentary elections were held again on 31 October 2010. Losses were predicted for the CCM under Kikwete in favour of the Wilbrod Slaa-led CHADEMA. Kikwete was re-elected with about 61 percent of the vote, Slaa received about 26 percent, and Ibrahim Lipumba of the CUF received about 8 percent. After serving two terms, he was not allowed to run again in the 2015 elections.