HOW TO INSTALL RUBY RBENV
Just like many other tools, we turn to the popular Homebrew macOS package manager. Run the following command in your terminal:
brew install rbenv ruby-build
Once everything is installed, we need to instruct rbenv to setup our shell, so we’re using the Ruby runtime version that we’ve customly installed with rbenv whenever we open a new terminal window to type commands in the prompt (as opposed to the preinstalled Ruby 2.6.8 version on the macOS).
To setup rbenv with your shell run the following command:
rbenv init
Follow the on-screen printed instructions if any extra configuration is required on your part. You might be asked to paste a command in your shell’s configuration. If you’re on a macOS and haven’t changed from the default Z Shell (ZSH), then you’ll want to make sure you add the following line of code to the top of your ~/.zshrc file:
eval "$(rbenv init - zsh)"
The rbenv install process is now complete. You might want to run some diagnostic checks to ensure that everything is set up correctly. If so, you can run the rbenv-doctor tool that the rbenv GitHub project provides:
Open a new terminal window so that you have the new rbenv environment loaded
Run the command:
curl -fsSL https://github.com/rbenv/rbenv-installer/raw/main/bin/rbenv-doctor | zsh
Warning: This command shouldn’t be taken lightly, as you are piping commands into your development environment’s shell that originate from a public and open source GitHub repository. It is possible that a malicious actor had tampered with the source code, so you are highly encouraged to review the code properly before you run this command.
How do you use rbenv to install a new Ruby version?
Congrats on building your own Ruby virtual environment! Now that you have a functional rbenv setup we can use it to completely manage your Ruby versions.
To begin, you want to list which Ruby versions are available for installation so you can choose the latest Ruby release, or perhaps a specific version that you’re looking for. To use rbenv to list all Ruby versions, run the following command:
rbenv install -l
The output of which was:
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.1.0
truffleruby+graalvm-22.1.0
Only latest stable releases for each Ruby implementation are shown.
Use 'rbenv install --list-all / -L' to show all local versions.
You can then proceed to installing one of these Ruby versions. Let’s use rbenv to install a Ruby version of 3.1.2:
rbenv install 3.1.2
The command will then start fetching the relevant source code files to extract, compile, and then install the Ruby 3.1.2 runtime version available to your shell.
When you’ve finished installing it, you should set it to be the chosen version anytime your shell starts. To do that, you need to run:
rbenv global 3.1.2
Finally, you can verify that this is working as expected by running the ruby --version in your shell, which should output a similar result: