How to download and install Mastodon on an Ubuntu server

October 2023

Decentralized social networking platforms like Mastodon have gained popularity in recent years. Although similar to Twitter, this open-source platform is not hosted in one place. Each individual Mastodon dedicated server is an access point to a different community, also called an “instance”. With this tutorial, we are going to install Mastodon on Ubuntu, a Linux distribution

If you plan to host a Mastodon dedicated server and create a community of your own, you will need to install Mastodon and also register a domain to get started. Mastodon is mainly written in Ruby and Javascript, which is what you will see in the steps below. This social platform offers some exciting features and is designed to provide a more user-controlled and community-driven experience. Let’s get started!

Install Mastodon on Ubuntu

Let’s dive into the steps to download and install Mastodon on an Ubuntu server.

Installing Dependencies

Our first step is installing dependencies, but before executing this step, it is highly recommended that you update your system to the latest version of Ubuntu. To do this you can run the below command. 

sudo apt-get update -y
sudo apt-get upgrade -y

Once you’re done with the update, restart your system and login as a sudo user.

Now it’s time to install the required dependencies for Mastodon to your Ubuntu server. Follow the below command.

sudo curl -sL https://deb.nodesource.com/setup_4.x | sudo bash –
sudo apt-get install imagemagick ffmpeg libpq-dev libxml2-dev libxslt1-dev nodejs -y
sudo npm install -g yarn

After installing the dependencies for Mastodon successfully, It’s time to install the Redis server in your system. Use the following command to install.

sudo apt-get install redis-server redis-tools -y

Once this is done, let’s move on to the next step.

Installing and Configuring PostgreSQL

Mastodon uses a PostgreSQL database. So, it’s essential to install PostgreSQL on your server. The following command will install PostgreSQL in your system.

sudo apt-get install postgresql pidentd -y

Once the installation is done, you need to start the ident daemon using the following command.

sudo systemctl start pidentd

After that you can create a user for Mastodon on PostgreSQL.

sudo su – postgres
psql
CREATE USER mastodon CREATEDB;
exit

The following command will allow users to log in without requiring a password.

sudo sed -i ‘/^local.*postgres.*peer$/a host all all 127.0.0.1/32 ident’ /etc/postgresql/9.?/main/pg_hba.conf

When you’re done with installing and configuring PostgreSQL, restart it for the changes to take effect.

sudo systemctl postgresql restart

Installing Ruby

As Mastodon is a Ruby-based application, you also need to install Ruby with the required dependencies onto your dedicated server.

sudo apt-get install autoconf bison build-essential libssl-dev libyaml-dev libncurses5-dev libffi-dev libgdbm3 libreadline6-dev zlib1g-dev libgdbm-dev -y

After all packages are successfully installed, it’s time to create a user for Mastodon without a password. Let’s call this user “mastodon”.

sudo adduser –disabled-password –disabled-login mastodon

Next, login with the Mastodon user that we created recently and install the rbenv and rbenv-build by following the below command.

su – mastodon
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo ‘export PATH=”$HOME/.rbenv/bin:$PATH”‘ >> ~/.bashrc
echo ‘eval “$(rbenv init -)”‘ >> ~/.bashrc
source ~/.bashrc

To apply the bash modification you made, logout from the Mastodon user and login again.

exit
su – mastodon
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build

Lastly, install Ruby for Mastodon using the following command.

rbenv install 2.4.1
rbenv global 2.4.1

Installing Mastodon

Now you’ve installed all required dependencies to make your server ready for running Mastodon.

It’s time to install Mastodon in your system. For this, we are going to download the latest version of Mastodon from the Git repository by using the following lines of command.

git clone https://github.com/tootsuite/mastodon.git live
cd live
git checkout $(git tag | tail -n 1)

Now install a bundler to manage the dependencies and disable the gem documentation:

echo “gem: –no-document” > ~/.gemrc
gem install bundler –no-ri

Install Mastodon with the below command

bundle install –deployment –without development test
yarn install

Next create a configuration file for Mastodon

cp .env.production.sample .env.production
nano .env.production

Add the following codes in the configuration file

REDIS_HOST=localhost
REDIS_PORT=6379
DB_HOST=/var/run/postgresql
DB_USER=mastodon
DB_NAME=mastodon_production
DB_PASS=
DB_PORT=5432

# Federation
LOCAL_DOMAIN=yourdomain.com
LOCAL_HTTPS=true

Don’t forget to save the file when you’re done with it.

