Installing Mastodon 4.3 on a Raspberry Pi

Although the official Mastodon installation docs are quite thorough, I did find a few quirks when installing on a Raspberry Pi (both 4B and 5). I've brought together various guides that I used (sources are cited) into one set of instructions which have repeatedly worked for me.


Step 1. Increase the swap file

If you're using a 4GB Raspberry Pi (4B or 5), you will need to increase the size of the swap file, at least temporarily, otherwise the process (in Step 9) to compile CSS/JS assets may crash. Obviously, the 1GB and 2GB models should not be used here as they lack sufficient memory.

You can set the swap file back to its original size—once Mastodon is installed—by following these steps again, but this is not necessary.

⚠️  If you're using a Raspberry Pi (4B or 5) with 8GB of RAM, you can safely skip this and move straight on to Step 2.
  1. Stop the system from using the current swap file:

    sudo dphys-swapfile swapoff
  2. Open the swap file configuration file:

    sudo nano /etc/dphys-swapfile
  3. Find the following line of text within the file (value may vary):

    CONF_SWAPSIZE=200

    and change the value to 2048 (MB):

    CONF_SWAPSIZE=2048
  4. Save the file by pressing CTRL + X, followed by Y, then Enter.

  5. Re-initialise the swap file (removes the original swap file and recreates it to fit the newly defined size):

    sudo dphys-swapfile setup
  6. Turn the swap back on:

    sudo dphys-swapfile swapon
  7. Reboot the device to allow all programs to be reloaded with access to the new memory pool:

    sudo reboot


Step 2. SSH Keys

Using SSH keys for authentication instead of passwords is a great way of securing the Raspberry Pi, as well as making logins much quicker.

  1. Find your public SSH key on your computer. On macOS, this will be a file ending .pub in the ~/.ssh directory.

  2. Create a .ssh directory in your home directory on the Raspberry Pi:

    install -d -m 700 ~/.ssh
  3. Create (and edit) an authorized_keys file in the new directory:

    nano ~/.ssh/authorized_keys
  4. Paste the contents of your public SSH key into this file, then save.

  5. Ensure the file has the correct permissions:

    sudo chmod 644 ~/.ssh/authorized_keys
  6. Ensure the file has the correct owner (if not using pi as the default user, replace it below with the name of your default user):

    sudo chown pi:pi ~/.ssh/authorized_keys


Step 3. NGINX web server

We want the ability to host other websites on this server so we will use NGINX as a reverse proxy for Mastodon.

  1. Update the package list and upgrade existing packages:

    sudo apt update && sudo apt upgrade
  2. Install NGINX:

    sudo apt install -y nginx
  3. Start the NGINX service:

    sudo systemctl start nginx
  4. Install PHP-FPM and recommended PHP modules:

    sudo apt install -y php8.2-fpm php8.2-mbstring php8.2-mysql php8.2-curl php8.2-gd php8.2-curl php8.2-zip php8.2-xml


Step 4. SSL certificates

I am using Cloudflare for DNS and tunnelling so I need to follow these steps to create and deploy an Origin CA certificate.

⚠️  If you don't use Cloudflare, install your own SSL certificate here instead of following the exact instructions below.
  1. Log in to the Cloudflare dashboard and select an account.

  2. Choose your domain.

  3. Go to SSL/TLS > Origin Server.

  4. Select Create Certificate.

  5. Choose Generate private key and CSR with Cloudflare and leave all other options as they are. Click the Create button.

  6. Ensure the Key Format is PEM and then copy the signed Origin Certificate and Private Key into separate .pem and .key files. Click the OK button.

  7. Copy the .pem and .key files to your Raspberry Pi in the /etc/ssl directory.

  8. Change the owner of both files to root:

    sudo chown root:root /etc/ssl/filename.key /etc/ssl/filename.pem
  9. Restart NGINX:

    sudo systemctl restart nginx
  10. Back in the Cloudflare dashboard, go to SSL/TLS > Overview and click Configure. Under the option Custom SSL/TLS click Select. Change the option to Full (Strict) and click Save.


