The Army of the Andes
San Martín immediately began recruiting, outfitting and drilling the Army of the Andes. By the end of 1816 he had an army of some 5,000 men, including a healthy mix of infantry, cavalry, artillerymen and support forces. He recruited officers and accepted tough gauchos into his army, usually as horsemen. Chilean exiles were welcome, and he appointed O'Higgins as his immediate subordinate. There was even a regiment of British soldiers who would fight bravely in Chile.
San Martín was obsessed with details, and the army was as well equipped and trained as he could make it. The horses all had shoes, blankets, boots and weapons were procured, food was ordered and preserved, etc. No detail was too trivial for San Martín and the Army of the Andes, and his planning would pay off when the army crossed the Andes.
Crossing the Andes
In January of 1817, the army set off. The Spanish forces in Chile were expecting him and he knew it. Should the Spanish decide to defend the pass he chose, he could face a hard battle with weary troops. But he fooled the Spanish by mentioning an incorrect route "in confidence" to some Indian allies. As he had suspected, the Indians were playing both sides and sold the information to the Spanish. Therefore, the royalist armies were far to the south of where San Martín actually crossed.
The crossing was arduous, as flatland soldiers and gauchos struggled with the freezing cold and high altitudes, but San Martín's meticulous planning paid off and he lost relatively few men and animals. In February of 1817, the Army of the Andes entered Chile unopposed.
The Battle of Chacabuco
The Spanish soon realized they had been duped and scrambled to keep the Army of the Andes out of Santiago. The Governor, Casimiro Marcó del Pont, sent all available forces out under the command of General Rafael Maroto with the purpose of delaying San Martín until reinforcements could arrive. They met at the Battle of Chacabuco on February 12, 1817. The result was a huge patriot victory: Maroto was completely routed, losing half his force, while the patriot losses were negligible. The Spanish in Santiago fled, and San Martín rode triumphantly into the city at the head of his army.
The Battle of Maipu
San Martín still believed that for Argentina and Chile to be truly free, the Spanish needed to be removed from their stronghold in Peru. Still covered in glory from his triumph at Chacabuco, he returned to Buenos Aires to get funds and reinforcements.
News from Chile soon brought him hurrying back across the Andes. Royalist and Spanish forces in southern Chile had joined with reinforcements and were threatening Santiago. San Martín took charge of the patriot forces once more and met the Spanish at the Battle of Maipu on April 5, 1818. The patriots crushed the Spanish army, killing some 2,000, capturing around 2,200 and seizing all of the Spanish artillery. The stunning victory at Maipu marked the definitive liberation of Chile: Spain would never again mount a serious threat to the area.
On to Peru
With Chile finally secure, San Martin could set his sights on Peru at last. He began building or acquiring a navy for Chile: a tricky task, given that the governments in Santiago and Buenos Aires were virtually bankrupt. It was difficult to make Chileans and Argentines see the benefits of liberating Peru, but San Martín had great prestige by then and he was able to convince them. In August of 1820 he departed from Valparaiso with a modest army of some 4,700 soldiers and 25 cannons, well supplied with horses, weapons and food. It was a smaller force than what San Martín believed he would need.
March to Lima
San Martín believed that the best way to liberate Peru was to get the Peruvian people to accept independence voluntarily. By 1820, royalist Peru was an isolated outpost of Spanish influence. San Martín had liberated Chile and Argentina to the south, and Simón Bolívar and Antonio José de Sucre had freed Ecuador, Colombia and Venezuela to the north, leaving only Peru and present-day Bolivia under Spanish rule. San Martín had brought a printing press with him on the expedition, and he began bombarding citizens of Peru with pro-independence propaganda. He maintained a steady correspondence with Viceroys Joaquín de la Pezuela and José de la Serna in which he urged them to accept the inevitability of independence and surrender willingly in order to avoid bloodshed.
Meanwhile, San Martín's army was closing in on Lima. He captured Pisco on September 7 and Huacho on November 12. Viceroy La Serna responded by moving the royalist army from Lima to the defensible port of Collao in July of 1821, basically abandoning the city of Lima to San Martín. The people of Lima, who feared an uprising by slaves and Indians more than they feared the army of Argentines and Chileans at their doorstep, invited San Martin into the city. On July 12, 1821, he triumphantly entered Lima to the cheers of the populace.
Protector of Peru
On July 28, 1821, Peru officially declared independence, and on August 3, San Martín was named "Protector of Peru" and set about setting up a government. His brief rule was enlightened and marked by stabilizing the economy, freeing slaves, giving freedom to the Peruvian Indians and abolishing such hateful institutions as censorship and the Inquisition.
The Spanish had armies at the port of Collao and high in the mountains. San Martín starved out the garrison at Collao and waited for the Spanish army to attack him along the narrow, easily defended coastline leading to Lima: they wisely declined, leaving a sort of stalemate. San Martín would later be accused of cowardice for failing to seek out the Spanish army, but to do so would have been foolish and unnecessary.