Table of Contents

Installing Mastodon from source on Debian 11 Bullseye

Instructional guide on creating your own Mastodon-powered website, based on the official Ubuntu setup guide of Mastodon 1) that won't work for Debian 11 (Bullseye) for a number of reasons 2)3)4).

Using this guide and copy & pasting the commands should give you a working Mastodon instance in less than 60 minutes.

Pre-requisites

You will be running the commands as root. If you aren’t already root, switch to root:

System packages

apt update
apt install -y  \
imagemagick ffmpeg libpq-dev libxml2-dev libxslt1-dev file git-core  
g++ libprotobuf-dev protobuf-compiler pkg-config nodejs gcc autoconf  
bison build-essential libssl-dev libyaml-dev libreadline6-dev  
zlib1g-dev libncurses5-dev libffi-dev libgdbm-dev curl sudo  
nginx redis-server redis-tools postgresql postgresql-contrib  
certbot python3-certbot-nginx yarn libidn11-dev libicu-dev libjemalloc-dev

System repositories

Node.js

curl -sL https://deb.nodesource.com/setup_12.x | bash -  
apt-get install -y nodejs=12*

Yarn

curl -sS 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  
apt-get update && apt-get install -y yarn  

Installing Ruby

We will be using rbenv to manage Ruby versions, because it’s easier to get the right versions and to update once a newer release comes out. rbenv must be installed for a single Linux user, therefore, first we must create the user Mastodon will be running as:

adduser --disabled-login mastodon  

We can then switch to the user:

su - mastodon  

And proceed to install rbenv and rbenv-build:

git clone https://github.com/rbenv/rbenv.git ~/.rbenv  
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 this is done, we can install the correct Ruby version:

RUBY_CONFIGURE_OPTS=--with-jemalloc rbenv install 2.7.2  
rbenv global 2.7.2  

We’ll also need to install bundler:

gem install bundler --no-document  

Return to the root user:

exit  

Setup

Setting up PostgreSQL

Performance configuration (optional)

For optimal performance, you may use pgTune to generate an appropriate configuration and edit values in /etc/postgresql/9.6/main/postgresql.conf before restarting PostgreSQL with systemctl restart postgresql

Creating a user

You will need to create a PostgreSQL user that Mastodon could use. It is easiest to go with “ident” authentication in a simple setup, i.e. the PostgreSQL user does not have a separate password and can be used by the Linux user with the same username.

Open the prompt:

sudo -u postgres psql  

In the prompt, execute:

CREATE USER mastodon CREATEDB;  \\
\q  

Done!

Setting up Mastodon

It is time to download the Mastodon code. Switch to the mastodon user:

su - mastodon  

Checking out the code

Use git to download the latest stable release of Mastodon:

git clone https://github.com/tootsuite/mastodon.git live && cd live  
git checkout $(git tag -l | grep -v 'rc[0-9]*$' | sort -V | tail -n 1)  

Installing the last dependencies

Now to install Ruby and JavaScript dependencies:

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

The two bundle config commands are only needed the first time you're installing dependencies. If you're going to be updating or re-installing dependencies later, just bundle install will be enough.

Generating a configuration

Run the interactive setup wizard:

LD_PRELOAD=libjemalloc.so RAILS_ENV=production bundle exec rake mastodon:setup  

This will:

If you're going to use Mastodons' single-user mode, you can choose your final handle (which will later display as “example.com/@handle” for the admin account instead of the pre-selected admin handle.

The configuration file is saved as

.env.production

You can review and edit it to your liking. Refer to Mastodons' documentation on configuration.

You’re done with the mastodon user for now, so switch back to root:

exit  

Setting up nginx

Copy the configuration template for nginx from the Mastodon directory:

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

Then edit /etc/nginx/sites-available/mastodon to replace example.com with your own domain name. If you're using VIM you can do that like so:

:%s/example.com/mydomain.com/g  

Make any other adjustments you might need.

Acquiring a SSL certificate

We’ll use Let’s Encrypt to get a free SSL certificate:

certbot --nginx -d example.com  

This will obtain the certificate, automatically update /etc/nginx/sites-available/mastodon to use the new certificate, and reload nginx for the changes to take effect.

At this point you should be able to visit your domain in the browser and see the elephant hitting the computer screen error page. This is because we haven’t started the Mastodon process yet.

Setting up systemd services

Copy the systemd service templates from the Mastodon directory

cp /home/mastodon/live/dist/mastodon-*.service /etc/systemd/system/  

Change /etc/systemd/system/mastodon-sidekiq.service to preload libjemalloc.so

$EDITOR /etc/systemd/system/mastodon-sidekiq.service  

Below the line containing

Environment="MALLOC_ARENA_MAX=2"  

enter

Environment="LD_PRELOAD=libjemalloc.so"  

Change /etc/systemd/system/mastodon-web.service to preload libjemalloc.so

$EDITOR /etc/systemd/system/mastodon-web.service  

Below the line containing

Environment="PORT=3000"  

enter

Environment="LD_PRELOAD=libjemalloc.so"  

If you deviated from the defaults at any point, check that the username and paths are correct

$EDITOR /etc/systemd/system/mastodon-*.service  

Start and enable the new systemd services

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

They will now automatically start at boot.

Hooray! You should now have a working Mastodon server

To Investigate

yarn install --pure-lockfile yields a warning

warning " > react-redux-loading-bar@4.0.8" has incorrect peer dependency "react-redux@^3.0.0 || ^4.0.0 || ^5.0.0".

It doesn't seem to have an impact, though.

License, Source

This article is based on https://docs.joinmastodon.org/admin/install/ by https://joinmastodon.org/ and is licensed under CC BY-SA 4.0.