I've been smoking cannabis everyday for probably 14 years. You gotta see what works for you, and what works in what states of mind.
It's the only thing that picks my mood up, and makes me significantly less irritable, in exchange for this however, it often will give me a bit more anxiety.
Will it help with loneliness? Not completely, but it might help take your mind off it, maybe help you enjoy listening to music, or a walk, or whatever it is, even doing house work is much more enjoyable when high. I imagine it may be more helpful in this regard, if you don't smoke everyday, since it's a bit different, something to enjoy like having a drink.
For me however, I smoke multiple times a day, irregardless of my state of mind and sometimes, it makes me think heavier on how I'm feeling and may end up focusing even more negative things, like feeling incredibly isolated also. But overall I enjoy smoking and it does boost my mood irregardless, and even if I hit a bad high, often I end up quite creative with it anyway can outwardly express myself, which may comfort me also. But I've smoked through losing people and it can't heal it, but I also couldn't do without it.
Only thing you can do is experiment. I'm sorry about the legality of it. It's illegal here too, but luckily for me most people and the police (unless it's a right asshole) turn a blind eye too it for personal use. You'll need to find out where the stoners are hanging about, and if you ever pass by someone having a puff, you can try asking them for a contact, as long as they don't seem dodgy, do be careful please when going about trying to find drugs.