He was born in Moquegua in the home formed by Miguel Gasteau of French descent, and Tomasa Valderrama.[3] He studied at the "La Libertad" national school, graduating in 1843 and became a cadet in the army organized by Marshal Domingo Nieto of Moquegua.[3] He then sided with Ramón Castilla to oppose the dictatorial government of Manuel Ignacio de Vivanco. After the Battle of San Antonio in December 1843, he was promoted to second lieutenant and after successive acts of arms, he was promoted to lieutenant.[3]
When, in 1854, Marshal Castilla's revolution against President José Rufino Echenique began, he held the rank of captain, participating in the battle of La Palma and rising to sergeant major during the campaign.
In 1860 he served as consul of Peru in the Brazilian state of Pará, later moving to the Moyobamba Province where he was entrusted with the compilation of colonial documents that accredited the rights of Peru over Amazonian territories claimed by Ecuador, a task that he carried out efficiently, leading the original documents to Lima to be delivered to President Ramón Castilla.
In 1864 he supported the constitutional government of President Juan Antonio Pezet and he marched to fight the Arequipa revolution led by General Mariano Ignacio Prado, occupying Arica but Lima was captured by the revolutionaries and overthrowing the Pezet administration and Gastó accepted the new authorities, participating in 1867 of the frustrated siege and attack on the city of Misti.
He returned to active service during the government of Manuel Pardo, taking part in the repressive campaign against the revolutionaries of Nicolás de Piérola, who were defeated and dispersed in the Cuesta de los Ángeles (near Moquegua), after this campaign he was appointed prefect of Puno, also holding the same position in Cusco and Ayacucho in 1878.[4]