How To Install Mastodon on Ubuntu 22.04 LTS

Install Mastodon on Ubuntu 22.04

In this tutorial, we will show you how to install Mastodon on Ubuntu 22.04 LTS. For those of you who didn’t know, Mastodon is an open-source decentralized social network. It offers microblogging features that allow you to follow other users and post messages and images with Mastodon. Its is written in Ruby and JavaScript and its open-source nature makes sure that it remains open for anyone to use privately and securely. Anyone can create a Mastodon server and build their own communities with friends. Additionally, Mastodon is supported by and available through multiple apps for iOS, Android, and other platforms

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo‘ to the commands to get root privileges. I will show you the step-by-step installation of the Mastodon social networking on Ubuntu 22.04 (Jammy Jellyfish). You can follow the same instructions for Ubuntu 22.04 and any other Debian-based distribution like Linux Mint, Elementary OS, Pop!_OS, and more as well.

Prerequisites

  • A server running one of the following operating systems: Ubuntu 22.04, 20.04, and any other Debian-based distribution like Linux Mint.
  • It’s recommended that you use a fresh OS install to prevent any potential issues.
  • SSH access to the server (or just open Terminal if you’re on a desktop).
  • A non-root sudo useror access to the root user. We recommend acting as a non-root sudo user, however, as you can harm your system if you’re not careful when acting as the root.

Install Mastodon on Ubuntu 22.04 LTS Jammy Jellyfish

Step 1. First, make sure that all your system packages are up-to-date by running the following apt commands in the terminal.

sudo apt update
sudo apt upgrade
sudo apt install software-properties-common dirmngr apt-transport-https ca-certificates redis-server curl gcc g++ make imagemagick ffmpeg libpq-dev libxml2-dev libxslt1-dev file git-core libprotobuf-dev protobuf-compiler pkg-config autoconf bison build-essential libssl-dev libyaml-dev libreadline-dev libidn11-dev libicu-dev libjemalloc-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm-dev

Step 2. Installing Node.js

By default, Node.js is not available on Ubuntu 22.04 base repository. Now run the following command below to add the NodeSource repository to your Ubuntu system:

wget -qO- https://deb.nodesource.com/setup_16.x | sudo -E bash

After adding the NodeSource repository to your system, install the Node.js using the Apt package manager:

sudo apt install nodejs

Step 3. Installing PostgreSQL.

By default, PostgreSQL is available on Ubuntu 22.04 base repository. Now run the following command below to install the latest version of PostgreSQL to your system:

sudo apt install postgresql postgresql-contrib

Once successfully installed, enable PostgreSQL (to start automatically upon system boot), start, and verify the status using the commands below:

sudo systemctl enable postgresql
sudo systemctl start postgresql
sudo systemctl status postgresql

Secure PostgreSQL.

By default, PostgreSQL created a user called “postgres” and is not password secured. Now we run the following command to generate a password for the “postgres” user account:

sudo passwd postgres

Next, create a user for Mastodon with the following command:

postgres=# CREATE USER mastodon CREATEDB;

Then, exit from the PostgreSQL shell with the following command:

postgres=#exit

Step 4. Installing Ruby.

First, you will need to create a new system user to run the Mastodon server. You can create it with the following command:

adduser --disabled-login --gecos 'Mastodon Server' mastodon

Next, log in to mastodon user with the following command:

su - mastodon

After that, download the Ruby shell script file installer to your Ubuntu system:

curl -fsSL https://github.com/rbenv/rbenv-installer/raw/HEAD/bin/rbenv-installer | bash

Next, update the path environment using the following shell script command:

echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrcecho 'eval "$(rbenv init -)"' >> ~/.bashrcsource ~/.bashrc

Finally, run the following command below to install the latest version of Ruby on Ubuntu:

rbenv install ruby 3.1.2
rbenv global 3.1.2

Now check the Ruby version using the command below:

ruby --version

Step 5. Configure Mastodon.

Now we log in to Mastodon user and download the Mastodon Git repository with the following command below:

su - mastodon
git clone https://github.com/tootsuite/mastodon.git ~/live
cd ~/live

Next, install all the dependencies with the following command:

