Installing Mastodon: A Step-by-Step Guide for Ubuntu 22.04 Users

Mastodon is a free, open-source platform for operating self-hosted social networking services. Known for its microblogging features, Mastodon enables users to follow others and post messages, images, audio, video, accessibility descriptions, polls, content warnings, animated avatars, custom emojis, and more. Mastodon applications are available on multiple platforms, including Android and iOS.

In this comprehensive guide, we will walk you through the steps to install Mastodon on Ubuntu 22.04.

Note: This guide was updated and successfully retested on 11/30/2022 to improve clarity and address issues previously reported in the comments. This was necessary due to changes in some of the software versions since the guide’s initial publication.

For those interested, an alternative installation guide for Mastodon on Ubuntu 22.04 using Docker is available here.

Prerequisites

  • A server running Ubuntu 22.04.
  • A configured root password on your server.
  • A valid domain name that points to your server’s IP address.

Getting Started

Begin by updating your system packages to their latest versions. Execute the following commands:

apt update -y
apt upgrade -y

After updating your system, install the necessary dependencies for Mastodon using the following command:

apt install git software-properties-common make apt-transport-https redis-server optipng pngquant jhead jpegoptim gifsicle imagemagick ffmpeg libpq-dev libxml2-dev libxslt1-dev file g++ libprotobuf-dev protobuf-compiler pkg-config gcc autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm-dev libidn11-dev libicu-dev libjemalloc-dev -y

Proceed to the next step once all dependencies are installed.

Install Node.js

Mastodon requires Node.js. To install Node.js, first add the Node.js repository with the following command:

curl -sL https://deb.nodesource.com/setup_16.x | bash -

Then, install Node.js version 16:

apt install nodejs -y

Download and add Yarn’s GPG key and enable the repository:

curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | tee /usr/share/keyrings/yarnkey.gpg >/dev/null
echo "deb [signed-by=/usr/share/keyrings/yarnkey.gpg] https://dl.yarnpkg.com/debian stable main" | tee /etc/apt/sources.list.d/yarn.list

Update the repository and install Yarn:

apt update -y
apt -y install yarn

Install and Configure PostgreSQL

Mastodon uses PostgreSQL. Install it with:

apt install -y postgresql postgresql-contrib

Start the PostgreSQL service:

systemctl start postgresql

Log in to the PostgreSQL shell:

sudo -u postgres -i psql

Create the Mastodon database and user:

CREATE DATABASE mastodon;
CREATE USER mastodon;
ALTER USER mastodon WITH ENCRYPTED PASSWORD 'password';
ALTER USER mastodon createdb;
ALTER DATABASE mastodon OWNER TO mastodon;

Exit the PostgreSQL shell:

\q

Install Ruby

Make sure Ruby 2.5+ is installed:

apt install -y ruby ruby-dev

Verify the Ruby version:

ruby -v

Install and Configure Mastodon

Create a dedicated user:

adduser mastodon --system --group --disabled-login

Download the latest Mastodon version:

git clone https://github.com/tootsuite/mastodon.git

Move the Mastodon directory:

mkdir -p /var/www/
mv mastodon/ /var/www/
chown -R mastodon:mastodon /var/www/mastodon/

Navigate to the Mastodon directory and check out the latest branch:

cd /var/www/mastodon/
sudo -u mastodon git checkout v4.0.2

Install all dependencies:

gem install bundler
sudo -u mastodon bundle config deployment 'true'
sudo -u mastodon bundle config without 'development test'
sudo -u mastodon bundle install -j$(getconf _NPROCESSORS_ONLN)

Run the Mastodon setup wizard:

sudo -u mastodon RAILS_ENV=production bundle exec rake mastodon:setup

Follow the prompts to configure your Mastodon instance.

Create Systemd Service File for Mastodon

Copy the pre-configured systemd service files:

cp /var/www/mastodon/dist/mastodon*.service /etc/systemd/system/

Edit service files:

sed -i 's/home\/mastodon\/live/var\/www\/mastodon/g' /etc/systemd/system/mastodon-*.service
sed -i 's/home\/mastodon\/.rbenv\/shims/usr\/local\/bin/g' /etc/systemd/system/mastodon-*.service

Reload systemd and start services:

systemctl daemon-reload
systemctl enable --now mastodon-web mastodon-sidekiq mastodon-streaming

Check service status:

systemctl status mastodon-web mastodon-sidekiq mastodon-streaming

Configure Nginx as a Reverse Proxy

Install and configure Nginx:

apt install nginx -y
cp /var/www/mastodon/dist/nginx.conf /etc/nginx/conf.d/mastodon.conf

Edit the configuration file to update your domain and paths:

nano /etc/nginx/conf.d/mastodon.conf

Create a cache directory for Nginx:

mkdir -p /var/nginx/cache/

Verify Nginx syntax and restart:

nginx -t
systemctl restart nginx

Secure Mastodon with Let’s Encrypt SSL

Install and configure Let’s Encrypt SSL certificates:

apt install snapd
snap install core
snap refresh core
snap install --classic certbot
ln -s /snap/bin/certbot /usr/bin/certbot

Obtain the SSL certificate for your domain:

certbot --nginx -d mastodon.linuxbuz.com

Access Mastodon Web Interface

Open your web browser and navigate to https://mastodon.linuxbuz.com. Sign in using the credentials you created earlier.

Login to access the Mastodon dashboard:

You should see the Mastodon dashboard:

Congratulations! You’ve successfully installed and configured Mastodon on your Ubuntu 22.04 server. Feel free to reach out if you have any questions.

FAQ

  • Q: What is Mastodon?
    A: Mastodon is a free, open-source social networking service with microblogging features.
  • Q: Which languages is Mastodon written in?
    A: Mastodon is primarily developed using Ruby and JavaScript.
  • Q: Can I install Mastodon using Docker?
    A: Yes, there is an alternative guide to install Mastodon using Docker on Ubuntu 22.04.
  • Q: What are the minimum server requirements for Mastodon?
    A: You need a server running Ubuntu 22.04 along with a valid domain name pointing to your server’s IP address.
  • Q: Does Mastodon support SSL?
    A: Yes, this guide includes steps to secure Mastodon with Let’s Encrypt SSL certificates.