In 1810 he returned to Peru and, from then until the arrival of José de San Martín, he was the main figure of the anticolonial conspiracies in Lima, directing the Lodge of the Copetudos.
The company led by Andrés de Santa Cruz failed in its attempt to stop the royalist advance and the expedition was defeated.
Disagreements with Congress and the arrival of Simón Bolívar determined the end of his brief government and his deportation to Guayaquil, first, and later to Europe, where he resided until 1828, then returning to America.
Riva Agüero spent his childhood in Lima, where he received his early education and a commission as lieutenant of the Regiment of the Nobility Corps in 1796 when he was thirteen years old.
He was sent to Spain to complete his military education under the supervision of influential paternal relatives (one of his uncles was Minister of the Consejo de Indias and another one lieutenant general of the Army), however he decided to abandon his career as well as his subsequent legal studies to travel to France.
Moved by the nationalist ardor caused by the Napoleonic invasion in 1808, he was involved in some early actions against the French in Guipuzcoa and Burgos and then joined the forces of General Echevarri in Cordoba participating in the Battle of Alcolea Bridge.
In Lima, Riva Agüero participated actively in various conspiracies for the Peruvian independence, the reason for which he was placed under strict surveillance by order of Viceroy Abascal.
Upon the departure of San Martín and the ensuing social instability in the country, Andrés de Santa Cruz revolted against the Peruvian Congress on 26 February 1823 and forced it to elect Riva Agüero as President.
Fearing the loss of leadership, Riva Agüero sought to conciliate with the viceroy to prevent the arrival of Bolívar, only to be arrested and accused of high treason.
During the short-lived Peru-Bolivian Confederation Riva Agüero supported Mariscal Andrés de Santa Cruz, and became president of the Republic of North Peru in 1838.
During his trip, he avoided harassment from the authorities: he was briefly arrested in Montevideo but; in Buenos Aires, he had to flee furtively after realizing that they were going to return him to Spain.
Riva Agüero was then in intensive contacts with José de San Martín, who after securing the independence of Chile planned to go to Peru.
The Viceroy Joaquín de la Pezuela then ordered the confinement of Riva Agüero in Tarma (central highlands of Peru) while preparing a boat to take him to Spain, but the legal appeal and the disruption caused by the arrival of San Martin during his Freedom Expedition led such a severe measure to be abandoned.