Setting up Ruby on WSL2 using Rbenv

4 minute read

This should be no different than on regular Ubuntu

The machine that I do most of my work on is my Lenovo Carbon X1 Laptop running PoP_OS!. But, because of reasons that I will not go into today, my main desktop runs Windows 11 and on that I run Windows Subsystem for Linux 2 (WSL2). WSL2 has a few Linux distros that you can run on it. For simplicities sake, I have gone with Ubunutu

Right now I am working with Jekyll to write and update a blog, and as such I want to be able to jump between machines when doing development. That means that they both need to have Ruby installed and be able to run the dev server.

As the distro is pretty much just a regular Ubuntu install, there should be no differences in getting this setup. The real issue is that I like to have my own references for the future, and I will know where this will be. No searching for a tutorial that may or may not work.

Enough of the idle chat. This is not a cooking blog with 16 pages about Autum days before getting to the meat of things.

Step By Step Guide On Installing Ruby

To manage Ruby versions we are going to use rbenv. There are other ruby version managers out there, so if you want to go with them, stop reading and find another tutorial.

Step 1. Installing Homebrew

Note: you do not have to install Homebrew. But, this is the recommended way of installing rbenv, and in the long run is easier to keep up to date.

To start with update your system. It is just good practice. Then, install Homebrew.

System update and Homebrew install

$ sudo apt update
$ sudo apt upgrade
$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

After the install completes, it is recommended to install build-essentials and gcc. Here are the docs for Homebrew-on-Linux.

$ sudo apt-get install build-essential
$ brew install gcc

Step 2. Install rbenv and a ruby-build

With Homebrew installed this part is super simple. The first step is to install rbenv and ruby-build. Ruby-build will build the ruby versions, and trust me, you want it installed. The rest of this is useless if you skip that step.

Install Command

$ brew install rbenv ruby-build
...
==> Caveats
==> ruby-build
ruby-build installs a non-Homebrew OpenSSL for each Ruby version installed and these are never upgraded.

To link Rubies to Homebrew's OpenSSL 1.1 (which is upgraded) add the following
to your ~/.profile:
  export RUBY_CONFIGURE_OPTS="--with-openssl-dir=$(brew --prefix openssl@1.1)"

Note: this may interfere with building old versions of Ruby (e.g <2.4) that use OpenSSL <1.1
$ 

The next step is to add rbenv to be part of the shell by default. For this to work we have to update the .profile of the account. You can do this by hand, or just add a single line that will take care of it every time that a shell is launched.

I am going update to use the Homebrew version of OpenSSL because I am using a more current version of Ruby. So, the next steps that need to be perfomed when using the bash shell is to add that command to the profile file. Now, I will always go in and clean this up. But this is the easy way to do it. Also, I will source .profile again so that you do not have to restart the shell.

Add ruby-build using Homebrew OpenSSL to Path

$ echo 'eval "$(rbenv init - bash)"' >> ~/.profile
$ echo 'export RUBY_CONFIGURE_OPTS="--with-openssl-dir=$(brew --prefix openssl@1.1)"' >> ~/.profile
$ . ~/.profile

At this point, you have everything installed that you need. To ensure that rbenv and ruby-build are installed, you can run the following commands.

Validate the install

$ rbenv global
system
$ ruby-build --version
ruby-build 20220726

Step 3. Listing and Installing Ruby versions

By installing rbenv via Homebrew, you should have a version of ruby installed. For me, this was version 3.1.2. Depending on when you read this, your version could be different. If the version installed works for you, skip the rest of this step and go to the next one on how to configure rbenv.

There are a few commands that are used to list installed versions, list available versions, and then install a Ruby version.

List versions of Ruby installed

$ rbenv versions
3.1.2

List stable versions of Ruby to install

$ rbenv install -l
2.6.10
2.7.6
3.0.4
3.1.2
jruby-9.3.6.0
mruby-3.1.0
picoruby-3.0.0
rbx-5.0
truffleruby-22.2.0
truffleruby+graalvm-22.2.0

Only latest stable releases for each Ruby implementation are shown.
Use 'rbenv install --list-all / -L' to show all local versions.

Install a version

$ rbenv install 3.0.4
Downloading ruby-3.0.4.tar.gz...
-> https://cache.ruby-lang.org/pub/ruby/3.0/ruby-3.0.4.tar.gz
Installing ruby-3.0.4...
ruby-build: using readline from homebrew
Installed ruby-3.0.4 to /home/username/.rbenv/versions/3.0.4

Step 4. Setting Ruby Version

This is the most vital part to using rbenv, being able to setup a global and local version of Ruby to use. Global means that it is available to the system in general. Local indicates that it belongs in a subdirectory, and when operating there, that version will be used.

This allows for gems to be configured on a project by project basis.

Setting global ruby version

$ rbenv versions
* 3.0.4 (set by /home/username/.rbenv/version)
  3.1.2
$ rbenv global 3.1.2
$ rbenv global
3.1.2
$ ruby --version
ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-linux]

Setting local ruby version

$ ruby --version
ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-linux]
$ rbenv local 3.0.4
$ ruby --version
ruby 3.0.4p208 (2022-04-12 revision 3fa771dded) [x86_64-linux]
$ cat .ruby-version
3.0.4

It is now as simple as that. When you move into that subdirectory, the version of Ruby will automatically switch to the version specified. This can be used in projects, and added to repos to ensure that the versions are the same accross systems.

Step 5. Done

Happy Coding