Mastodon is free and open-source software for running self-hosted social networking services. It offers microblogging features that allow you to follow other users and post messages and images with Mastodon. It is written in Ruby and JavaScript and supports audio, video and picture posts, accessibility descriptions, polls, content warnings, animated avatars, custom emojis, and more. Mastodon offers an application for various platforms like Android and iOS.

In this tutorial, we will show you how to install Mastodon on Ubuntu 22.04.

Requirements

  • A server running Ubuntu 22.04.
  • A root password is configured on your server.
  • A valid domain name is pointed to your server IP address.

Getting Started

First, it is recommended to update all your system packages with the latest version. You can do it by running the following command:

apt-get update -y

apt-get upgrade -y

After updating your system, you will need to install some dependencies required by Mastodon. You can install all of them with the following command:

apt-get 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 -y

Once all the dependencies are installed, you can proceed to the next step.

Install Node.js

Mastodon requires Node.js to be installed on your system. To install Node.js, add the Node.js repository to your server using the following command:

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

Once the repository has been added, install Node.js version 16 with the following command:

apt-get install nodejs -y

Next, download and add Yarn’s GPG key and enable the repository with the following command:

curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -

echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list

Once added, update the repository and install Yarn with the following commands:

apt-get update -y

apt-get install yarn -y

Once you have finished, you can proceed to the next step.

Install and Configure PostgreSQL

Mastodon uses PostgreSQL as a database backend. You can install the latest version of PostgreSQL with the following command:

apt-get install postgresql postgresql-contrib -y

Once installed, log in to PostgreSQL with the following command:

su - postgres

[email protected]:~$ psql

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

postgres=# CREATE USER mastodon CREATEDB;

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

postgres=#exit

Install Ruby

Mastodon uses Ruby on Rails for the back-end purpose. 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

Once created, log in to mastodon user with the following command:

su - mastodon

Next, clone the rbenv repository with the following command:

git clone https://github.com/rbenv/rbenv.git ~/.rbenv

Next, set up rbenv and ruby-build with the following commands:

cd ~/.rbenv && src/configure && make -C src

echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc

echo 'eval "$(rbenv init -)"' >> ~/.bashrc

exec bash

git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build

Once you have finished, install the latest version of Ruby with the following command:

RUBY_CONFIGURE_OPTS=--with-jemalloc rbenv install 3.0.3

Once installed, you should get the following output:

Downloading ruby-3.0.3.tar.gz...
-> https://cache.ruby-lang.org/pub/ruby/3.0/ruby-3.0.3.tar.gz
Installing ruby-3.0.3...

Next, set the Ruby available globally with the following command:

rbenv global 3.0.3

Next, update the gem and install bundler with the following command:

gem update --system

gem install bundler --no-document

Once you have finished, you can check the Ruby with the following command:

ruby --version

ruby 3.0.3p157 (2021-11-24 revision 3fb7d2cadc) [x86_64-linux]

Configure Mastodon

First, log in to mastodon user and download the mastodon git repository with the following command:

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

cd ~/live

Next, check out the latest branch with the following command:

git checkout $(git tag -l | grep -v 'rc[0-9]*$' | sort -V | tail -n 1)

You should get the following output:

