A thesaurus is, literally, a treasure chest. For most of its history the word had nothing to do with synonyms.
The Greek thēsauros meant a treasure, a storehouse or a chest for valuables. Etymonline says it is related to tithenai, "to put, place," but notes that etymologists disagree on its deeper origin. One view links it to a Proto-Indo-European root meaning "to set, put," and another treats it as a probable loanword from Pre-Greek. Latin borrowed it as thesaurus, "a treasury or hoard," and the same Latin word came down through Old French tresor to become English "treasure."
Scholars of the Renaissance liked the idea of a dictionary as a treasury of words. Wikipedia lists the Thesaurus Linguae Latinae of 1532 and the Thesaurus Linguae Graecae of 1572 as early examples, and Etymonline notes the related thesaurarie in dictionary titles in the 1590s, based on the idea of a "treasury of words." Until the 19th century, according to Wikipedia, "thesaurus" could mean any dictionary or encyclopedia. English had borrowed the word by 1823, still meaning a treasury or storehouse.
The change came with Peter Mark Roget, an English physician. According to Merriam-Webster, he completed a first draft of his list of words in 1805, when he was 26 and working in Manchester. He returned to it after retiring from science in 1848 and spent nearly four years preparing it. It was published in London in 1852 as the Thesaurus of English Words and Phrases.
Roget's book was not alphabetical. It organized about 1,000 headings into six classes, so a reader started with an idea and found the word that expressed it. Merriam-Webster says that shortly before publication he added an alphabetical index so it could be used as a simple book of synonyms. Etymonline dates the meaning of a word collection arranged by sense to 1852, the year of that title.
Roget was not the first to arrange words by meaning. Wikipedia points to the Sanskrit Amarakosha of the 4th century and to John Wilkins's 1668 work, which also groups related words together. Roget's contribution was the title and the book that made it stick.
He kept revising it until his death in 1869 at the age of 90. Merriam-Webster counts about 28 editions in his lifetime, and says the book has sold more than 40 million copies. Wikipedia says it has been in print continuously since 1852.