How to Deploy a Mastodon Instance on Your VPS
Leave a comment on How to Deploy a Mastodon Instance on Your VPS
Mastodon has emerged as the leading decentralized, open-source alternative to conventional social networks. Powered by the ActivityPub protocol, it forms the backbone of the Fediverse, a federated network where distinct communities exchange updates seamlessly while retaining independent server rules and data ownership.

Running your own Mastodon instance provides total authority over your moderation policies, media storage, and user privacy. Because Mastodon requires background workers, in-memory caches, and database processes, a robust VPS is essential. This guide walks you through deploying an operational production instance of Mastodon on an Ubuntu VPS.
What is Mastodon?
Mastodon is an open-source, decentralized social networking platform that forms part of the Fediverse. Instead of relying on one central server, Mastodon allows anyone to host their own instance while still being able to communicate with users across other servers.
Requirements
Before you begin, ensure you have a VPS with at least the following specifications:
- 4 GB RAM (8 GB recommended for stable Sidekiq background processing)
- 2 vCPUs
- 40-60 GB disk space
- A registered domain name pointed to your server IP
Step 1: Update Your Server
Start by updating your server packages to ensure you have the latest security patches and repositories.
apt update && apt upgrade -y
Next, install the required dependencies for building and running the platform:
apt install git curl nano gnupg apt-transport-https build-essential -y
Step 2: Install PostgreSQL
PostgreSQL acts as the database backend for Mastodon.
apt install postgresql postgresql-contrib -y
Switch to the PostgreSQL shell to create a dedicated database and user:
su - postgres -c psql
Inside the PostgreSQL shell, run the following commands (replace ‘yourpassword’ with a strong password):
CREATE USER mastodon WITH PASSWORD 'yourpassword';
CREATE DATABASE mastodon_production OWNER mastodon;
\q
Step 3: Install Redis
Redis is required for caching and managing background job queues. Install it using the following command:
apt install redis-server -y
Enable and start the Redis service so it runs automatically:
systemctl enable redis-server
systemctl start redis-server
Step 4: Install Node.js and Yarn
Mastodon requires Node.js for building the frontend assets. Install it by pulling the setup script:
curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
apt install -y nodejs
Next, install and enable Yarn:
corepack enable
corepack prepare yarn@stable --activate
Step 5: Install Ruby (via rbenv)
Mastodon relies heavily on Ruby. Using an environment manager like rbenv is highly recommended. First, create a mastodon user:
adduser --disabled-login --gecos "" mastodon
su - mastodon
Run these commands to set up rbenv:
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/plugins/ruby-build
Once the environment manager is ready, install the required Ruby version and set it globally:
rbenv install 3.2.2
rbenv global 3.2.2
Step 6: Install Mastodon
While still logged in as the mastodon user, clone the official Mastodon source code repository:
git clone https://github.com/mastodon/mastodon.git ~/live
cd ~/live
Check out the latest stable release:
git checkout $(git tag | sort -V | tail -1)
Install the required Ruby gems:
bundle install
Install the JavaScript dependencies for the frontend:
yarn install --production
Step 7: Configure Mastodon
Create the environment configuration file from the provided sample:
cp .env.production.sample .env.production
Edit the configuration file to set up your specific instance details:
vi .env.production
Ensure you set values such as LOCAL_DOMAIN, PostgreSQL credentials, Redis URL, Email settings, and Media storage paths.
When you are done editing, run the database setup and asset precompilation:
RAILS_ENV=production bundle exec rails db:migrate
RAILS_ENV=production bundle exec rails assets:precompile
Exit back to the root user:
exit
Step 8: Configure Nginx and SSL
Install Nginx to serve your application to the web:
apt install nginx -y
Create a Mastodon site configuration under /etc/nginx/sites-available/mastodon (You can copy the provided template from the Mastodon directory). Once done, enable it by creating a symlink:
ln -s /etc/nginx/sites-available/mastodon /etc/nginx/sites-enabled/
Restart Nginx to apply the changes:
systemctl restart nginx
To enable HTTPS, install Certbot and generate your SSL certificate:
apt install certbot python3-certbot-nginx -y
certbot --nginx -d yourdomain.com
Step 9: Set Up Mastodon Services
Copy the systemd service files for the Web, Streaming, and Sidekiq workers from the Mastodon directory, then enable and start the services:
systemctl enable --now mastodon-web mastodon-sidekiq mastodon-streaming
Check the status to ensure everything is running smoothly:
systemctl status mastodon-web
Step 10: Access Your Instance
Once everything is running, open your browser and visit: https://yourdomain.com. A brand-new instance of Mastodon will be displayed. Create your admin account, log in, and begin configuring your server.
Conclusion
Running your own Mastodon instance on a VPS gives you full ownership and control over your online community. While the installation process involves several components working together, the result is a powerful, secure, and customizable social platform that operates independently and still connects seamlessly with the wider Fediverse. When you host Mastodon yourself, your data and rules remain in your control, regardless of whether you are creating a space for friends, an organization, or a public community. With proper setup and occasional maintenance, your server can grow into a stable and meaningful hub for conversation and connection on your own terms.
📺 Watch the Video Tutorial on RackNerdTV
Looking to host other social or community platforms on your VPS? Check out our tutorial on installing Flarum (a modern forum software) via the RackNerdTV YouTube channel:
At RackNerd, you can deploy a Linux VPS with the flexibility and root-level access needed to host your own Fediverse node and manage your environment seamlessly. If you prefer a hands-off approach for standard website hosting, you can also explore our shared web hosting plans.
👉 Visit https://racknerd.com to learn more about our hosting solutions.