How to Install Mastodon Social Network CMS on Ubuntu Linux

Share

This article describes steps one can take to install Mastodon social networking CMS on Ubuntu Linux.

Mastodon is free and open-source CMS software that let you create your social networking website. It is written in Ruby and JavaScript with support on multiple platforms.

Similar to Twitter and Facebook, Mastodon offers microblogging features that allow users to post messages, including images, follow others and create other content.

You can self-host Mastodon on your server in your environment. Your users can interact with your server via mobile devices, including Android, macOS and others.

How to Install Mastodon Social Network CMS on Ubuntu Linux

As described above, Mastodon is free and open-source CMS-based software that let you create your social networking portal. The app is written in Ruby and JavaScript with support on multiple platforms.

Below is how to install it on Ubuntu Linux.

Install dependencies

First, prepare and install package dependencies that will allow you to install and run Mastodon.

Run the commands below to update Ubuntu and install dependencies for Mastodon.

sudo apt update
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

Once you have installed the packages above, continue below.

Install Node.js and Yarn

Mastodon is written in JavaScript (Node.js). It requires Node.js to be installed. Run the commands below to install it.

Add the Node.js repository to Ubuntu by running the commands below.

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

After that, run the commands below to install Node.js.

sudo apt install nodejs

Additional Node.js resources can be found in the post below.

How to install Node.js on Ubuntu Linux

After installing Node.js, go and also install Yarn.

Run the commands below to add Yarn’s repository GPG key and file.

curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list

Finally, run the commands below to update the Ubuntu package index and install Yarn.

sudo apt update
sudo apt install yarn

Additional Yarn resources can be found in the post below:

How to install Yarn on Ubuntu Linux

Install PostgreSQL

Mastodon needs a database server to store its content. It uses the PostgreSQL database server for that.

Run the commands below to install PostgreSQL.

sudo apt install postgresql postgresql-contrib

After installing, log on to PostgreSQL database shell.

sudo su - postgres
psql

On the command shell, run the commands below to create a user named mastodon.

CREATE USER mastodon CREATEDB;

Exit twice.

exit
exit

Additional resources on PostgreSQL can be found in the post below.

How to install PostgreSQL on Ubuntu Linux.

Install Ruby

Again, Mastodon is developed in Ruby on Rails. It needs Ruby installed to function. We will create a system account to create an environment to run Ruby.

Run the commands below to create a system account named mastodon.

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

Next, switch to the account by typing the commands below.

sudo su - mastodon

Then create a Ruby environment.

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

Finally, install Ruby and update dependencies.

RUBY_CONFIGURE_OPTS=--with-jemalloc rbenv install 3.0.3
rbenv global 3.0.3
gem update --system
gem install bundler --no-document

Install Mastodon

At this point, your system should be ready for Mastodon. Run the commands below to switch to the Mastodon account.

sudo su - mastodon

The clone is the latest version 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)

Next, install all dependencies.

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

Set up Mastodon by running the commands below.

RAILS_ENV=production bundle exec rake mastodon:setup

Answer the questions to the prompts 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? No
SMTP server: smtp.mailgun.org
SMTP port: 587
SMTP username: 
SMTP password: 
SMTP authentication: plain
SMTP OpenSSL verify mode: none
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
Running `RAILS_ENV=production rails db:setup` ...
Created database 'mastodon'
Done!
The final step is compiling CSS/JS assets.
This may take a while and consume a lot of RAM.
Compile the assets now? Yes
Running `RAILS_ENV=production rails assets:precompile` ...

You’ll be prompted to create an admin account. Do it.

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@example.com
You can login with the password: 1c9e2ce773933bec6a71dccd9b889d0a
You can change your password once you login.

Exit when all is done above.

Create a Systemd Service for Mastodon

To efficiently manage the Mastodon service, you will want to create a systemd service for it. Run the commands below to copy pre-configured scripts to use.

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

Reload systemd daemon

sudo systemctl daemon-reload

Now you should be able to start all necessary Mastodon services.

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

Setup Nginx reverse proxy for Mastodon

Mastodon is SSL enabled by default. You must set up a SSL certificate to use with Mastodon. You can use Let’s Encrypt free SSL with Mastodon, and below is how to do it.

Run the commands below to install Certbot and Nginx HTTP server.

sudo add-apt-repository ppa:certbot/certbot
sudo apt update
sudo apt install certbot python3-certbot-nginx

For additional resources on installing and creating Let’s Encrypt SSL certificates for Nginx, read the post below.

How to set up Let’s Encrypt SSL certificate for Nginx on Ubuntu Linux

After installing, run the commands below to use Mastodon’s default Nginx server block config setting to create the Nginx server block.

sudo cp /home/mastodon/live/dist/nginx.conf /etc/nginx/sites-available/mastodon.conf
sudo sed -i 's/example.com/mastodon.example.com/g' /etc/nginx/sites-available/mastodon.conf
sudo ln -s /etc/nginx/sites-available/mastodon.conf /etc/nginx/sites-enabled/

If you can’t get the Let’s Encrypt SSL certificate on your localhost, simply use the default Ubuntu snakeoil certificate in the Nginx server block file at /etc/nginx/sites-available/mastodon.conf.

#Uncomment these lines once you acquire a certificate:

ssl_certificate     /etc/ssl/certs/ssl-cert-snakeoil.pem;
ssl_certificate_key /etc/ssl/private/ssl-cert-snakeoil.key;

Give Nginx permission to Mastodon’s directory and restart.

sudo chown -R mastodon:www-data /home/mastodon
sudo systemctl restart nginx

Once you’re done, Mastodon should be installed and ready.

Open your browser and browse to the domain name specified in the Nginx server block file.

https://mastodon.example.com
ubuntu linux install mastodon web portal
ubuntu Linux install mastodon web portal

Log in with the Mastodon account created above.

ubuntu linux mastodon user dashboard
ubuntu Linux mastodon user dashboard

That should do it!

Conclusion:

This post showed you how to install and use Mastodon social network CMS on Ubuntu Linux. If you find any error above or have something to add, please use the comment form below.