Both are used as synonyms but the usage make them quite distinct.
Solitude is what a person surrounded by many seeks.It is a positive step to contemplate,mull over something and eventually create something-- an idea,a solution of some vexing problem etc.
Loneliness is a very negative aspect,there is no one nearby ,no one asks for you,you are seeking company but not getting any.This leads to depression and the diseases associated with it.This is the biggest malady of modern society.
Prefer solitude not loneliness.
However,a poet has asked a question,"Oh Solitude where are the charms that sages has seen in thy face?"