Moby-Dick is based on Melville's actual experience on a whaler. On December 30, 1840, he signed on as a green hand for the maiden voyage of the Acushnet, planned to last for 52 months. Its owner, Melvin O. Bradford resembled Bildad, who signed on Ishmael, in that he was a Quaker: on several instances when he signed documents, he erased the word "swear" and replaced it with "affirm". Its captain was Valentine Pease, Jr., who was 43 years old at the start of the voyage. Although 26 men signed up as crew members, two did not show up for the ship's departure and were replaced by one new crew member. Five of the crew were foreigners, four of them Portuguese. The Scottish carpenter was one of the two who did not show for the ship's departure. There were three black men in the crew, two seaman and the cook. Fleece, the cook of the Pequod, was also black, and therefore probably modeled on this Philadelphia-born William Maiden, who was 38 years old when he signed for the Acushnet.
Only eleven of the 26 original crew members completed the voyage. The others either deserted or were regularly discharged. The First Officer, Frederic Raymond, left the ship after a "fight" with the captain. A first mate, actually called Edward C. Starbuck, was on an earlier voyage with Captain Pease, in the early 1830s, and was discharged at Tahiti under mysterious circumstances. The second mate on the Acushnet was John Hall, English-born but a naturalized American. He is identified as Stubb in an annotation in the book's copy of crew member Henry Hubbard, who, like Melville, had joined the voyage as a green hand. Hubbard also identified the model for Pip: John Backus, a little black man added to the crew during the voyage. Hubbard's annotation appears in the chapter "The Castaway" and reveals that Pip's falling into the water was authentic; Hubbard was with him in the same boat when the incident occurred.
Ahab seems to have had no model in real life, though his death may have been based on an actual event. On May 18, 1843, Melville was aboard The Star, which sailed for Honolulu. Aboard were two sailors from the Nantucket who could have told him that they had seen their second mate "taken out of a whaleboat by a foul line and drowned." The model for the Whaleman's Chapel of chapter 7 is the Seamen's Bethel on Johnny Cake Hill. Melville attended a service there shortly before he shipped out on the Acushnet, and he heard a sermon by the chaplain, 63-year-old Reverend Enoch Mudge, who is at least in part the model for Father Mapple. Even the topic of Jonah and the Whale may be authentic, for Mudge was a contributor to Sailor's Magazine, which printed in December 1840 the ninth of a series of sermons on Jonah.
In addition to his own experience on the whaling ship Acushnet, two actual events served as the genesis for Melville's tale. One was the sinking of the Nantucket ship Essex in 1820, after it was rammed by an enraged sperm whale 2,000 miles (3,200 km) from the western coast of South America. First mate Owen Chase, one of eight survivors, recorded the events in his 1821 Narrative of the Most Extraordinary and Distressing Shipwreck of the Whale-Ship Essex.
The other event was the alleged killing in the late 1830s of the albino sperm whale Mocha Dick, in the waters off the Chilean island of Mocha. Mocha Dick was rumored to have twenty or so harpoons in his back from other whalers, and appeared to attack ships with premeditated ferocity. One of his battles with a whaler served as subject for an article by explorer Jeremiah N. Reynolds in the May 1839 issue of The Knickerbocker or New-York Monthly Magazine. Melville was familiar with the article, which described:
This renowned monster, who had come off victorious in a hundred fights with his pursuers, was an old bull whale, of prodigious size and strength. From the effect of age, or more probably from a freak of nature... a singular consequence had resulted — he was white as wool!
Significantly, Reynolds writes a first-person narration that serves as a frame for the story of a whaling captain he meets. The captain resembles Ahab and suggests a similar symbolism and single-minded motivation in hunting this whale, in that when his crew first encounters Mocha Dick and cowers from him, the captain rallies them:
As he drew near, with his long curved back looming occasionally above the surface of the billows, we perceived that it was white as the surf around him; and the men stared aghast at each other, as they uttered, in a suppressed tone, the terrible name of MOCHA DICK!
"Mocha Dick or the d----l [devil]', said I, 'this boat never sheers off from any thing that wears the shape of a whale."
Mocha Dick had over 100 encounters with whalers in the decades between 1810 and the 1830s. He was described as being gigantic and covered in barnacles. Although he was the most famous, Mocha Dick was not the only white whale in the sea, nor the only whale to attack hunters.
While an accidental collision with a sperm whale at night accounted for sinking of the Union in 1807, it was not until August 1851 that the whaler Ann Alexander, while hunting in the Pacific off the Galápagos Islands, became the second vessel since the Essex to be attacked, holed and sunk by a whale. Melville remarked:
Ye Gods! What a commentator is this Ann Alexander whale. What he has to say is short & pithy & very much to the point. I wonder if my evil art has raised this monster.
While Melville had already drawn on his different sailing experiences in his previous novels, such as Mardi, he had never focused specifically on whaling. The eighteen months he spent as an ordinary seaman aboard the whaler Acushnet in 1841–42, and one incident in particular, now served as inspiration. It was during a mid-ocean "gam" (rendezvous at sea between ships) that he met Chase's son William, who lent him his father's book. Melville later wrote:
I questioned him concerning his father's adventure; [...] he went to his chest & handed me a complete copy [...] of the Narrative [of the Essex catastrophe]. This was the first printed account of it I had ever seen. The reading of this wondrous story on the landless sea, and so close to the very latitude of the shipwreck, had a surprising effect upon me.
The book was out of print, and rare. Melville let his interest in the book be known to his father-in-law, Lemuel Shaw, whose friend in Nantucket procured an imperfect but clean copy which Shaw gave to Melville in April 1851. Melville read this copy avidly, made copious notes in it, and had it bound, keeping it in his library for the rest of his life. 
Moby-Dick contains large sections—most of them narrated by Ishmael—that seemingly have nothing to do with the plot but describe aspects of the whaling business. Although there had been a successful earlier novel about Nantucket whalers, Miriam Coffin or The Whale-Fisherman (1835) by Joseph C. Hart, which is credited with influencing elements of Melville's work, most accounts of whaling tended to be sensational tales of bloody mutiny, and Melville believed that no book up to that time had portrayed the whaling industry in as fascinating or immediate a way as he had experienced it. Early Romantics also proposed that fiction was the exemplary way to describe and record history, so Melville wanted to craft something educational and definitive.
The earliest surviving mention of the composition of what became Moby-Dick is the final paragraph of the letter Melville wrote to Richard Henry Dana, Jr. on May 1, 1850:
About the "whaling voyage" — I am half way in the work, & am very glad that your suggestion so jumps with mine. It will be a strange sort of book, tho', I fear; blubber is blubber you know; tho' you may get oil out of it, the poetry runs as hard as sap from a frozen maple tree; — & to cook the thing up, one must needs throw in a little fancy, which from the nature of the thing, must be ungainly as the gambols of the whales themselves. Yet I mean to give the truth of the thing, spite of this.
Some scholars have concluded that Melville composed Moby-Dick in two or even three stages. Reasoning from a series of inconsistencies and structural developments in the final version they hypothesize that the work he mentioned to Dana was, in the words of Lawrence Buell, a "relatively straightforward" whaling adventure, but that reading Shakespeare and his encounters with Hawthorne inspired him to rewrite it as "an epic of cosmic encyclopedic proportions". Bezanson objects that the letter contains too many ambiguities to assume "that Dana's 'suggestion' would obviously be that Melville do for whaling what he had done for life on a man-of-war in White-Jacket". In addition, Dana had experienced how incomparable Melville was in dramatic story telling when he met him in Boston, so perhaps "his 'suggestion' was that Melville do a book that captured that gift". And the long sentence in the middle of the above quotation simply acknowledges that Melville is struggling with the problem, not of choosing between fact and fancy but of how to interrelate them. The most positive statements are that it will be a strange sort of a book and that Melville means to give the truth of the thing, but what thing exactly is not clear.
Melville may have found the plot before writing or developed it after the writing process was underway. Considering his elaborate use of sources, "it is safe to say" that they helped him shape the narrative, its plot included. Scholars John Bryant and Haskell Springer cite the development of the character Ishmael as another factor which prolonged Melville's process of composition and which can be deduced from the structure of the final version of the book. Ishmael in the early chapters is simply the narrator, just as the narrators in Melville's earlier sea adventures had been, but in later chapters becomes a mystical stage manager who is central to the tragedy.
Less than two months after mentioning the project to Dana, Melville reported in a letter of June 27 to Richard Bentley, his English publisher:
My Dear Sir, — In the latter part of the coming autumn I shall have ready a new work; and I write you now to propose its publication in England. The book is a romance of adventure, founded upon certain wild legends in the Southern Sperm Whale Fisheries, and illustrated by the author's own personal experience, of two years & more, as a harpooneer.
Nathaniel Hawthorne and his family had moved to a small red farmhouse near Lenox, Massachusetts, at the end of March 1850. He became friends with Oliver Wendell Holmes, Sr. and Melville beginning on August 5, 1850, when the authors met at a picnic hosted by a mutual friend. Melville wrote an unsigned review of Hawthorne's short story collection Mosses from an Old Manse titled "Hawthorne and His Mosses," which appeared in the The Literary World on August 17 and 24. Bezanson finds the essay "so deeply related to Melville's imaginative and intellectual world while writing Moby-Dick" that it could be regarded as a virtual preface and should be "everybody's prime piece of contextual reading". In the essay Melville compares Hawthorne to Shakespeare and Dante, and his "self-projection" is evident in the repeats of the word "genius", the more than two dozen references to Shakespeare, and in the insistence that Shakespeare's "unapproachability" is nonsense for an American.
The most intense work on the book was done during the winter of 1850–1851, when Melville had changed the noise of New York City for a farm in Pittsfield. The move may well have delayed finishing the book. During these months, he wrote several excited letters to Hawthorne, including one of June 1851 in which he summarizes his career: "What I feel most moved to write, that is banned, — it will not pay. Yet, altogether, write the other way I cannot. So the product is a final hash, and all my books are botches." This is the stubborn Melville who stood by Mardi and talked about his other, more commercial books with contempt. The letter also reveals how Melville experienced his development from his 25th year: "Three weeks have scarcely passed, at any time between then and now, that I have not unfolded within myself. But I feel that I am now come to the inmost leaf of the bulb, and that shortly the flower must fall to the mould." One other theory holds that getting to know Hawthorne first inspired him to write Ahab's tragic obsession into the book, but Bryant and Springer object that Melville already had experienced other encounters which could just as well have triggered his imagination, such as the Bible's Jonah and Job, Milton's Satan, Shakespeare's King Lear, Byron's heroes.
Theories of the composition of the book have been harpooned in three ways, first by raising objections against the use of evidence and the evidence itself. Scholar Robert Milder sees "insufficient evidence and doubtful methodology" at work. John Bryant finds "little concrete evidence, and nothing at all conclusive, to show that Melville radically altered the structure or conception of the book". A second type of objection is based upon Melville's intellectual development. Bezanson is not convinced that before he met Hawthorne, "Melville was not ready for the kind of book Moby-Dick became", because in his letters from the time Melville denounces his last two "straight narratives, Redburn and White-Jacket, as two books written just for the money, and he firmly stood by Mardi as the kind of book he believed in. His language is already "richly steeped in seventeenth-century mannerisms", characteristics of Moby-Dick. A third type calls upon the literary nature of passages used as evidence. According to Milder the cetological chapters cannot be leftovers from an earlier stage of composition and any theory that they are "will eventually founder on the stubborn meaningfulness of these chapters", because no scholar adhering to the theory has yet explained how these chapters "can bear intimate thematic relation to a symbolic story not yet conceived". Buell finds that theories based on a combination of selected passages from letters and what are perceived as "loose ends" in the book not only "tend to dissolve into guesswork", but he also suggests that these so-called loose ends may be intended by the author: repeatedly the book mentions "the necessary unfinishedness of immense endeavors". Despite all this, Buell finds the evidence that Melville changed his ambitions during writing "on the whole convincing".