Stevenson had long been intrigued by the idea of how human personalities can affect how to incorporate the interplay of good and evil into a story. While still a teenager, he developed a script for a play about Deacon Brodie, which he later reworked with the help of W. E. Henley and which was produced for the first time in 1882. In early 1884, he wrote the short story "Markheim", which he revised in 1884 for publication in a Christmas annual. According to his essay, "A Chapter on Dreams" (Scribner's, Jan. 1888), he wracked his brains for an idea for a story and had a dream, and upon wakening had the intuition for two or three scenes that would appear in the story Strange Case of Dr Jekyll and Mr. Hyde. 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."
Inspiration may also have come from the writer's friendship with Edinburgh-based French teacher Eugene 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".
As was customary, Mrs. Stevenson would read the draft and offer her criticisms in the margins. Robert Stevenson was confined to bed at the time from a haemorrhage. Therefore, she left her comments with the manuscript and Robert in the toilet. She said that in effect the story was really an allegory, but Robert was writing it as a story. After a while, Robert 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 in the process 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.
Stevenson re-wrote the story in three to six days. A number of later biographers have alleged that Stevenson was on drugs during the frantic re-write; for example, William Gray's revisionist history A Literary Life (2004) said he used cocaine while other biographers said he used ergot. However, the standard history, according to the accounts of his wife and son (and himself), says he was bed-ridden and sick while writing it. According to Osbourne, "The mere physical feat was tremendous and, instead of harming him, it roused and cheered him inexpressibly". He continued to refine the work for four to six weeks after the initial re-write. The novella was written in the southern English seaside town of Bournemouth, where Stevenson had moved due to ill health, to benefit from its sea air and warmer southern climate.
The name Jekyll was borrowed from Reverend Walter Jekyll, a friend of Stevenson and younger brother of horticulturalist and landscape designer Gertrude Jekyll.