He returned to the capital in 1838 and entered the army as a second lieutenant of engineers. He took part in the Pastry War against the French (1838) and the War of Texas Independence. Initially a liberal in politics, in 1840 he defended the government of President Anastasio Bustamante (who had both liberal and conservative connections). The following year he was allied with Antonio López de Santa Anna. He fought the separatists in Yucatán and directed the fortifications at Monterrey. During the Mexican–American War, he was mayor of Chihuahua.