How to Intall a Mastodon server on Digital Ocean

I should preface this by telling you all this is NOT a beginner tutorial. You are expected to have a basic understanding of:

  • linux
  • command line
  • ruby (not much)
  • Servers
  • Databases


Installing Mastodon in Ubuntu 20.04


Server requirements:

Minimum Hardware Requirements:

  • 2GB RAM
  • 2vCPU

Software Requirements

  • PostgreSQL 9.5+
  • Redis 4+
  • Ruby 2.6+
  • Node.js 14+
  • Domain - to link to you Mastodon instance
  • SMTP Service - useful for sending outbound emails

Point your Domain to your Mastodon Instance

  1. Log in to your Domain Server Provider
  2. Find your domain and go to your DNS Zone Editor.
  3. Add a DNS record (A-record) to point to your Mastodon instance. For example: a. If you want to use your root domain, i.e., add an A-record and set the IP-address of your Mastodon instance as the value for this record b. Or if you want to use a subdomain, i.e., add an A-record and still set the value to be the IP-address of your Mastodon instance.

Before proceeding to the next section, make sure to connect to your Mastodon Instance via SSH.

Install NodeJS

Because Mastodon uses Yarn package manager, we will need to install that as well.

Install Ruby

  • Run: sudo apt install ruby ruby-dev

Make sure to check your ruby version by running:

ruby -v

Setup the PostgreSQL Database Server

If you have not run an apt update command from the previous section, please do so by running:

sudo apt update sudo apt install postgresql postgresql-contrib

By default, the database server will start and listen on, you can confirm that by running:

sudo ss -lnpt | grep postgres

You should get an output after running the command. For some reason no output shows up, it means your postgresql database server is not running. Run this command to start it:

sudo systemctl start postgresql

Log in to the PostgreSQL console by logging in as postgres, the superuser.

  • Run: sudo -u postgres -i psql

  • Create the database, run: CREATE DATABASE mastodon;

  • Create a database user, run: CREATE USER mastodon;

  • Set your database user password, run: ALTER USER mastodon WITH ENCRYPTED PASSWORD 'your_password_here';

  • Make sure to replace it with your own password Grant user to create a database, run: ALTER USER mastodon CREATEDB;

  • Assign the user as the owner of the database, run: ALTER DATABASE mastodon OWNER TO mastodon;

  • Exit the PostgreSQL console by entering: \q

Configure Mastodon

We need a new system user to run the Mastodon server.

Run this command: sudo adduser mastodon --system --group --disabled-login

We will store the Mastodon code in /var/www/, so we will need to create that directory. Run: sudo mkdir -p /var/www

Then let’s move the Mastodon code to that directory. Run: sudo mv mastodon/ /var/www/

Update and change the ownership of that project directory.

sudo chown -R mastodon:mastodon /var/www/mastodon/

Go to your mastodon directory by changing directory:

cd /var/www/mastodon/

Checkout to the latest stable release of Mastodon

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

Install the gem bundler, sudo gem install bundler Install required packages:

sudo apt install redis-server optipng pngquant jhead jpegoptim gifsicle nodejs 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

Install Mastodon packages:

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

Setup Mastodon:

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

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

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

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: PostgreSQL port: 5432 Name of PostgreSQL database: mastodon Name of PostgreSQL user: mastodon Password of PostgreSQL user: Database configuration works!

Redis host: 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? no E-mail address to send e-mails "from": Mastodon <>

Send a test e-mail with this configuration right now? Yes

This configuration will be written to .env.production Save configuration? Yes Send test e-mail to:

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

Create Systemd Service for Mastodon

Run the following commands

sudo cp /var/www/mastodon/dist/mastodon-web.service /etc/systemd/system/ sudo cp /var/www/mastodon/dist/mastodon-sidekiq.service /etc/systemd/system/ sudo cp /var/www/mastodon/dist/mastodon-streaming.service /etc/systemd/system/

Update the services files:

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

sudo systemctl daemon-reload Start the mastodon services, run: sudo systemctl enable --now mastodon-web mastodon-sidekiq mastodon-streaming Make sure that they are running, run this command: sudo systemctl status mastodon-web mastodon-sidekiq mastodon-streaming

Nginx as the Reverse Proxy for Mastodon Install by running: sudo apt install nginx Mastodon has an nginx configuration template, create a copy from that and use it.

sudo cp /var/www/mastodon/dist/nginx.conf /etc/nginx/conf.d/mastodon.conf Update the config file by replacing it with your domain name, example:

Run: sed -i 's/' /etc/nginx/conf.d/mastodon.conf

Make sure to replace in the command above with your domain name

Open the config file and replace: root /home/mastodon/live/public; to: root /var/www/mastodon/public; Comment out lines starting with ssl_certificate and ssl_certificate_key

Add the following lines: ssl_certificate /etc/ssl/certs/ssl-cert-snakeoil.pem; ssl_certificate_key /etc/ssl/private/ssl-cert-snakeoil.key;

Save and close the config file

Test your configuration, run: sudo nginx -t If the previous command is successful, reload the config:

sudo systemctl reload nginx

Visit your Mastodon installation in your browser through your registered domain, i.e.:

Adding SSL Cert using Let’s Encrypt

Install certbot, run: sudo apt install certbot python3-certbot-nginx Run the command to obtain certificate: sudo certbot --nginx --agree-tos --redirect --hsts --staple-ocsp --email -d

You will get a Congratulations! message on success. Otherwise, it’ll tell you about the problem. Visit your Mastodon install via the https url, i.e.: