Castilla was born in Tarapacá (then part of the Viceroyalty of Peru), the second son of Pedro Castilla, of Spanish-Argentine origin, and Juana Marquezado de Romero, who was of part Aymara descent. In 1807 he traveled to Lima at the age of 10 to study with his brother and later continued his education in Concepción, Chile, also helping his brother with his business. In 1817 he enrolled in the Spanish colonial army during Peru's War of Independence, fighting against the independence forces sent by Argentine general José de San Martín.