In 1824, when the Peruvian Congress named Simón Bolívar dictator or "Liberator of Peru", Castilla joined Bolivar's army, fighting in the decisive Battle of Ayacucho, which helped Peru gain its independence from Spain.