Step 5. Mastodon pre-installation

  1. Update and upgrade system packages:

    sudo apt update && sudo apt upgrade
  2. Install dependencies:

    sudo apt install -y curl lsb-release 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 nginx redis-server redis-tools certbot python3-certbot-nginx libidn11-dev libicu-dev libjemalloc-dev
  3. Create a mastodon user:

    sudo useradd mastodon -m


Step 6. Install NodeJS

  1. Update the package list and upgrade existing packages:

    sudo apt update && sudo apt upgrade
  2. To ensure we have all of the packages we need to access the Nodesource repository, run the following command:

    sudo apt install -y ca-certificates curl gnupg
  3. Download the Nodesource GPG key and store it within the /usr/share/keyrings directory:

    curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /usr/share/keyrings/nodesource.gpg
  4. Set an environment variable that we will reference next:

    NODE_MAJOR=20
  5. Add the NodeJS repository to your sources list:

    echo "deb [signed-by=/usr/share/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list
  6. Since we made changes to the sources list, we need to update the package list:

    sudo apt update
  7. Install the NodeJS package:

    sudo apt install -y nodejs


Step 7. Install PostgreSQL (and enable Yarn)

  1. Save the GPG key for the PostgreSQL repository:

    curl -L https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor | sudo tee /usr/share/keyrings/postgresql-archive-keyring.gpg >/dev/null
  2. Add the repository to our sources list:

    echo "deb [arch=arm64 signed-by=/usr/share/keyrings/postgresql-archive-keyring.gpg] http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" | sudo tee  /etc/apt/sources.list.d/postgresql.list
  3. As we changed the available repositories, we need to update the package list cache again:

    sudo apt update
  4. Install the PostgreSQL package:

    sudo apt install -y postgresql postgresql-contrib
  5. Launch the PostgreSQL command line interface (CLI):

    sudo -u postgres psql
  6. Create a PostgreSQL user named mastodon:

    CREATE USER mastodon CREATEDB;
  7. Quit out of the CLI:

    \q
  8. To build and run Mastodon, we need to enable Yarn (the NodeJS package manager). Do this now:

    sudo corepack enable && sudo yarn set version classic


Step 8. Install Ruby

  1. Switch to the mastodon user:

    sudo su mastodon
  2. Change to the home directory:

    cd ~
  3. Clone the code repository for the rbenv utility:

    git clone https://github.com/rbenv/rbenv.git ~/.rbenv
  4. Clone the code repository for the ruby-build plugin:

    git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
  5. Adjust the .bashrc file to initialise the rbenv utility to set up the Ruby environment:

    echo 'eval "$(~/.rbenv/bin/rbenv init - bash)"' >> ~/.bashrc
  6. To ensure the changes we made are available immediately:

    exec bash
  7. Install Ruby 3.3.5:

    RUBY_CONFIGURE_OPTS=--with-jemalloc rbenv install 3.3.5
    ⚠️  This step can take a long time to complete.
  8. Declare the installed Ruby version 3.3.5 as the global version to use:

    rbenv global 3.3.5
  9. Use the Ruby package manager to install bundler:

    gem install bundler --no-document


