title: "Install Mastodon without Docker (Ubuntu 20.04)"
description: "Snippet for running Mastodon instance (Twitter alternative) on Ubuntu 20.04 from source."
# linkTitle:
date: 2022-12-10T04:35:14+07:00
lastmod:
draft: false
noindex: false
# comments: false
nav_weight: 1000
# nav_icon:
# vendor: bootstrap
# name: toggles
# color: '#e24d0e'
series:
# - Tutorial
categories:
- SysAdmin
- Self-Hosted
tags:
- Mastodon
- PostgreSQL
- Ruby
- Nginx
images:
# menu:
# main:
# weight: 100
# params:
# icon:
# vendor: bs
# name: book
# color: '#e24d0e'
authors:
- ditatompel
---
**Mastodon** is free and *open-source* software for running *self-hosted* social networking services. It has microblogging features similar to the **Twitter** service, which are offered by a large number of independently run nodes, known as **instances**, each with its own code of conduct, terms of service, privacy policy, privacy options, and moderation policies.
<!--more-->
This article is my personal snippet for running Mastodon instance on Ubuntu 20.04 from source. I use *self-signed* certificate instead of **cerbot** because the instance will be run behind **Cloudflare** reverse proxy.
Before starting, there are several things that need to be fulfilled:
- Fresh Ubuntu `20.04` server with root access.
- Domain name (or sub-domain) for the Mastodon instance (in this case I'm using `vr4.me` and the Mastodon instance will be accessed from `https://social.vr4.me`.
- SMTP Server for email delivery service.
## Preparing the system
First, make sure the Ubuntu server we're using is up to date.
```shell
apt update && apt upgrade
```
Install `curl`, `wget`, `gnupg`, `apt-transport-https`, `lsb-release` and `ca-certificates`:
Enable **NodeJS**`corepack` feature and set **Yarn** version to `classic`:
```shell
corepack enable
yarn set version classic
```
Install **Ruby** with `rbenv`.
> _Note that `rbenv` must be installed for a **single Linux user**, therefore, first we must create linux user where Mastodon services will be running as (we'll create `mastodon` user):_
```shell
adduser --disabled-login mastodon
```
Then switch to mastodon user:
```shell
su - mastodon
```
And as `mastodon` user, proceed to install `rbenv` and `rbenv-build`:
For optimal performance, you may use [pgTune](https://pgtune.leopard.in.ua/) to generate an appropriate configuration and edit values in `/etc/postgresql/15/main/postgresql.conf` before restarting PostgreSQL with `systemctl restart postgresql`.
Now, create a PostgreSQL user that Mastodon service could use. The easiest way is with `ident` authentication so the PostgreSQL user does not have a separate password and can be used by the Linux user with the same username.
Open PostgreSQL prompt:
```shell
sudo -u postgres psql
```
In the `psql` prompt, execute:
```sql
CREATE USER mastodon CREATEDB;
\q
```
The core system requirement is now ready, now we can move forward to setting up Mastodon instance along with it's dependency.
## Setting up Mastodon
It is time to download the Mastodon code. Switch to the `mastodon` user:
```shell
su - mastodon
```
Use `git` to download the latest `stable` release of Mastodon:
```shell
git clone https://github.com/mastodon/mastodon.git live && cd live
The configuration file is saved as `.env.production`.
In my case, I want Mastodon instance can be accessed from `https://social.vr4.me` but use my main domain identity to serve `@ditatompel@vr4.me` (instead of `@ditatompel@social.vr4.me`) So I need to change `LOCAL_DOMAIN` configuration value to `vr4.me` and add `WEB_DOMAIN=social.vr4.me` in the `.env.production` file configuration. Please refer to [documentation on configuration](https://docs.joinmastodon.org/admin/config/) for more detailed information.
When done with the configuration we can switch back to `root` user:
```shell
exit
```
## Setting up Nginx
Copy the Nginx configuration template that comes from Mastodon repository:
Then edit `/etc/nginx/sites-available/mastodon` and replace `example.com` to our domain name (in my case `social.vr4.me`).
As I said before, the instance will be serve behind **Cloudflare** reverse proxy and I don't want to use **certbot** to issue my SSL certificate. So I use my own *self-signed* certificate.
If you want to use certbot to issue the SSL certificate:
```shell
systemctl reload nginx
## Replace social.vr4.me with your domain name.
certbot --nginx -d social.vr4.me
```
## Setting up Mastodon systemd services
Copy the `systemd` service templates from the Mastodon directory to `/etc/system/systemd` directory:
Wait for a few minutes and try to access your instance from your web browser, and enjoy your *self-hosted* Twitter alternative!
If you want to join my instance, please do so : [https://social.vr4.me/invite/G2BtoAfD](https://social.vr4.me/invite/G2BtoAfD). The data stored somewhere in Indonesia.