Coelho wrote The Alchemist in only two weeks in 1987. He explained he was able to write at this pace because the story was "already written in [his] soul".
The basic story of The Alchemist appears in previous works. In 1935, the Argentine writer, Jorge Luis Borges, published a short story called Tale of Two Dreamers in which two men dream of the other's treasure. Another version appeared in E. W. Lane's translation of The Thousand and One Nights. The story also appeared in Rumi's story, "In Baghdad, Dreaming of Cairo: In Cairo, Dreaming of Baghdad". A similar parable can also be found in a Jewish Chassidic story.