When I started working from home, the first thing I did was go out looking for a co-working studio in my city. I knew that I was going to do my best work from home at night, but I go to the "office" so I can still get coding done during the day. I have a full office setup in my apartment that I use from 6am until 11am almost everyday, I go to my "office" from 11am to 4pm and then come home, working from 9pm until midnight. It's a crazy work schedule and the thing that keeps me sane is splitting my time between my home office and co-working "office", so I'm not wasting so much time during the day with boredom/loneliness/the internet.
I understand that this may not be suitable for everyone, but it works great for me and I've worked out of almost every space you can imagine. To me, it is 100% worth it to get yourself a decent home office and/or a co-working office to be the most effective.