The invasion of the Iberian Peninsula from 1807 to 1808 by Napoleon Bonaparte's forces proved to be critical for the independence struggle in South America, during which the local elites of Upper Peru mainly remained loyal to Spain, supporting Junta Central, a government which ruled in the name of the overthrown king Ferdinand VII of Spain. Many radical criollos in 1808–1810 began a local power struggle. Pedro Domingo Murillo proclaimed an independent state in Upper Peru in the name of King Ferdinand VII. During the following seven years, Upper Peru became the battleground between the armed forces of independent United Provinces of the Río de la Plata and royalist troops from Viceroyalty of Peru.

After 1820, the criollos who had formed the Conservative Party supported General Pedro Antonio de Olañeta. During the 1820–1823 liberal revolution in Spain, Olañeta, convinced that revolution threatened the traditional royal authority, refused to join the royalist forces or the rebel armies under the command of Simón Bolívar and Antonio José de Sucre. Olañeta did not relinquish his command even after the Peruvian royalists included him and his forces in the capitulation agreement following their defeat in the Battle of Ayacucho in 1824, the final battle of the wars of independence in Latin America. Olañeta continued his resistance until his men killed him on April 2, 1825.

During the 1829-39 presidency of Marshal Andrés de Santa Cruz, Bolivia enjoyed the most successful period of its early history with significant social and economic reforms. Santa Cruz got involved in Peruvian politics and succeeded in unifying Peru and Bolivia into the Peru–Bolivian Confederation.

During the War of the Confederation, Chilean and Peruvian rebel armies were forced to sign the peace treaty known as the Paucarpata Treaty, which included their unconditional surrender. Still, in 1839 Battle of Yungay, the army of Confederation was defeated. This was the turning point in Bolivian history; for the next 40 years, coups and short-lived regimes dominated Bolivian politics. Plagued by a vicious economic and political crisis, Bolivia's weakness was further demonstrated during the War of the Pacific (1879–1883), when it lost access to the ocean and the nitrate rich fields to Chile.

An increase in the world price of silver brought Bolivia a measure of relative prosperity and political stability in the late 1800s under the Conservative Party. In about 1907, tin replaced silver as the country's most important source of wealth. A succession of Liberal Party governments applied laissez-faire policies throughout the first two decades of the 20th century before the coup of the Republican Party in 1920.

Struggle for independence

The invasion of the Iberian Peninsula between 1807 and 1808 by Napoleon Bonaparte's forces proved critical for the independence struggle in South America. The overthrow of the Bourbon Dynasty and the placement of Joseph Bonaparte on the Spanish throne tested the loyalty of the local elites in Upper Peru, who were suddenly confronted with several conflicting authorities. Most remained loyal to Bourbons. Taking a wait-and-see attitude, they supported the Junta Central (Central Junta) in Spain, a government in the name of the abdicated king Ferdinand VII of Spain. While some liberals eagerly welcomed the reforms of colonial rule promised by Joseph Bonaparte, others supported the claims of Carlota, Ferdinand's sister, who governed Brazil with her husband, Prince Regent John of Portugal, and many radical criollos (persons of pure Spanish descent born in the New World) wanted independence for Upper Peru.Maria Luise Wagner. "Struggle for independence". In Hudson & Hanratty.

This conflict of authority resulted in a local power struggle in Upper Peru between 1808 and 1810 and constituted the first phase of the efforts to achieve independence. In 1808, the president of the audiencia, Ramón García León de Pizarro, demanded affiliation with the Junta Central. The conservative judges of the audiencia were influenced, however, by their autocratic royalist philosophy and refused to recognize the authority of the junta because they saw it as a product of a rebellion. On May 25, 1809, tensions grew when the radical criollos, refusing to recognize the junta because they wanted independence, took to the streets. The authorities soon put down this revolt, one of the first in Latin America.

On July 16, 1809, Pedro Domingo Murillo led another revolt by criollos and mestizos (those of mixed European and Indian ancestry) in La Paz and proclaimed an independent state in Upper Peru in the name of Ferdinand VII. The loyalty to Ferdinand was a pretense used to legitimize the independence movement. By November 1809, Cochabamba, Oruro, and Potosí had joined Murillo. Although the revolt was put down by royalist forces sent to La Paz by the viceroy of Peru and to Chuquisaca by the viceroy of Río de La Plata, Upper Peru was never again entirely controlled by Spain.

During the following seven years, Upper Peru became the battleground for forces of the independent United Provinces of the Río de la Plata and royalist troops from Viceroyalty of Peru. Although the royalists repulsed four Argentine invasions, guerrillas controlled most of the countryside, where they formed six significant republiquetas, or zones of insurrection. In these zones, local patriotism would eventually develop into the fight for independence.

