The city was first founded in 1536 by a Spanish gold-seeking expedition under Pedro de Mendoza. However, attacks by indigenous peoples forced the settlers in 1539 to move to Asunción (now the capital of Paraguay), and in 1541 the old site was burned. A second and permanent settlement was begun in 1580 by Juan de Garay, who set out from Asunción. Although Spain long neglected Buenos Aires in favor of the riches of Mexico and Peru, the settlement's growth was enhanced by the development of trade, much of it contraband.
In 1617 the province of Buenos Aires, or Río de la Plata, was separated from the administration of Asunción and was given its own governor; a bishopric was established there in 1620. During the 17th cent. the city ceased to be endangered by indigenous peoples, but French, Portuguese, and Danish raids were frequent. Buenos Aires remained subordinate to the Spanish viceroy in Peru until 1776, when it became the capital of a newly created viceroyalty of the Río de la Plata, including much of present-day Argentina, Uruguay, Paraguay, and Bolivia.
Prosperity increased with the gradual removal of restrictions on trade, which formerly had to pass through Lima, Peru. The creation of an open port at Buenos Aires by Charles III of Spain, however, only made the porteños more desirous of separation from the Spanish Empire. In 1806, when Spain was allied with France during the Napoleonic Wars, British troops invaded Buenos Aires; their expulsion by the colonial militia without Spanish help further stimulated the drive for independence from Spain. Another British attack was repelled the following year. On May 25, 1810 (now celebrated as a national holiday), armed citizens of the cabildo, or town council, successfully demanded the resignation of the Spanish viceroy and established a provisional representative government. This action inaugurated the Latin American revolt against Spanish rule.
Argentina's official independence (July 9, 1816) was followed by a long conflict between the unitarians, strongest in Buenos Aires prov., who advocated a centralized government dominated by the city of Buenos Aires, and the federalists, mostly from the interior provinces, who supported provincial autonomy and equality. In 1853 the city and province of Buenos Aires refused to participate in a constituent congress and seceded from Argentina. National political unity was finally achieved when Bartolomé Mitre became Argentina's president in 1862 and made Buenos Aires his capital. Bitterness between Buenos Aires and the province continued, however, until 1880, when the city was detached from the province and federalized. A new city, La Plata, was built as the provincial capital.
Argentine railroad construction in the second half of the 19th cent. stimulated settlement and cultivation of the pampas, whose products Buenos Aires marketed and exported. The city's spectacular economic development attracted immigrants from all over the world through the 1920s. Shantytowns built on the city's margins remained through the 1950s. The development of the city's transportation system in the 1970s and 80s facilitated economic growth.