I’ve recently upgraded to Ubuntu 22.04 on my main machine and laptop. As I use a mixture of Rails versions for different apps I maintain, I needed to install Ruby 3.0, 2.7 & 2.6 alongside 3.1.

Because Ubuntu 22.04 comes with OpenSSL 3.0, Ruby 3.0 and below don’t support this and an older version is required for these versions to be installed.

Compiling OpenSSL

This guide assumes you’ve already got build-essential installed.

Firstly download OpenSSL 1.1.1g and extract it:

wget https://www.openssl.org/source/openssl-1.1.1g.tar.gz
tar zxvf openssl-1.1.1g.tar.gz

Once extracted, we need to configure where this version will be installed. In my case, I want it in a hidden folder in my home directory ~/.openssl/version

cd openssl-1.1.1g
./config --prefix=$HOME/.openssl/openssl-1.1.1g --openssldir=$HOME/.openssl/openssl-1.1.1g

make
make test

make install

As this doesn’t include any certificates, I just symlink the main OpenSSL certs folder into this version like so:

rm -rf ~/.openssl/openssl-1.1.1g/certs
ln -s /etc/ssl/certs ~/.openssl/openssl-1.1.1g/certs

Install Ruby

Once OpenSSL 1.1.1g is installed, we can now install Ruby 2.5 but telling ruby-build to use this version of OpenSSL. In this example, I’m using asdf but it should work the same way with rbenv.

# Ruby 3.0
RUBY_CONFIGURE_OPTS=--with-openssl-dir=$HOME/.openssl/openssl-1.1.1g asdf install ruby 3.0.4

# Ruby 2.7
RUBY_CONFIGURE_OPTS=--with-openssl-dir=$HOME/.openssl/openssl-1.1.1g asdf install ruby 2.7.6

# Ruby 2.6
RUBY_CONFIGURE_OPTS=--with-openssl-dir=$HOME/.openssl/openssl-1.1.1g asdf install ruby 2.6.9

# Ruby 2.5 (No Longer Supported)
RUBY_CONFIGURE_OPTS=--with-openssl-dir=$HOME/.openssl/openssl-1.1.1g asdf install ruby 2.5.9

# Ruby 2.4 (No Longer Supported)
RUBY_CONFIGURE_OPTS=--with-openssl-dir=$HOME/.openssl/openssl-1.1.1g asdf install ruby 2.4.10

And that’s it! You should now have older Ruby versions installed on Ubuntu 22.04.