Getting Mastodon to Run on a Low-RAM VPS

I recently spent a couple hours configuring a Mastodon instance and thought I'd record some troubles I encountered and their solutions.

Thankfully, Mastodon installation and configuration is pretty easy. The installation manual is comprehensive and easy to follow (just make sure you do each step as stated, in the correct order).

The biggest problem I encountered was that the bundle exec rails assets:precompile step failed because my VM ran out of memory. I also had installed the latest node version, which seemed to cause some incompatibilities.

In order to get the right node version, we can run
sudo apt-get remove nodejs curl -sL https://deb.nodesource/com/setup_18.x | sudo -E bash - sudo apt-get install nodejs -y
Some GitHub messages suggested node18, which is a bit old now so you'll get a deprecation notice running the installer.

Some searching reveals that a common way to reduce RAM requirements during the build process is to reduce build-time parallelism and turn off some Terser Webpack features. This is done in config/webpack/product.js from the root mastodon directory. The patch may be found here.


diff --git a/config/webpack/production.js b/config/webpack/production.js
index cec810184..a5807830a 100644
--- a/config/webpack/production.js
+++ b/config/webpack/production.js
@@ -15,8 +15,9 @@ const sharedConfig = require('./shared');
  const root = resolve(__dirname, '..', '..');
           
  module.exports = merge(sharedConfig, {
+  parallelism: 1,
   mode: 'production',
-  devtool: 'source-map',
+  devtool: 'none',
   stats: 'normal',
   bail: true,
   optimization: {
@@ -24,8 +25,8 @@ module.exports = merge(sharedConfig, {
     minimizer: [
       new TerserPlugin({
         cache: true,
-        parallel: true,
-        sourceMap: true,
+        parallel: false,
+        sourceMap: false,
       }),
     ],
  },

During setup, I decided to try out Wasabi without realizing that public access is limited on trial/unpaid accounts. Deferring difficult storage decisions until later, I wanted to just use local storage instead. Thanks to this post by Ben Tasker, I was able to easily change the storage backend post-installation.

To resolve this, simply edit .env.product in your mastodon root directory, changing the line S3_ENABLED=true to S3_ENABLED=false. Of course this can be used to change from local to cloud storage after installation as well. Restart the mastodon services to ensure the changes take effect.