Stevenson had long been intrigued by the idea of how personalities can affect a human and 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 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. One night in late September or early October 1885, possibly while he was still revising "Markheim," Stevenson had a dream, and upon wakening had the intuition for two or three scenes that would appear in the story. 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 disease of the world 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."
Louis Vivet, a mental patient who was suffering from split-personality disorder, caught Stevenson's inspiration while developing the story. As was customary, Mrs Stevenson would read the draft and offer her criticisms in the margins. Louis was confined to bed at the time from a haemorrhage. Therefore, she left her comments with the manuscript and Louis in the toilet. She said that in effect the story was really an allegory, but Louis was writing it as a story. After a while Louis 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 sea side town of Bournemouth, where Stevenson had moved due to ill health, to benefit from its sea air and warmer southern climate.