In 1807, French troops invaded Portugal. This prompted the Portuguese royal court to move to Rio de Janeiro, further contributing to the growth of the city and altering its character. The Liberal Revolution of 1820 in Portugal forced the royal family to return to Lisbon. The status of Brazil was raised to that of a constituent kingdom, and the king's eldest son became its prince regent. The Cortes in Lisbon attempted to revert Brazil to the status of colony and repatriate the prince regent, which led him to declare independence in 1822. A Brazilian Army was formed with local soldiers and foreign mercenaries. The Portuguese garrisons were defeated, and the Brazilian Empire was established as a constitutional monarchy.
The early Empire was plagued by internal conflicts: popular rebellions, republican uprisings, separatist movements and military revolts. By the middle of the century, the country had stabilized. It attracted immigrants, grew economically and began the process of industrialization. In Rio, the emperor established federal control over some of the hills surrounding the city to reforest them. This was one of the first efforts to protect the Atlantic Forest and would eventually become the Tijuca National Park. In the mountains north of the city, Petrópolis was built as a summer imperial residence with opulent palaces. On the international stage, the country triumphed over its neighbors in a series of wars and disputes to establish its modern borders. Although the imperial family enjoyed popular support, it lacked confidence in its own prospects for survival. Discontent among the armed forces, who were influenced by positivist ideas, and among large landowners, who resented the recent abolition of slavery, set the stage for regime change. A military coup, facing little resistance, established the First Brazilian Republic in 1889.
After independence, large-scale coffee cultivation started in the Paraíba Valley and then moved west in the state of São Paulo. The expansion of coffee production was associated with massive deforestation of the Atlantic Forest. Coffee became the country's main export and Brazil turned into the world's largest producer. It generated significant wealth, and the workforce had to be increased to keep the industry growing. A large number of slaves were moved from the Northeast of the country to the Southeast. As slavery was progressively abolished, the government started to encourage immigration from Europe. It had already attracted a small number of non-Portuguese Europeans, even before independence, such as the Swiss settlers founding Nova Friburgo in the state of Rio de Janeiro. But the end of the 19th century and the beginning of the 20th century saw millions of Europeans immigrating to Brazil, mainly from Portugal, Italy, Spain and Germany.
Portuguese immigrants predominated in Rio, while Italians were more numerous in São Paulo. Non-European immigrants also arrived to Brazil, including a significant number of Lebanese and Syrians, mainly Christians. An even greater number of Japanese moved to Brazil. Many went to the coffee plantations, but the government also encouraged their settlement around Registro in the underdeveloped Ribeira Valley, where rice and later bananas were cultivated. Some white southerners from the United States, called Confederados, moved to inland São Paulo after the defeat of the Confederacy. To the south, Paraná received an important contingent of Slavic immigrants. Santa Catarina and Rio Grande do Sul mainly attracted Italians and Germans to settle the interior. Rural isolation has allowed them to preserve their regional dialects for generations, but government suppression and then modern telecommunications have reduced the number of speakers significantly.
While most of the newcomers initially established themselves in rural areas, many eventually moved to urban centers. The city of São Paulo, especially, became a magnet for immigrants and started growing at a fast pace. Railroads were built to connect the coffee-producing regions to the city and then south to the port of Santos for export. The wealth generated by coffee contributed to the industrialization of São Paulo, the creation of factories and the birth of a significant urban working class.
The new republic became dominated by the agrarian oligarchy of São Paulo and Minas Gerais. The system was nicknamed café com leite politics, for the predominant industry of each state (coffee and dairy). The regime failed to represent large sectors of society, such as poor peasants, disaffected military personnel, the growing urban population and landed oligarchs outside of the two most powerful states. This led to multiple rebellions and conflicts. Some rebels looked toward the past for solace, resisting modernization efforts, such as urban and sanitary reforms, and rallying around messianic leaders and millenarian cults, sometimes with monarchist tendencies. Others looked forward, and the new ideologies of communism and anarchism gained ground in the cities. The Wall Street crash of 1929 led to a fall in coffee prices and contributed to the weakening of the regime. In 1930, politicians and oligarchs from peripheral states led a coup with the support of the military and established a new government.
The new regime had used liberal and populist rhetoric to take power, but initially ruled mostly by decree. The state of São Paulo, in reaction to losing influence and autonomy, started the Constitutionalist Revolution of 1932 against the federal government. The uprising was defeated militarily, but some of its demands were met in an attempt to appease the state's elite, including the promulgation of a mostly liberal Constitution in 1934. It would be short-lived. The central government veered toward authoritarianism and, after a self coup in 1937, established the Estado Novo regime through a new dictatorial Constitution.
The government centralized power and censored the press. It co-opted the integralists (a Brazilian fascist movement) and then crushed them. It suppressed the communists and attempted to control the urban working class through repression but also labor reform and mediation with capital. It promoted nationalism and enacted interventionist economic policies to stimulate the growth of local industry. Despite its authoritarian regime, Brazil fought on the side of the Allies during World War II. After the end of the war, pressure for democratization grew. The military became dissatisfied with the government and restored democracy in 1945.
The subsequent period of democracy saw a continuation of some features of the Estado Novo, such as its populism and economic nationalism, exemplified by the creation of state-owned enterprises. To develop the country, especially the interior, the state financed large infrastructure projects, such as the construction of Brasília as a new capital in the center of the country. Political crises plagued the nation. Labor and peasant movements agitated for reforms, which the government considered enacting. To prevent their implementation, military officers, supported by conservative elements and the United States, launched a coup and established a military dictatorship in 1964.
The country suffered from high inflation before the coup, but years of import substitution industrialization had contributed to economic growth and the creation of a substantial manufacturing sector. The military regime enacted policies to stabilize the economy and to promote exports. It also financed large infrastructure projects to develop remote regions, such as the Trans-Amazonian Highway, and to produce energy, such as the Itaipu Dam and the Angra dos Reis Nuclear Power Plant. The middle years of the military government saw greater economic growth.
However, economic development was uneven. It was mainly concentrated in the Southeast and the South. Other regions remained poorer with a lower level of industrialization. The Northeast, for example, had been important economically in colonial times, but later failed to modernize its economy. It had historically been dependent on the export of a few commodities prone to wide price fluctuations. It also suffered from recurrent droughts. The central government was either neglectful or incompetent at promoting growth there. Meanwhile, local society was unequal and politicians were generally more preoccupied with power than development. This situation led to a large-scale migration of people from the Northeast (nordestinos) to more prosperous large cities, especially São Paulo and Rio. They brought with them their unique culture.
Throughout the country, large-scale internal migration occurred from rural areas to cities. This, combined with natural growth, caused a rapid increase of the urban population. São Paulo grew to be the most populous metropolitan area in South America and the one with the largest economy. Many newcomers to the cities established themselves in informal settlements known as favelas, which starkly highlight the inequality of Brazilian society. The uncontrolled urbanization has provided a fertile ground for criminal organizations. Brazil became a hub for drug trafficking, especially cocaine, to Europe (sometimes via Africa) and North America. The country now suffers from high rates of violent crimes.