The lyrics to "When Johnny Comes Marching Home" were written by the Irish-American bandleader Patrick Gilmore during the Civil War. Its first sheet music publication was deposited in the Library of Congress on September 26, 1863, with words and music credited to "Louis Lambert"; copyright was retained by the publisher, Henry Tolman & Co., of Boston. Gilmore is said to have written the song for his sister Annie as she prayed for the safe return of her fiancé, Union Light Artillery Captain John O'Rourke.
The melody was previously published around July 1, 1863, as the music to the Civil War drinking song "Johnny Fill Up the Bowl". The tune is also used for "Johnny I Hardly Knew Ye".