How to Install Mastodon on Ubuntu 24.04 or Debian 13
Mastodon documents its source install for Ubuntu 24.04 and Debian 13, not for CentOS successors. Here is the full setup with nginx, PostgreSQL and TLS.
Mastodon is a self-hosted social networking server that federates with other servers over ActivityPub. This guide installs the current stable Mastodon release (the 4.x series) from source, following the project's own installation documentation, with PostgreSQL, Redis, nginx and a Let's Encrypt certificate. At the end you will have a working instance at your own domain with an administrator account.
Mastodon's documentation is written for Ubuntu 24.04 LTS and Debian 13, and those are the two systems to use. CentOS 7 reached end of life in June 2024, and the project does not publish instructions for its successors (AlmaLinux 9, Rocky Linux 9, CentOS Stream 9); if you must stay on the RHEL family, run the official container images instead of a source install. Ubuntu 26.04 LTS is not yet named in the Mastodon documentation, so 24.04 remains the safe Ubuntu choice.
Requirements
- A server running Ubuntu 24.04 or Debian 13 with root access. Asset compilation during setup is memory-hungry, so add swap space if the server has little RAM.
- A domain or subdomain for the instance, for example
example.com, with DNS pointing to the server. The domain becomes part of every account name and cannot be changed later. - An SMTP server or email delivery service. Mastodon sends confirmation and password-reset emails.
- TCP ports 80 and 443 open in your firewall.
The commands in steps 1 to 3 and 6 to 8 run as root. Become root first:
sudo -iStep 1: Add the Node.js and PostgreSQL repositories
Mastodon needs newer Node.js and PostgreSQL versions than the distributions ship, so the documentation uses the NodeSource and PostgreSQL project repositories:
apt install -y curl wget gnupg lsb-release ca-certificates
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_24.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list
wget -O /usr/share/keyrings/postgresql.asc https://www.postgresql.org/media/keys/ACCC4CF8.asc
echo "deb [signed-by=/usr/share/keyrings/postgresql.asc] http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/postgresql.listStep 2: Install system packages
apt update
apt install -y \
imagemagick ffmpeg libvips-tools libpq-dev libxslt1-dev file git \
protobuf-compiler pkg-config autoconf bison build-essential \
libssl-dev libyaml-dev libreadline-dev zlib1g-dev libffi-dev \
libgdbm-dev nginx nodejs redis-server postgresql certbot \
python3-certbot-nginx libidn-dev libicu-dev libjemalloc-devEnable Corepack so that the Yarn version pinned by Mastodon is used, and create the unprivileged account that will run the application:
corepack enable
adduser --disabled-password mastodonStep 3: Create the PostgreSQL role
Mastodon connects through the local socket with ident authentication, so the role needs no password, only the right to create its database:
sudo -u postgres psql -c "CREATE USER mastodon CREATEDB;"On a busy instance, tune PostgreSQL afterwards (the Mastodon documentation suggests generating settings with pgTune) and restart it with systemctl restart postgresql.
Step 4: Check out Mastodon and install Ruby
Switch to the mastodon user:
su - mastodonClone the repository and check out the newest stable tag:
git clone https://github.com/mastodon/mastodon.git live && cd live
git checkout $(git tag -l | grep '^v[0-9.]*$' | sort -V | tail -n 1)Install rbenv and ruby-build, then build the Ruby version that the checked-out release pins in its .ruby-version file. Compiling Ruby takes several minutes:
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
source ~/.bashrc
git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build
RUBY_CONFIGURE_OPTS=--with-jemalloc rbenv installInstall the Ruby and JavaScript dependencies. You are still in /home/mastodon/live:
bundle config deployment 'true'
bundle config without 'development test'
bundle install
yarn installStep 5: Run the setup wizard
RAILS_ENV=production bin/rails mastodon:setupThe wizard asks for the domain, the database and Redis connection (the defaults match the packages installed above; leave the database password empty), optional object storage and your SMTP settings. It then writes .env.production, creates the database schema, precompiles the assets and offers to create an administrator account. Note the generated admin password. When it finishes, return to root:
exitStep 6: Get a TLS certificate
certbot certonly --nginx -d example.comThe certificate is stored in /etc/letsencrypt/live/example.com/.
Step 7: Configure nginx
Mastodon ships an nginx configuration template. Copy it into place and disable the default site:
cp /home/mastodon/live/dist/nginx.conf /etc/nginx/sites-available/mastodon
ln -s /etc/nginx/sites-available/mastodon /etc/nginx/sites-enabled/mastodon
rm /etc/nginx/sites-enabled/defaultEdit /etc/nginx/sites-available/mastodon:
- Replace every
example.comwith your domain. - Uncomment the
ssl_certificateandssl_certificate_keylines and make sure they point to the files Certbot created.
nginx must be able to enter the home directory to serve static files. Allow that, test the configuration and restart:
chmod o+x /home/mastodon
nginx -t
systemctl restart nginxOpening the site now shows Mastodon's error page, because the application itself is not running yet.
Step 8: Start the Mastodon services
Copy the systemd units shipped with the source and start them. If you used a different user name or path than in this guide, edit the unit files first.
cp /home/mastodon/live/dist/mastodon-*.service /etc/systemd/system/
systemctl daemon-reload
systemctl enable --now mastodon-web mastodon-sidekiq mastodon-streamingVerify the installation
Check that all three services are active:
systemctl status mastodon-web mastodon-sidekiq mastodon-streaming --no-pagerThen open https://example.com and sign in with the administrator email and password from the wizard. If a page fails to load, follow the logs while you reproduce the problem:
journalctl -u mastodon-web -u mastodon-sidekiq -u mastodon-streaming -fA user who registers but never receives the confirmation email points to wrong SMTP settings in /home/mastodon/live/.env.production. Fix them and restart mastodon-web and mastodon-sidekiq.
Maintenance
- Backups: in order of importance, dump the PostgreSQL database with
pg_dump, copy.env.production(it holds the application secrets), copy thepublic/systemdirectory with uploaded media if you store files locally, and copy the Redis dump/var/lib/redis/dump.rdb. - Media cache: remote media accumulates quickly. Remove cached copies older than seven days (the default; change it with
--days) by runningRAILS_ENV=production bin/tootctl media removeas themastodonuser from/home/mastodon/live, by hand or from that user's crontab. - Upgrades: every release on the project's GitHub releases page lists its own upgrade steps (new dependencies, migrations, asset compilation). Follow them in the order given; do not skip releases that require intermediate migrations.
- Certificates: the
certbotpackage installs a systemd timer that renews certificates. Because the certificate was requested withcertonly, place an executable script that runssystemctl reload nginxin/etc/letsencrypt/renewal-hooks/deploy/so that nginx loads renewed certificates.
The full reference is the Mastodon installation guide.
Frequently asked questions
Can I install Mastodon on AlmaLinux or Rocky Linux?
The project documents and tests the source installation on Ubuntu and Debian only. On RHEL-family servers the package names, service files and SELinux policy all need adapting by hand, so either use a Debian or Ubuntu server or deploy the official container images with Docker or Podman.
Can I change the domain after installation?
No. The domain is stored in every account identifier and known to every server you have federated with. Choose the final domain before running the setup wizard.
How do I create another administrator or reset a password?
Use tootctl as the mastodon user in /home/mastodon/live, for example RAILS_ENV=production bin/tootctl accounts modify alice --role Owner or RAILS_ENV=production bin/tootctl accounts modify alice --reset-password.
Do I need Elasticsearch?
No. Hashtag and account search work without it. Full-text search of posts is an optional feature that requires a separate Elasticsearch or OpenSearch service, described in the Mastodon documentation.