The earliest account of Nairobi's history dates back to 1899 when a railway depot was built in a brackish African swamp occupied by a pastoralist people, the Maasai, the sedentary Akamba people, as well as the agriculturalist Kikuyu people who were all displaced by the colonialists. The railway complex and the buildings around it rapidly expanded and urbanized until it became the largest city of Kenya and the country's capital. The name Nairobi comes from the Maasai phrase , which translates to 'the place of cool waters'. However, Nairobi is popularly known as the "Green City in the Sun".

Pre-independence The site of Nairobi was originally a swamp land occupied by a pastoralist people, the Maasai, the long-distance trader community, Akamba People, as well as the agriculturalist Kikuyu people.Donald B. Freeman, City of Farmers: Informal Urban Agriculture in the Open Spaces of Nairobi, Kenya, McGill-Queen's Press – MQUP, 1 March 1991 The name Nairobi comes from the Maasai expression meaning 'cool waters'. referring to the cold water stream which flowed through the area.Frédéric Landy, From Urban National Parks to Natured Cities in the Global South: The Quest for Naturbanity, Springer, 20 Jul 2018, p.50 With the arrival of the Uganda Railway, the site was identified by Sir George Whitehouse for a store depot, shunting ground and camping ground for the Indian labourers working on the railway.Anne-Marie Deisser, Mugwima Njuguna, Conservation of Natural and Cultural Heritage in Kenya, UCL Press, 7 Oct 2016, p.76 In the pre-colonial era, the people of modern Kenya mostly lived in villages amongst their tribes and cultural groups, where they had rulers within their communities rather than one singular government or leader.The former swamp land occupied by the city now was once inhabited by a pastoralist people, the Maasai, the sedentary Akamba people, as well as the agriculturalist Kikuyu people, under the British East Africa protectorate when the British decided to build a railroad from Mombasa to Kisumu on the edge of Lake Victoria in order to open East Africa and make it accessible for trade and encourage colonial settlements. The Maasai were forcibly removed to allow white ranchers to use the land. The city continued to grow under British rule, and many British people settled within the city's suburbs. This continuous expansion of the city began to anger the Maasai people, as the city was devouring their land to the south. The Kikuyus were also angered and wanted the land returned to them.

Colonial settlement

Uganda Railway Whitehouse, chief engineer of the railway, favoured the site as an ideal resting place due to its high elevation, temperate climate, adequate water supply and being situated before the steep ascent of the Limuru escarpments. His choice was criticised by officials within the Protectorate government who felt the site was too flat, poorly drained and relatively infertile.

In 1894, work on the railway began. A British railroad camp and supply depot for the Uganda Railway was built in the Maasai area in 1899. The building soon became the railway's headquarters and a town grew up surrounding it, named after a watering hole known in Maasai as , meaning 'cool waters'. The location of the Nairobi railway camp was chosen due to its central position between Mombasa and Kampala, as well as its proximity to a network of rivers that could supply the camp with water. Its elevation made it cool enough for comfortable residential living. Furthermore, at 1661 meters above the sea level, the temperatures are too low for the mosquitoes carrying malaria to survive.

Platting and rebuilding (1898–1900) In 1898, he was commissioned to design the first town layout for the railway depot. It constituted two streets – Victoria Street and Station Street, ten avenues, staff quarters and an Indian commercial area. The railway arrived at Nairobi on 30 May 1899, and soon Nairobi replaced Machakos as the headquarters of the provincial administration for the Ukamba province.Roman Adrian Cybriwsky, Capital Cities around the World: An Encyclopedia of Geography, History, and Culture, ABC-CLIO, USA, 2013, p. 303The Eastern Africa Journal of Historical and Social Sciences Research, Volume 1, Indiana University, 8 Publishers, 1996

On the arrival of the railway, Whitehouse remarked that "Nairobi itself will in the course of the next two years become a large and flourishing place and already there are many applications for sites for hotels, shops and houses." The town's early years were however beset with problems of malaria leading to at least one attempt to have the town moved.

The town was totally rebuilt in the early 1900s after an outbreak of plague and the subsequent burning down of the original town. In the early 1900s, Bazaar Street, now Biashara Street, was completely rebuilt after an outbreak of plague and the burning of the original town.

Growth (1900–1963) Between 1902 and 1910, the town's population rose from 5,000 to 16,000 and grew around administration and tourism, initially in the form of big game hunting.Sana Aiyar, Indians in Kenya: The Politics of Diaspora, Harvard University Press, 2015, p.42 In 1907, Nairobi replaced Mombasa as the capital of the East Africa Protectorate.Claire C. Robertson, Trouble Showed the Way: Women, Men, and Trade in the Nairobi Area, 1890–1990, Indiana University Press, 1997, p.16 In 1919, Nairobi was declared to be a municipality. By 1905, Nairobi was a humming commercial center and replaced Mombasa as capital of the British East Africa. The city expanded, supported by the growth in administrative functions and in tourism, initially in the form of British big game hunting. As the British colonialists explored the region, they began using Nairobi as their first stop. This prompted the colonial government to build several grand hotels in the city for British tourists and big game hunters.