bundle config deployment 'true'
bundle config without 'development test'
bundle install -j$(getconf _NPROCESSORS_ONLN)
yarn install --pure-lockfile

Then, we set up the Mastodon with the following command below:

RAILS_ENV=production bundle exec rake mastodon:setup

During the installation process, you will be asked several questions:

Your instance is identified by its domain name. Changing it afterward will break things.
Domain name: mastodon.example.com

Single user mode disables registrations and redirects the landing page to your public profile.
Do you want to enable single user mode? No

Are you using Docker to run Mastodon? no

PostgreSQL host: /var/run/postgresql
PostgreSQL port: 5432
Name of PostgreSQL database: mastodon_apps
Name of PostgreSQL user: mastodon
Password of PostgreSQL user: 
Database configuration works! ????

Redis host: localhost
Redis port: 6379
Redis password: 
Redis configuration works! ????

Do you want to store uploaded files on the cloud? No

Do you want to send e-mails from localhost? yes
E-mail address to send e-mails "from": Mastodon <notifications@mastodon.example.com>
Send a test e-mail with this configuration right now? no

This configuration will be written to .env.production
Save configuration? Yes

Now that configuration is saved, the database schema must be loaded.
If the database already exists, this will erase its contents.
Prepare the database now? Yes

All done! You can now power on the Mastodon server ????

Do you want to create an admin user straight away? Yes
Username: admin
E-mail: admin@idroot.us
You can login with the password: 159e442001120169eafa39849303f10
You can change your password once you login.

Once is done, exit from the Mastodon user with the following command:

exit

Step 6. Configure Nginx as a Reverse Proxy.

Now we install Nginx and Certbot to your Ubuntu system. First, add the Certbot repository using the following command below:

add-apt-repository ppa:certbot/certbot

After that, update the repository and install Certbot with Nginx by running the following command:

sudo apt update
sudo apt install nginx python-certbot-nginx

Next, copy the Nginx configuration file from the Mastodon directory to Nginx with the following command:

cp /home/mastodon/live/dist/nginx.conf /etc/nginx/sites-available/mastodon.conf
ln -s /etc/nginx/sites-available/mastodon.conf /etc/nginx/sites-enabled/

By default, the Mastodon Vhost configuration file is configured with the domain your-example-domain.com. So you will need to replace the example.com domain with your domain name in the file mastodon.conf. You can replace it with the following command:

sed -i 's/example.com/mastodon.your-domain.com/g' /etc/nginx/sites-enabled/mastodon.conf

Finally, restart the Nginx service to apply the configuration:

sudo systemctl restart nginx

Next, download the Let’s Encrypt free SSL certificate and configure Nginx to use this certificate by running the Certbot command:

certbot --nginx -d mastodon.your-domain.com

Step 7. Create Systemd Service for Mastodon.

Now we create systemd a service file for Mastodon. To do so, copy the systemd service templates from the Mastodon directory:

cp /home/mastodon/live/dist/mastodon-web.service /etc/systemd/system/
cp /home/mastodon/live/dist/mastodon-sidekiq.service /etc/systemd/system/
cp /home/mastodon/live/dist/mastodon-streaming.service /etc/systemd/system/

Then, start all services and enable them to start after reboot with the following command:

sudo systemctl start mastodon-web
sudo systemctl start mastodon-sidekiq
sudo systemctl start mastodon-streaming
sudo systemctl enable mastodon-web
sudo systemctl enable mastodon-sidekiq
sudo systemctl enable mastodon-streaming

Step 8. Configure Firewall.

Now we set up an Uncomplicated Firewall (UFW) with Apache to allow public access on default web ports for HTTP and HTTPS:

sudo ufw allow OpenSSH
sudo ufw allow 'Apache Full'
sudo ufw enable

Step 9. Accessing Mastodon Web Interface.

Once successfully installed, open your web browser and access the Mastodon installation wizard using the URL https://mastodon.your-domain.com. You will be redirected to the following page:

Install Mastodon on Ubuntu 22.04 LTS Jammy Jellyfish

Congratulations! You have successfully installed Mastodon. Thanks for using this tutorial for installing Mastodon social network on Ubuntu 22.04 LTS Jammy Jellyfish system. For additional help or useful information, we recommend you check the Mastodon website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!