Note: switching to 'v3.5.3'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c 

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at fbcbf7898 Bump version to 3.5.3 (#18530)

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

Now, setup the Mastodon with the following command:

RAILS_ENV=production bundle exec rake mastodon:setup

During the setup process, you will be asked several questions. Answer all the questions as shown below:

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_production
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 <[email protected]>
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: [email protected]
You can login with the password: 159e473bbfe846eac169eafac880b3f0
You can change your password once you login.

Once you have finished, exit from the mastodon user with the following command:

exit

Configure Nginx as a Reverse Proxy for Mastodon

Next, you will need to install Nginx and Certbot to your system. By default, the latest version of Certbot is not available in the Ubuntu 22.04 default repository. So, you will need to add the Certbot repository to your system.

You can add it with the following command:

add-apt-repository ppa:certbot/certbot

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

apt-get update -y

apt-get install nginx python-certbot-nginx -y

Once both packages are installed, 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

Next, enable the Mastodon virtual host configuration file with the following command:

ln -s /etc/nginx/sites-available/mastodon.conf /etc/nginx/sites-enabled/

By default, Mastodon virtual host configuration file is configured with domain example.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.example.com/g' /etc/nginx/sites-enabled/mastodon.conf

Next, restart the Nginx service to apply the configuration:

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.example.com

This will download a free Let’s Encrypt SSL certificate and configure the Nginx for your domain mastodon.example.com.

Create Systemd Service for Mastodon

Nginx is now installed and configured to serve Mastodon. Next, you will need to configure systemd 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/

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

systemctl start mastodon-web

systemctl start mastodon-sidekiq

systemctl start mastodon-streaming

systemctl enable mastodon-web

systemctl enable mastodon-sidekiq

systemctl enable mastodon-streaming

Next, check the status of the Mastodon service using the following command:

systemctl status mastodon-web mastodon-sidekiq mastodon-streaming

You will get the following output:

? mastodon-web.service - mastodon-web
     Loaded: loaded (/etc/systemd/system/mastodon-web.service; disabled; vendor preset: enabled)
     Active: active (running) since Tue 2022-10-18 15:47:13 UTC; 56s ago
   Main PID: 78185 (bundle)
      Tasks: 29 (limit: 2242)
     Memory: 296.7M
        CPU: 10.554s
     CGroup: /system.slice/mastodon-web.service
             ??78185 "puma 5.6.4 (tcp://127.0.0.1:3000) [live]" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "">
             ??78224 "puma: cluster worker 0: 78185 [live]" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" >
             ??78228 "puma: cluster worker 1: 78185 [live]" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" >

Oct 18 15:47:14 ubuntu2204 bundle[78185]: [78185] *  Max threads: 5
Oct 18 15:47:14 ubuntu2204 bundle[78185]: [78185] *  Environment: production
Oct 18 15:47:14 ubuntu2204 bundle[78185]: [78185] *   Master PID: 78185
Oct 18 15:47:14 ubuntu2204 bundle[78185]: [78185] *      Workers: 2
Oct 18 15:47:14 ubuntu2204 bundle[78185]: [78185] *     Restarts: (?) hot (?) phased
Oct 18 15:47:14 ubuntu2204 bundle[78185]: [78185] * Preloading application
Oct 18 15:47:27 ubuntu2204 bundle[78185]: [78185] * Listening on http://127.0.0.1:3000
Oct 18 15:47:27 ubuntu2204 bundle[78185]: [78185] Use Ctrl-C to stop
Oct 18 15:47:27 ubuntu2204 bundle[78185]: [78185] - Worker 0 (PID: 78224) booted in 0.04s, phase: 0
Oct 18 15:47:27 ubuntu2204 bundle[78185]: [78185] - Worker 1 (PID: 78228) booted in 0.02s, phase: 0

? mastodon-sidekiq.service - mastodon-sidekiq
     Loaded: loaded (/etc/systemd/system/mastodon-sidekiq.service; disabled; vendor preset: enabled)
     Active: active (running) since Tue 2022-10-18 15:47:20 UTC; 48s ago
   Main PID: 78206 (bundle)
      Tasks: 32 (limit: 2242)
     Memory: 242.5M
        CPU: 10.113s
     CGroup: /system.slice/mastodon-sidekiq.service
             ??78206 "sidekiq 6.4.2 live [0 of 25 busy]" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" >

Oct 18 15:47:35 ubuntu2204 bundle[78206]: 2022-10-18T15:47:35.747Z pid=78206 tid=1mle INFO: Scheduling accounts_statuses_cleanup_scheduler {">
Oct 18 15:47:35 ubuntu2204 bundle[78206]: 2022-10-18T15:47:35.748Z pid=78206 tid=1mle INFO: Schedules Loaded

Access Mastodon Web Interface

Now, open your web browser and type the URL https://mastodon.example.com. You will be redirected to the following page:

<img alt="Mastodon" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/10/echo/page1.png6352ea30bb5b4.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="398" loading="lazy" src="data:image/svg xml,” width=”750″>

Now, provide your admin email and password which you created earlier, and click on the Log in button. You should see the following page:

<img alt="Mastodon – First Steps" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/10/echo/page2.png6352ea31ec8b7.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="392" loading="lazy" src="data:image/svg xml,” width=”750″>

Now, click on the LET’S GO! button. You should see the following page:

<img alt="Home – Local – Federated" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/10/echo/page4.png6352ea33acfaa.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="385" loading="lazy" src="data:image/svg xml,” width=”750″>

Now, click on the Next button. You should see the following page:

<img alt="Mastodon Features" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/10/echo/page41.png6352ea3548117.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="390" loading="lazy" src="data:image/svg xml,” width=”750″>

Now, click on the FINISH TUTORIAL! button. You should see the Mastodon dashboard on the following page:

<img alt="Mastodon Dashboard" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/10/echo/page5.png6352ea36658ef.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="394" loading="lazy" src="data:image/svg xml,” width=”750″>

Conclusion

Congratulations! you have successfully installed and configured Mastodon on Ubuntu 22.04 server. You can now easily create your own social media network using Mastodon. Feel free to ask me if you have any questions.