As a Ruby developer, you may often find yourself working on multiple projects that require different versions of Ruby. Managing and switching between different Ruby versions can be a headache. This is where rbenv comes to the rescue. In this blog post, we’ll learn how to switch Ruby versions using rbenv.
What is rbenv?
rbenv is a lightweight Ruby version management tool that allows you to easily switch between multiple Ruby versions. It does this by changing the $PATH environment variable to point to the selected version of Ruby. rbenv does not handle Ruby installation but relies on another tool called ruby-build to do that.
Installing rbenv and ruby-build
Before we begin, we need to ensure that rbenv and ruby-build are installed on our system. If you haven’t installed them yet, follow the instructions below:
brew install rbenv brew install ruby-build
sudo apt-get update sudo apt-get install rbenv sudo apt-get install ruby-build
Switching Ruby Versions with rbenv
Now that we have rbenv and ruby-build installed, we can switch between different Ruby versions. Let’s assume we have two projects, Project A requires Ruby 2.6.3 and Project B requires Ruby 2.7.1.
Step 1: Install the required Ruby versions using rbenv. If you haven’t installed the versions yet, run the following commands:
rbenv install 2.6.3 rbenv install 2.7.1
Step 2: To switch to a specific Ruby version, use the rbenv global command followed by the desired version number:
rbenv global 2.6.3
Now, if you run
ruby -v, it should show the selected version:
$ ruby -v ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin20]
Step 3: To switch to the other Ruby version, just use the rbenv global command with the other version number:
rbenv global 2.7.1
Now, if you run
ruby -v, it should show the switched version:
$ ruby -v ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-darwin20]
Setting Ruby Versions Per Project
Instead of manually switching Ruby versions every time you work on a different project, you can set a specific Ruby version for each project using an .rbenv-version file. This file should be placed in the root directory of your project and should contain the desired Ruby version.
For example, to set Ruby 2.6.3 for Project A, create an .rbenv-version file in the root directory of Project A and add the following line:
Now, when you navigate to Project A’s directory and run
ruby -v, it should show Ruby 2.6.3 regardless of the global Ruby version set using rbenv global.
Similarly, create an .rbenv-version file for Project B and add the following line:
Now, when you navigate to Project B’s directory and run
ruby -v, it should show Ruby 2.7.1 regardless of the global Ruby version set using rbenv global.
In this blog post, we learned how to switch Ruby versions using rbenv, as well as how to set specific Ruby versions for individual projects. With rbenv, you no longer need to worry about managing multiple Ruby versions for different projects. Just install rbenv and ruby-build, and you’re good to go!