The earliest known remnants in the region were found at Peers Cave in Fish Hoek and date to between 15,000 and 12,000 years ago. Little is known of the history of the region's first residents, since there is no written history from the area before it was first mentioned by Portuguese explorer Bartolomeu Dias in 1488 who was the first European to reach the area and named it "Cape of Storms" (Cabo das Tormentas). It was later renamed by John II of Portugal as "Cape of Good Hope" (Cabo da Boa Esperança) because of the great optimism engendered by the opening of a sea route to India and the East. Vasco da Gama recorded a sighting of the Cape of Good Hope in 1497. In the late 16th century, Portuguese, French, Danish, Dutch and English but mainly Portuguese ships regularly stopped over in Table Bay en route to the Indies. They traded tobacco, copper and iron with the Khoikhoi in exchange for fresh meat.
A View of the Cape of Good Hope, Taken on the Spot, from on Board the 'Resolution', Capt Cooke
(1772), by William Hodges
A model of Cape Town as it would have appeared in 1800.
In 1652, Jan van Riebeeck and other employees of the Dutch East India Company (Dutch: Verenigde Oost-indische Compagnie, VOC) were sent to the Cape to establish a way-station for ships travelling to the Dutch East Indies, and the Fort de Goede Hoop (later replaced by the Castle of Good Hope). The settlement grew slowly during this period, as it was hard to find adequate labour. This labour shortage prompted the authorities to import slaves from Indonesia and Madagascar. Many of these became ancestors of the first Cape Coloured communities. Under Van Riebeeck and his successors as VOC commanders and later governors at the Cape, an impressive range of useful plants were introduced to the Cape – in the process changing the natural environment forever. Some of these, including grapes, cereals, ground nuts, potatoes, apples and citrus, had an important and lasting influence on the societies and economies of the region.
The Dutch Republic being transformed in Revolutionary France's vassal Batavian Republic, Great Britain moved to take control of its colonies. Britain captured Cape Town in 1795, but the Cape was returned to the Dutch by treaty in 1803. British forces occupied the Cape again in 1806 following the Battle of Blaauwberg. In the Anglo-Dutch Treaty of 1814, Cape Town was permanently ceded to Britain. It became the capital of the newly formed Cape Colony, whose territory expanded very substantially through the 1800s. With expansion came calls for greater independence from Britain, with the Cape attaining its own parliament (1854) and a locally accountable Prime Minister (1872). Suffrage was established according to the non-racial, but sexist Cape Qualified Franchise.
The discovery of diamonds in Griqualand West in 1867, and the Witwatersrand Gold Rush in 1886, prompted a flood of immigrants to South Africa. Conflicts between the Boer republics in the interior and the British colonial government resulted in the Second Boer War of 1899–1902, which Britain won. In 1910, Britain established the Union of South Africa, which unified the Cape Colony with the two defeated Boer Republics and the British colony of Natal. Cape Town became the legislative capital of the Union, and later of the Republic of South Africa.
In the 1948 national elections, the National Party won on a platform of apartheid (racial segregation) under the slogan of "swart gevaar". This led to the erosion and eventual abolition of the Cape's multiracial franchise, as well as to the Group Areas Act, which classified all areas according to race. Formerly multi-racial suburbs of Cape Town were either purged of unlawful residents or demolished. The most infamous example of this in Cape Town was District Six. After it was declared a whites-only region in 1965, all housing there was demolished and over 60,000 residents were forcibly removed. Many of these residents were relocated to the Cape Flats and Lavender Hill. Under apartheid, the Cape was considered a "Coloured labour preference area", to the exclusion of "Bantus", i.e. Africans.
School students from Langa, Gugulethu and Nyanga in Cape Town reacted to the news of protests against Bantu Education in Soweto in June 1976 and organised gatherings and marches which were met with resistance from the police. A number of school buildings were burnt down.
Cape Town was home to many leaders of the anti-apartheid movement. On Robben Island, a former penitentiary island 10 kilometres (6 miles) from the city, many famous political prisoners were held for years. In one of the most famous moments marking the end of apartheid, Nelson Mandela made his first public speech since his imprisonment, from the balcony of Cape Town City Hall hours after being released on 11 February 1990. His speech heralded the beginning of a new era for the country, and the first democratic election, was held four years later, on 27 April 1994. Nobel Square in the Victoria & Alfred Waterfront features statues of South Africa's four Nobel Peace Prize winners: Albert Luthuli, Desmond Tutu, F. W. de Klerk and Nelson Mandela. Since 1994, the city has struggled with problems such as drugs, a surge in violent drug-related crime and more recently gang violence. In the Cape Flats alone, there are approximately 100,000 people in over 130 different gangs in 2018. While there are some alliances, this multitude and division is also cause for conflict between groups. At the same time, the economy has surged to unprecedented levels due to the boom in the tourism and the real estate industries. With a Gini coefficient of 0.58, Cape Town has the lowest inequality rate in South Africa.