Building a Homelab Mastodon Instance
The why
Mastodon is cool. Really cool in fact. It feels like the last bastion of the old social media experience I grew up with. Bots are labeled as bots, AI content scrapers are discouraged, people are real, admins open their instances from the kindness of their heart and wallet. It has no algorithm to tailor your experience to, it’s just people posting things they want to post. That’s something I can get behind.
Another reason I love it is that it’s “federated.” Which, is basically a tiny slice of servers connected using the ActivityPub protocol. As such, it feels like an internet within the internet. It’s called the “Fediverse” by a lot of people on Mastodon for that reason.
How it started
I first created an instance on Azure, thinking it would only cost about $10 a month. I was so wrong. I started off knowing I wasn’t going to be a big instance. Just an instance where I can post some stuff and follow some people, nothing crazy. I wasn’t too terribly concerned with browsing Mastodon. In the “Running a single user instance” section, I talk about why that’s difficult. So, I put in for a 2 core / 2 GB Ubuntu server. I couldn’t even build Mastodon without it tapping out that memory usage.
So, I scaled up to 4 GB. That, at least, got me in the door. I priced it out to about $25-$30 a month. This is a single instance, I’m the only user, and was only needing it for posting. So, I started to look for alternatives.
Cloudflare tunnels
Cloudflare tunnels are cool. Really cool in fact. It allows you to host something on your network at home and share it out fully encrypted, without having to touch your router. It does this by Cloudflare’s client - cloudflared. You just install that on a Linux machine, run it and copy/paste some keys to connect it on Cloudflare’s end. You can connect your domain to it and even set it to redirect to https only. So, I did that but it was a bigger learning process than I thought. You can read all about it on this post. I did have to migrate Mastodon over, and that was a big pain too. I spent hours trying to figure out why it was giving me a 500 error, only to discover I didn’t match the versions correctly between the Azure VM and my new machine, whoops.
Homelab specs
I decided to go with these specs below:
- VM on Proxmox running Ubuntu
- 4 Cores
- 8 GB of RAM
- 140 GB HDD
Running a successful single user instance on minimal hardware
This part was the challenge. Mastodon is huge, there are thousands of posts every minute. You can sit there and watch the feed in real time. Because of the way Federation is done, everything gets downloaded locally on your machine. I think of it like this - it has to be caught to be shown to you. If my Mastodon instance is down for 12 hours, well, that’s 12 hours of lost posts I’ll never see. It does this using something called Relays. A relay allows you to connect to other instances to grab what they’re seeing. It gets complicated fast, and I’ll admit, I really don’t fully understand it. I just know I pick up on bigger instances feeds, the people I follow, hashtags I follow, etc. And that’s enough for me. After all, I could always join a bigger instance with a second user to get that browsing experience.
The data is too much! It’s too much!
I quickly realized just how difficult it was to run a small instance. Again, I wasn’t concerned with browsing. So, I whipped up the script below to clean the data every day. That solved a lot of my woes. Feel free to use it on your instance!
# Cleans up posts that are n days old, currently set to 0.
echo "Removing posts"
RAILS_ENV=production /home/mastodon/live/bin/tootctl media remove -d 0;
RAILS_ENV=production /home/mastodon/live/bin/tootctl media remove --remove-headers -d 0;
RAILS_ENV=production /home/mastodon/live/bin/tootctl media remove --prune-profiles -d 0;
RAILS_ENV=production /home/mastodon/live/bin/tootctl preview_cards remove;
echo "Done!"
Backing up my instance
Because I’m the only person with this data, it’s important I preserve the posts, right? I decided to write a complicated backup script that bundles everything together. I push it to a secondary HDD so I can have the data. I know that’s not really “disaster recovery” friendly, but I guess I could push it offsite to a cloud bucket. I like the privacy knowing I can control my data.
You can see that script and more on my GitLab
Things are still humming along
And the moment of truth, is my mastodon instance still up? Why don’t you check for yourself. I can say, I haven’t run out of hard drive space yet, I can still see posts, I can see my historical posts. I’d call that a success.