Update Mastodon server instance

You might have already noticed I use Mastodon for my main fediverse handle @gytis@mastodon.lt. Current version is 3.1.2.

While typically updating Mastodon is quite straightforward, sometimes you might need to iterate Ruby version on the server as well.

That was visible from error thrown:

rbenv: version `2.6.1' is not installed (set by live/.ruby-version)

So the full upgrade sequence is following:

Retrieve Mastodon source updates

Switch or login to user which is running Mastodon:

su - mastodon

And execute following:

cd ~/live
git fetch --tags
git checkout $(git tag -l | grep -v 'rc[0-9]*$' | sort -V | tail -n 1)

You might get error here:

error: Your local changes to the following files would be overwritten by checkout:
	.ruby-version
Please commit your changes or stash them before you switch branches.
Aborting

In that case reset git:

git reset --hard

And try checkout again.

Update Ruby

cd ~/.rbenv/plugins/ruby-build
git pull
cd ~/.rbenv
rbenv install 2.7.0

This was also documented in issue 638.

Update Mastodon

cd ~/live

Update Ruby dependencies

bundle install --deployment --without development test

Possible error

When recently updating mastodon.lt to Mastodon version 2.8.2 I got following error:

Warning: the running version of Bundler (1.17.2) is older than the version that created the lockfile (1.17.3). We suggest you upgrade to the latest version of Bundler by running `gem install bundler`.

Fix it by running:

cd ~/.rbenv
gem install bundler

And go back to previous folder:

cd ~/live

Update node.js dependencies

yarn install --pure-lockfile

When upgrading to version 3.1.1 got this warning about Yarn:

warning Your current version of Yarn is out of date. The latest version is "1.22.0", while you're on "1.21.1".
info To upgrade, run the following command:
$ sudo apt-get update && sudo apt-get install yarn

Interestingly, I also needed to fix Yarn repository key because of error:

W: An error occurred during the signature verification. The repository is not updated and the previous index files will be used. GPG error: https://dl.yarnpkg.com/debian stable InRelease: The following signatures were invalid: EXPKEYSIG 23E7166788B63E1E Yarn Packaging <yarn@dan.cx>
W: Failed to fetch https://dl.yarnpkg.com/debian/dists/stable/InRelease  The following signatures were invalid: EXPKEYSIG 23E7166788B63E1E Yarn Packaging <yarn@dan.cx>
W: Some index files failed to download. They have been ignored, or old ones used instead.

Fix it by running:

curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -

The minimum supported Node.js version has been bumped from 8 to 10. To upgrade Node.js, run:

curl -sL https://deb.nodesource.com/setup_10.x | bash -
apt install nodejs

Back to Mastodon user and install dependencies:

bundle install
yarn install

Database schema updates

RAILS_ENV=production bundle exec rails db:migrate

Upgrading to version 3.0.x

SKIP_POST_DEPLOYMENT_MIGRATIONS=true RAILS_ENV=production bundle exec rails db:migrate

Pre-compiling updated assets

RAILS_ENV=production bundle exec rails assets:precompile

Restart services

Mastodon runs in memory so you will need to restart it for any of the previous updates to take effect.

Switch to root or run in sudo:

systemctl restart mastodon-*.service

Or restart services separately:

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

Clear cache

RAILS_ENV=production bin/tootctl cache clear

Database migrations after upgrade to 3.0

Now that the new code is running, we can finish the database migrations. This will run the post-deployment ones:

RAILS_ENV=production bundle exec rails db:migrate

There we go – Mastodon 2.8.0 with polls functionality running fine 😊 Try out my poll about Go code retrieval 👍

Updated 2019-05-17: added possible error when updating to version 2.8.2. Updated 2019-06-21: added clear cache command after updating to version 2.9.0. Updated 2019-07-08: upgrading to version 2.9.2: changed ruby version from 2.6.1 to 2.6.3:

mastodon@server:~/.rbenv$ rbenv install 2.6.3
Downloading ruby-2.6.3.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.6/ruby-2.6.3.tar.bz2
Installing ruby-2.6.3...
Installed ruby-2.6.3 to /home/mastodon/.rbenv/versions/2.6.3

Updated 2019-09-01: upgrading to version 2.9.3 changed ruby version from 2.6.3 to 2.6.4:

mastodon@server:~/.rbenv$ rbenv install 2.6.4
Downloading ruby-2.6.4.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.6/ruby-2.6.4.tar.bz2
Installing ruby-2.6.4...
Installed ruby-2.6.4 to /home/mastodon/.rbenv/versions/2.6.4

Updated 2019-10-12: upgrading to version 3.0.1: updates Ruby version to 2.6.5:

mastodon@server:~/.rbenv$ rbenv install 2.6.5
Downloading ruby-2.6.5.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.6/ruby-2.6.5.tar.bz2
Installing ruby-2.6.5...
Installed ruby-2.6.5 to /home/mastodon/.rbenv/versions/2.6.5

Updated 2020-02-25: upgrading to version 3.1.1: Ruby version upgrade from 2.6.5 to 2.7.0. Yarn repository needed key update.

Updated 2020-03-31: Upgrade from 3.1.1 to 3.1.2 is usual, no surprises. Version of rbenv is the same – 2.7.0.

Tags: #mastodon #server #ruby