He was born on 20 May 1795 in San Mateo de Huichapan, a town located in the modern-day state of Hidalgo. His parents were Pedro José Anaya y Maldonado and María Antonia de Álvarez, both Spaniards. He started his military career in the Royal Army in 1810, as a cadet in the company of Tres Villas. In June 1821 he joined the rebel army fighting for independence.