Gilbert was born in Zanesville, Ohio. He graduated from West Point in the famed Class of 1846, finishing 21st out of 59 students. His classmates included twenty future Civil War generals, including George B. McClellan, Stonewall Jackson, George Stoneman, Darius N. Couch, and George Pickett. During the Mexican–American War he served in the 3rd U.S. Infantry and 1st U.S. Infantry and fought at Veracruz and Mexico City. Then he served in Texas for two years. Returning to West Point in 1850 as the Assistant Professor of Geography, History and Ethics he was promoted to first lieutenant on June 10, 1850. In 1855, now a captain in the 1st Infantry, he returned to Texas for frontier duty and Indian fighting. He was also the first member of the Beta Theta Pi fraternity chapter at Ohio University (Beta Kappa).