Wikipedia has a biography of Whitman, but the essential details are these: Whitman had family and friends in the front lines of the Civil War. He volunteered as an Army nurse and worked with the wounded and amputees. He also was a great admirer of President Lincoln. His harrowing experiences poured into his poetry. Before Whitman’s death in 1892, O Captain! My Captain! was the only one of his poems available in book-length poetry anthologies. So, Whitman’s far more complex and powerful When Lilacs Last in the Dooryard Bloom’d was largely unknown until many years later.
In his biography, Walt Whitman: The Song of Himself, Jerome Loving describes the enduring popularity of Whitman’s poems about Lincoln’s death, writing: It was Whitman’s passion for the president as the redeemer of the Union and its democracy that makes the poetry so successful as a national eulogy, turning a monologue into a dialogue with the American reader. Also at work is Whitman’s mourning for Lincoln as the commander-in-chief of his beloved soldiers, who suffered and died as Lincoln now was. It was probably the poet’s intense involvement in the hospitals that made Lincoln’s death so monumental to him. … For him, Lincoln’s death symbolized the war’s most profound loss.
On the specific influences behind O Captain!—Justin Kaplan writes in, Walt Whitman: A Life, a biography that won the National Book Award: Among (the influences were) his early glimpsing of Lincoln as an archangel Captain and also the widely circulated newspaper report that the night before he was shot Lincoln dreamed about a ship entering harbor under full sail. “He had had that very dream before every great national success,” George Templeton Strong noted a week after the assassination, “and he was certain he should hear of some great piece of news within 48 hours. A poet could make something of that.” The poet also may have found some clues in Moby Dick, where Starbuck pleads with the doomed Ahab, “Oh, my Captain! My Captain! Noble soul! Grand old heart! … How cheerily, how hilariously, O my Captain, would we bowl on our way to see old Nantucket again.”
Lincoln scholar Duncan Newcomer, for the 150th anniversary of Lincoln’s Thanksgiving proclamation, draws on Whitman’s O Captain! for a moving sample sermon he calls A Captain in the Storm.
Here is …
O Captain! My Captain!
O Captain! My Captain! our fearful trip is done;
The ship has weather’d every rack, the prize we sought is won;
The port is near, the bells I hear, the people all exulting,
While follow eyes the steady keel, the vessel grim and daring:
But O heart! heart! heart!
O the bleeding drops of red,
Where on the deck my Captain lies,
Fallen cold and dead.
O Captain! my Captain! rise up and hear the bells;
Rise up—for you the flag is flung—for you the bugle trills;
For you bouquets and ribbon’d wreaths—for you the shores a-crowding;
For you they call, the swaying mass, their eager faces turning;
O captain! dear father!
This arm beneath your head;
It is some dream that on the deck,
You’ve fallen cold and dead.
My Captain does not answer, his lips are pale and still;
My father does not feel my arm, he has no pulse nor will;
The ship is anchor’d safe and sound, its voyage closed and done;
From fearful trip, the victor ship, comes in with object won;
Exult, O shores, and ring, O bells!
But I, with mournful tread,
Walk the deck my captain lies,
Fallen cold and dead.