Creating the Mastodon Systemd File

In this step, we’re going to create systemd web service, background service, and API service for Mastodon.

Let’s start by creating the web service file,

nano /etc/systemd/system/mastodon-web.service

Add the following lines:

[Unit]
Description=mastodon-web
After=network.target

[Service]
Type=simple
User=mastodon
WorkingDirectory=/home/mastodon/live
Environment=“RAILS_ENV=production”
Environment=“PORT=3000”
ExecStart=/home/mastodon/.rbenv/shims/bundle exec puma -C config/puma.rb
TimeoutSec=15
Restart=always

[Install]
WantedBy=multi-user.target

Next, create the Background service file

nano /etc/systemd/system/mastodon-sidekiq.service

Add the following lines:

[Unit]
Description=mastodon-sidekiq
After=network.target

[Service]
Type=simple
User=mastodon
WorkingDirectory=/home/mastodon/live
Environment=“RAILS_ENV=production”
Environment=“DB_POOL=5”
ExecStart=/home/mastodon/.rbenv/shims/bundle exec sidekiq -c 5 -q default -q mailers -q pull -q push
TimeoutSec=15
Restart=always

[Install]
WantedBy=multi-user.target

Lastly, create the API service file:

nano /etc/systemd/system/mastodon-streaming.service

Add the following lines for this file:

[Unit]
Description=mastodon-streaming
After=network.target

[Service]
Type=simple
User=mastodon
WorkingDirectory=/home/mastodon/live
Environment=“NODE_ENV=production”
Environment=“PORT=4000”
ExecStart=/usr/bin/npm run start
TimeoutSec=15
Restart=always

[Install]
WantedBy=multi-user.target

Now enable the service files that we created by executing the following command,

sudo systemctl enable /etc/systemd/system/mastodon-*.service
sudo systemctl enable /etc/systemd/system//etc/systemd/system//etc/systemd/system/mastodon-web.service
sudo systemctl enable /etc/systemd/system//etc/systemd/system/mastodon-streaming.service

At the end, start the Mastodon instance:

sudo systemctl start mastodon-web.service
sudo systemctl start mastodon-sidekiq.service
sudo systemctl start mastodon-streaming.service

Configuring Nginx for Mastodon

This is our last step. Here you need to set up a reverse-proxy using Nginx to access Mastodon with your domain.

Let’s begin by installing Nginx with the following command:

sudo apt-get install nginx -y

Then create a Nginx virtual server block for Mastodon.

sudo nano /etc/nginx/sites-enabled/mastodon.conf

Include the following lines,

map $http_upgrade $connection_upgrade {
default upgrade;
close;
}
server {
listen 80;
listen [::]:80;
server_name www.yourdomain.com yourdomain.com;
return 301 https://votredomaine.com$request_uri;

access_log /dev/null;
error_log /dev/null;
}

server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name www.yourdomain.com yourdomain.com;

access_log /var/log/nginx/yourdomain.com-access.log;
error_log /var/log/nginx/yourdomain.com-error.log;

ssl_certificate /etc/letsencrypt/live/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/privkey.pem;
ssl_protocols TLSv1.2;
ssl_ciphers EECDH+AESGCM:EECDH+AES;
ssl_prefer_server_ciphers on;
add_header Strict-Transport-Security “max-age=15552000; preload”;

keepalive_timeout 70;
sendfile on;
client_max_body_size 0;
gzip off;

root /home/mastodon/live/public;

location / {
try_files $uri @proxy;
}

location @proxy {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_pass_header Server;
proxy_pass http://127.0.0.1:3000;
proxy_buffering off;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
tcp_nodelay on;
}

location /api/v1/streaming {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_pass http://127.0.0.1:4000;
proxy_buffering off;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
tcp_nodelay on;
}

error_page 500 501 502 503 504 /500.html;
}

Now restart Nginx,

systemctl restart nginx

Finally, you’ve completed all the steps to download and install Mastodon on your server. You can now open your web browser and type your domain URL to access your Mastodon install.

For more system administration guides, such as backing up your server or creating an API, visit Mastodon’s website.

Contact Us

If you’re looking for a dedicated server to host your Mastodon instance, we’re here to help. ReliableSite offers a wide range of dedicated hosting options to meet your needs and budget. Choose from our servers in three prime locations: New York City metro area, Miami, FL, and Los Angeles, CA. Get in touch with the ReliableSite team to get started!

See a mistake in the code? Send an email to marketing @ reliablesite.net.