Stevenson had long been intrigued by the idea of how human personalities can reflect the interplay of good and evil. While still a teenager, he developed a script for a play about William Brodie, a Scottish cabinet-maker, deacon of a trades guild, and Edinburgh city councillor, who maintained a secret life as a burglar in order to fund his mistresses and gambling addiction, which Stevenson later reworked with the help of W. E. Henley and which was produced for the first time in 1882.
Inspiration may also have come from the writer's friendship with an Edinburgh-based French teacher, Eugène Chantrelle, who was convicted and executed for the murder of his wife in May 1878. Chantrelle, who had appeared to lead a normal life in the city, poisoned his wife with opium. According to author Jeremy Hodges, Stevenson was present throughout the trial and as "the evidence unfolded he found himself, like Dr Jekyll, 'aghast before the acts of Edward Hyde'." Moreover, it was believed that the teacher had committed other murders both in France and Britain by poisoning his victims at supper parties with a "favourite dish of toasted cheese and opium".
Stevenson was friends with homosexual men, including Horatio Brown, Edmund Gosse, and John Addington Symonds, and the duality of their socially suppressed selves may have shaped his book. Symonds was shocked by the book, writing to Stevenson that "viewed as an allegory, it touches one too closely."
According to Stevenson's essay "A Chapter on Dreams" (Scribner's, Jan. 1888), after racking his brains for days over a possible plot for the story, he had a dream that gave him "three scenes, and the central idea of a voluntary change becoming involuntary". Biographer Graham Balfour quoted Stevenson's wife, Fanny Stevenson: In the small hours of one morning,[...] I was awakened by cries of horror from Louis. Thinking he had a nightmare, I awakened him. He said angrily: "Why did you wake me? I was dreaming a fine bogey tale." I had awakened him at the first transformation scene.
Lloyd Osbourne, Stevenson's stepson, wrote: "I don't believe that there was ever such a literary feat before as the writing of Dr Jekyll. I remember the first reading as though it were yesterday. Louis came downstairs in a fever; read nearly half the book aloud; and then, while we were still gasping, he was away again, and busy writing. I doubt if the first draft took so long as three days."
As was customary, Mrs Stevenson would read the draft and offer her criticisms in the margins. Stevenson was confined to bed at the time from a haemorrhage. In her comments in the manuscript, she observed that in effect the story was really an allegory, but Stevenson was writing it as a story. After a while, Stevenson called her back into the bedroom and pointed to a pile of ashes: he had burnt the manuscript in fear that he would try to salvage it, and thus forced himself to start again from nothing, writing an allegorical story as she had suggested. Scholars debate whether he really burnt his manuscript; there is no direct factual evidence for the burning, but it remains an integral part of the history of the novella. In another version of the story, Stevenson came downstairs to read the manuscript for his wife and stepson. Enraged by his wife's criticism, he went back to his room, only to come back later admitting she was right. He then threw the original draft into the fire, and stopped his wife and stepson from rescuing it.