How to Install Mastodon on a Raspberry Pi

Ben
Ben
@benjislab

Setting up Mastodon, an open-source decentralized social network, on a Raspberry Pi can be a rewarding project that combines your interest in networking, single-board computers, and software projects. This blog post will guide you through the steps required to get Mastodon running on a Raspberry Pi, transforming this compact device into a node in the wider Mastodon federated universe.

What You'll Need

  • Raspberry Pi 4 (or newer): Mastodon can be resource-intensive, so using a Pi 4 or newer is recommended for better performance.
  • MicroSD Card: 16GB or larger, class 10 for the operating system and Mastodon data.
  • Power Supply: Appropriate for your Raspberry Pi model.
  • Internet Connection: Ethernet or Wi-Fi.
  • A Domain Name: Required for setting up Mastodon's web interface.
  • Time and Patience: Setting up Mastodon involves several steps, including some that may require troubleshooting.

Step 1: Setting Up Your Raspberry Pi

First, install a compatible operating system on your Raspberry Pi. Raspberry Pi OS (previously called Raspbian) is a good choice due to its widespread support and ease of use.

  1. Download the Raspberry Pi Imager from the official Raspberry Pi website.
  2. Insert your microSD card into your computer and open Raspberry Pi Imager.
  3. Choose Raspberry Pi OS Lite (without desktop environment) for a more lightweight setup.
  4. Select your microSD card and click on "Write" to flash the OS.

After the OS is installed, insert the microSD card into your Raspberry Pi, connect it to your network, and power it up. You can access your Pi via SSH, using the default hostname raspberrypi.local, with the username pi and password raspberry.

Step 2: Installing Dependencies

Mastodon relies on various software packages like Ruby, Node.js, PostgreSQL, Redis, and more. You'll need to install these dependencies on your Raspberry Pi.

  1. Update your system packages:
sudo apt update && sudo apt upgrade -y
  1. Install Node.js, Yarn, and other necessary packages:
curl -sL https://deb.nodesource.com/setup_14.x | sudo bash -
sudo apt install -y nodejs imagemagick ffmpeg libpq-dev libxml2-dev libxslt1-dev file git-core g++ libprotobuf-dev protobuf-compiler pkg-config nodejs gcc autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm6 libgdbm-dev nginx yarn
  1. Install Ruby using Rbenv (for easier Ruby version management):
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
source ~/.bashrc
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
rbenv install 2.7.2
rbenv global 2.7.2

Step 3: Setting Up Mastodon

Once the dependencies are installed, you can proceed to set up Mastodon.

  1. Clone the Mastodon repository:
git clone https://github.com/tootsuite/mastodon.git ~/mastodon
cd ~/mastodon
  1. Install Ruby and Node.js dependencies:
bundle install
yarn install
  1. Configure Mastodon:
  • Copy the example configuration file: cp .env.production.sample .env.production
  • Edit .env.production with your details (domain name, email, database credentials, etc.).
  • Set up the database: RAILS_ENV=production bundle exec rails db:setup
  1. Precompile assets:
RAILS_ENV=production bundle exec rails assets:precompile

Step 4: Running Mastodon

  • Use systemd to manage Mastodon services (web, sidekiq, and streaming).
  • Create a service file for each component in /etc/systemd/system.
  • Start and enable the services:
sudo systemctl start mastodon-web
sudo systemctl enable mastodon-web
sudo systemctl start mastodon-sidekiq
sudo systemctl enable mastodon-sidekiq
sudo systemctl start mastodon-streaming
sudo systemctl enable mastodon-streaming

Step 5: Configuring Nginx

  • Configure Nginx to proxy requests to Mastodon. You'll find an example Nginx configuration in the Mastodon repository. Adjust it as necessary for your domain and paths.

Step 6: Finishing Touches

Finally, secure your Mastodon instance with Let's Encrypt SSL certificates and set up a cron job for regular maintenance tasks.

Congratulations! You've set up Mastodon on your Raspberry Pi. Now you can enjoy participating in or even moderating your own corner of the fediverse from this compact, efficient computer.

This guide provides a broad overview of the process. Depending on your specific setup and requirements, you may need to adapt steps, troubleshoot issues, and consult the official Mastodon documentation for more detailed guidance.