By 1817, Upper Peru was relatively quiet and under the control of Lima. After 1820 the Conservative Party criollos supported General Pedro Antonio de Olañeta, a Charcas native, who refused to accept the measures by the Spanish Cortes (legislature) to conciliate the colonies after the liberal revolution in Spain. Olañeta, convinced that these measures threatened royal authority, refused to join the royalist forces or the rebel armies under the command of Simón Bolívar and Antonio José de Sucre. Olañeta did not relinquish his command even after the Peruvian royalists included him and his forces in the capitulation agreement following their defeat in the Battle of Ayacucho in 1824, the final battle of the wars of independence in Latin America. Olañeta continued a quixotic war until Sucre's forces defeated his forces, and he was killed by his men on April 1, 1825, in a battle that effectively ended Spanish rule in Upper Peru.

Creating Bolivia: Bolívar, Sucre, and Santa Cruz

On August 6, 1825, the assembly adopted the Bolivian Declaration of Independence. Five days later, the assembly, hoping to placate Bolívar's reservations about the independence of Upper Peru, resolved to name the new nation after him.Maria Luise Wagner. "Construction of Bolivia: Bolívar, Sucre, and Santa Cruz". In Hudson & Hanratty. Simon Bolívar entered La Paz triumphantly on August 8, 1825. During his brief rule of less than five months, he issued a flood of decrees, resolutions, and orders reflecting his ideas about government. He declared the equality of all citizens and abolished the tribute payments, replacing them with a "direct contribution" (contribución directa) that amounted to less than half of the previous payments. Bolívar also decreed a land reform to distribute land, preferably to Indians, and tried to reduce the influence of the Roman Catholic Church in politics. Most of his decrees could not be implemented during his short tenure. Still, they were included in the constitution he wrote for Bolivia after his departure in January 1826.

Despite his efforts at reform, Bolívar was outspoken about his doubts about the ability of Bolivians to govern themselves. He was careful to avoid recognizing Bolivia's independence, constantly referring to the country as Upper Peru and signing his decrees as dictator of Peru. Only in January 1826, when he turned the country over to Sucre, did he promise that the Peruvian legislature would approve Bolivia's independence.

Bolívar transferred his authority over Upper Peru to his lieutenant Antonio José de Sucre, formally installed as Bolivia's first elected president after the General Constituent Assembly convened in May and elected him. Sucre convened the Constituent Assembly in Chuquisaca to determine the region's future. Almost all delegates wanted an independent Upper Peru and rejected attachment to Argentina or Peru.

The new republic, created in the territory of the Audencia of Charcas, faced profound problems. The wars of independence had disrupted the economy. The entire mining industry was in decline because of the destruction, flooding, and abandonment of mines. Lack of investment and labor scarcity contributed to a sharp drop in silver production. Agricultural production was low, and Bolivia had to import food, even the basic staples consumed by the Indian population. The government had serious financial difficulties because of the huge military expenditures and debt payments to Peru as compensation for the army of liberation. All these problems were aggravated by the isolation of the new republic from the outside world and the difficulties of securing its borders.

During Sucre's three-year rule, governed by the Bolivian Constitution of 1826, the government tried to solve its grave financial problems, aggravated by the lack of foreign credit. Sucre reformed the existing tax structure to finance public expenditures and tried to revive silver mining by attracting foreign capital and technology. In one of the most radical attacks on the church anywhere in Latin America, he confiscated church wealth in Bolivia and closed down many monasteries. The Roman Catholic Church in Bolivia never recovered the powerful role that it had held. Import duties and taxes on the internal movement of goods were also important state revenue sources. In addition, Sucre reestablished Indian chief's tribute payments to solve the country's financial crisis.

Sucre's attempts at reform were only partially successful because Bolivia lacked the administration to carry them out. Many Conservative Party criollos turned away when his reforms threatened to challenge the economic and social patterns of the colonial past. As opposition increased, the local nationalist elite came to resent the leadership of their Venezuelan-born president. The invasion of Bolivia by the Peruvian general Agustín Gamarra and an assassination attempt in April 1828 led to Sucre's resignation. Sucre left the country for voluntary exile, convinced that "the solution was impossible".

Despite the fall of his government, Sucre's policies formed the basis for the following ten-year rule of Andrés de Santa Cruz y Calahumana (1829–39), the first native-born president, who was sworn into office in May 1829 after a series of short-term rulers. Santa Cruz, a mestizo, had a brilliant military career fighting for independence in the armies of Bolívar. His close connection with Bolívar had led to a short interlude as the president of Peru in 1826. After Sucre's resignation, it also made him a strong candidate to become Bolivia's new president.

Santa Cruz created a relatively stable economic, social, and political order in Bolivia. To overcome Bolivia's isolation, Santa Cruz opened the port of Cobija on the Pacific coast. He also devalued the silver currency to finance government activities, instituted protective tariffs in support of the local cotton cloth (tucuyo) industry, and reduced the mining tax, thereby increasing mining output. In addition, Santa Cruz codified the country's laws and enacted Latin America's first civil and commercial codes. The Higher University of San Andrés in La Paz was founded during his rule. Although Santa Cruz approved a democratic constitution, he ruled virtually as a dictator and did not tolerate opposition.