Step 9. Install Mastodon

  1. Make sure that you are still running as the mastodon user; switch to that user now, if not:

    sudo su mastodon
  2. Change to the home directory:

    cd ~
  3. Clone the code repository for Mastodon, then change to the new live directory:

    git clone https://github.com/mastodon/mastodon.git live && cd live
  4. Change to the latest stable release of Mastodon:

    git checkout $(git tag -l | grep '^v[0-9.]*$' | sort -V | tail -n 1)
  5. Set the deployment option to true and exclude any packages that are only used for development or test purposes:

    bundle config deployment 'true' && bundle config without 'development test'
  6. Install all of the Ruby dependencies for Mastodon:

    bundle install -j$(nproc)

    Note: we use the -j option to tell the package manager to use as many CPU cores as we have.

  7. Install all of the NodeJS dependencies for Mastodon:

    yarn install
    ⚠️  You can safely ignore any warnings thrown here.
  8. Configure Mastodon:

    RAILS_ENV=production bundle exec rake mastodon:setup
  9. Follow the on-screen prompts for how you would like to set up Mastodon. You can leave the PostgreSQL and Redis options as default (just press Enter on each option).

  10. Switch back to your default user:

    exit
  11. Update the default NGINX host file using the template provided on the Mastodon docs website, or replace the file with a copy of a previously backed-up host file.

  12. Reload NGINX web server:

    sudo systemctl reload nginx
  13. Copy the systemd services for Mastodon to the relevant directory:

    sudo cp /home/mastodon/live/dist/mastodon-*.service /etc/systemd/system/
  14. Start the services now and enable them to start automatically at logon in future:

    sudo systemctl enable --now mastodon-web mastodon-sidekiq mastodon-streaming


Step 10. Post-installation customisation (optional)

Import a blocklist

  1. Download this blocklist.

  2. In Mastodon, go to Moderation > Federation and click on the Import button at the top of the page.

  3. Choose the blocklist file you previously downloaded and click Upload.

  4. You should now see a long list of domain blocks. Make sure the Select All checkbox is selected and then click on Import.

Change the maximum post length and poll limit

  1. Switch to the mastodon user:

    sudo su mastodon
  2. Change to the home directory:

    cd ~
  3. Edit the compose_form_container.js file:

    nano -w live/app/javascript/mastodon/features/compose/containers/compose_form_container.js
  4. Search for the line:

    maxChars: state.getIn(['server', 'server', 'configuration', 'statuses', 'max_characters'], 500)

    and change the value 500 to the new limit (e.g. 2000).

  5. Save the file by pressing CTRL + X, followed by Y, then Enter.

  6. Edit the status_length_validator.rb file:

    nano -w live/app/validators/status_length_validator.rb
  7. Search for the line:

    MAX_CHARS = 500

    and change the value 500 to the same new limit as above.

  8. Save the file by pressing CTRL + X, followed by Y, then Enter.

  9. Edit the poll_validator.rb file:

    nano -w live/app/validators/poll_validator.rb
  10. Search for the line:

    MAX_OPTIONS = 4

    and change the value 4 to the new limit (e.g. 12).

  11. Save the file by pressing CTRL + X, followed by Y, then Enter.

  12. Rebuild the assets:

    cd live && RAILS_ENV=production bundle exec rails assets:precompile
  13. Switch back to the main user:

    exit
  14. Restart the Mastodon services:

    sudo systemctl restart mastodon-sidekiq mastodon-web mastodon-streaming

Change the username, display name, and notes limits

  1. Switch to the mastodon user:

    sudo su mastodon
  2. Change to the home directory:

    cd ~
  3. Edit the account.rb file:

    nano -w live/app/models/account.rb
  4. Search for the following lines and amend as required:

    USERNAME_LENGTH_LIMIT = 30
    DISPLAY_NAME_LENGTH_LIMIT = 30
    NOTE_LENGTH_LIMIT = 500
  5. Save the file by pressing CTRL + X, followed by Y, then Enter.

  6. Restart the Mastodon services:

    sudo systemctl restart mastodon-sidekiq mastodon-web mastodon-streaming

1. https://pimylifeup.com/raspberry-pi-swap-file 2. https://pimylifeup.com/raspberry-pi-ssh-keys 3. https://pimylifeup.com/raspberry-pi-nginx 4. https://developers.cloudflare.com/ssl/origin-configuration/origin-ca
 5. https://www.digicert.com/kb/csr-ssl-installation/nginx-openssl.htm 6. https://pimylifeup.com/raspberry-pi-mastodon 7. https://pimylifeup.com/raspberry-pi-nodejs 8. https://github.com/sgrigson/oliphant/tree/main/blocklists/mastodon 9. https://fouquet.me/2024/10/10/mastodon-4-3-char-limit

Tags: #Mastodon #RaspberryPi