Nairobi continued to grow under British rule, and many Britons settled within the city's suburbs. The continuous expansion of the city began to anger the Maasai, as the city was devouring their land to the south, as well as the Kikuyu people, who felt that the land belonged to them. In 1919, Nairobi was declared to be a municipality by the British.

In 1921, Nairobi had 24,000 residents, of which circa 12,000 were native Africans.Garth Andrew Myers, Verandahs of Power: Colonialism and Space in Urban Africa, Syracuse University Press, 2003 The next decade saw growth in native African communities in Nairobi, and they began to constitute a majority for the first time. This growth caused planning issues, described by Thorntorn White and his planning team as the "Nairobi Problem". In February 1926, colonial officer Eric Dutton passed through Nairobi on his way to Mount Kenya, and said of the city:

Unrest In 1915,the British passed laws restricting the ownership of land to whites. Then followed high taxes and low wages. Blacks were forced to carry identification cards. In 1921, Harry Thuku founded the Young Kikuyu Association and began organizing protests as people became more open about their grievances against the British. On 14 March 1922,he was arrested. His arrest caused a general strike in Nairobi in which thousands of Africans protested and the British government reacted by shooting 56 protesters, 25 of whom died, the massacre shocking people worldwide, even the British. Although Thuku was exiled to a remote desert oasis, this was only the beginning of unrest that continued with escalating severity.

The Thuku riots reinforced a segregationist method of town planning, as a means to control the African population in Nairobi.

After the end of World War II, the friction developed into the Mau Mau Uprising. Jomo Kenyatta, Kenya's future president, joined the Kikuyu Central Association after moving to the urban Nairobi from a small village, becoming its general secretary in three years, a step that lead to his becoming Kenya's first prime minister and then Kenya's first president. Pressure exerted from the local people on the British resulted in Kenyan independence in 1963, with Nairobi as the capital of the new republic.

Because the area around Nairobi continued to be a popular attraction for British big game hunters, to protect the animals the Nairobi National Park was established by Britain in 1946, the first national park in East Africa. It remains unique in 2008 in that it is the only game reserve bordering on a capital city in the world.

After World War II, continuous expansion of the city angered both the indigenous Maasai and Kikuyu.Elkins, Caroline. Britain's Gulag: The Brutal End of Empire in Kenya. United Kingdom, Pimlico, 2005. This led to the Mau Mau Uprising in the 1950s, and the Lancaster House Conferences, which initiated a transition to Kenyan independence in 1963.

In the spring of 1950, the East African Trades Union Congress (EAUTC) led a nine-day general strike in the city.

Post independence

20th century Nairobi remained Kenya's capital after independence, and its continued rapid growth put pressure on the city's infrastructure, with power cuts and water shortages becoming a common occurrence.

In September 1973, the Kenyatta International Conference Centre (KICC) was opened to the public. The 28-story building at the time was designed by the Norwegian architect Karl Henrik Nøstvik and Kenyan David Mutiso. It is the only building within the city with a helipad that is open to the public. Of the buildings built in the Seventies, the KICC was the most eco-friendly and most environmentally conscious structure. Its main frame was constructed with locally available materials like gravel, sand, cement and wood, and it had wide open spaces which allowed for natural aeration and lighting. Cuboids made up the plenary hall, the tower consisted of a cylinder composed of several cuboids, and the amphitheatre and helipad both resembled cones. The tower was built around a concrete core, and it had no walls but glass windows, which allowed for maximum natural lighting. It had the largest halls in eastern and central Africa."Our History" , The Kenyatta International Convention Centre.

In 1972, the World Bank approved funds for further expansion of the then Nairobi Airport (now Jomo Kenyatta International Airport), including a new international and domestic passenger terminal building, the airport's first dedicated cargo and freight terminal, new taxiways, associated aprons, internal roads, car parks, police and fire stations, a State Pavilion, airfield and roadway lighting, a fire hydrant system, water, electrical, telecommunications and sewage systems, a dual carriageway passenger access road, security, drainage and the building of the main access road to the airport (Airport South Road). The cost of the project was more than US$29 million, US$111.8 million in 2013 dollars. On 14 March 1978, construction of the terminal building was completed on the other side of the airport's single runway and opened by President Jomo Kenyatta less than five months before his death. The airport was renamed Jomo Kenyatta International Airport in memory of its first president.

The Giraffe Centre, an animal sanctuary on the southwestern outskirts of Nairobi, was opened in 1983. It breeds the endangered Rothschild's giraffe.

The United States Embassy, then located in downtown Nairobi, was bombed in August 1998 by Al-Qaeda and the Egyptian Islamic Jihad, as one of a series of US embassy bombings. It is now the site of a memorial park.

After independence, Nairobi grew rapidly and this growth put pressure on the city's infrastructure. Power cuts and water shortages were a common occurrence, though in the past few years better city planning has helped to put some of these problems in check.

In 1975 Nairobi was the host city of the 5th Assembly of the World Council of Churches.