It is unknown when humans first occupied the area before the first European visits in the 15th century. The earliest known remnants in the region were discovered at Peers cave in Fish Hoek and date to around 15,000-12,000 years ago. There is no written history from the area until it was first mentioned by Portuguese explorer Bartolomeu Dias in 1486. Vasco da Gama recorded a sighting of the Cape of Good Hope in 1497. In the late 16th century, French, Dutch, Portuguese, Danish and English ships regularly stopped over in Table Bay en route to the Indies, trading tobacco, copper and iron with the Khoikhoi in exchange for fresh meat. In 1652, Jan van Riebeeck and other employees of the Dutch East India Company were sent to the Cape to establish a way-station for ships travelling to the Dutch East Indies.
The settlement grew slowly during this period: the labour shortage prompted the authorities to import slaves from Indonesia and Madagascar and 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 plants was 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. During the French Revolution and Napoleonic wars, the Netherlands was repeatedly occupied by France, and Great Britain moved to take control of Dutch colonies. Britain captured Cape Town in 1795, but it was returned to the Netherlands by treaty in 1803. British forces occupied the Cape again in 1806 after the battle of Bloubergstrand, but in the Anglo-Dutch Treaty, Cape Town was permanently ceded to Britain in 1814. It became the capital of the newly formed Cape Colony, whose territory expanded very substantially during the 1800s.
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 was won Britain. 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 then became the legislative capital of the Union, and later of the Republic of South Africa.