Mohr had written the poem in 1816 , Gruber made the melody for it before Christmas 1818 . It shows that Mohr wrote the words in 1816 when he was assigned to a pilgrim church in Mariapfarr , Austria , and shows that the music was composed by Gruber in 1818 .