At the outbreak of the Mexican–American War in 1846, he enlisted as a private in the 1st Pennsylvania Volunteers, and, the next year, was commissioned a second lieutenant in the 9th Infantry of the Regular Army. He took part in many of the battles of the war, including Chapultepec and Mexico City.