The "Ejército del Perú" was officially established on August 18, 1821 when the government of general José de San Martín established the "Legión Peruana de la Guardia" (Peruvian Guard Legion), although some militia units had been formed before. Peruvian troops were key participants in the final campaign against Spanish rule in South America, under the leadership of general Simón Bolívar, which ended victoriously in the battles of Junín and Ayacucho in 1824.