One of the unique features of programming in Ruby is its ability to manage multiple versions. Ruby developers often work with different Ruby versions for different projects.
This is where rbenv comes in, a popular Ruby version manager that makes it easy to switch between different versions of Ruby on your system. In this blog post, we will learn how to set the Ruby version using rbenv.
Prerequisites
Before we start, you need to have rbenv installed on your system. If you haven’t installed it yet, follow the instructions on the rbenv GitHub page to get it set up.
Step 1: List Available Ruby Versions
First, let’s check the available Ruby versions on your system. To do this, open the terminal and enter the following command:
rbenv versions
This command will list all the installed Ruby versions on your system. You should see something like this:
system
2.6.5
* 2.7.2 (set by /home/user/.rbenv/version)
3.0.0
In this output, the asterisk (*) indicates the currently active Ruby version.
Step 2: Install a New Ruby Version (Optional)
If the Ruby version you want to set is not installed on your system, you can install it using rbenv. To do this, run the following command, replacing ‘X.X.X’ with the desired Ruby version:
rbenv install X.X.X
This process may take some time as it downloads and installs the specified Ruby version. Once completed, it will be available to set as your active Ruby version.
Step 3: Set the Ruby Version
To set a Ruby version using rbenv, you have two options: setting it globally or locally.
Option 1: Set the Global Ruby Version
To set a Ruby version as the global default for your system, run the following command, replacing ‘X.X.X’ with the desired Ruby version:
rbenv global X.X.X
This command will set the specified version as the default Ruby version for your system. To verify the change, run rbenv versions again, and you should see the asterisk next to the newly set version.
Option 2: Set a Local Ruby Version for a Specific Project
If you want to set a Ruby version only for a specific project, you can set it locally. First, navigate to your project’s directory in the terminal. Then, run the following command, replacing ‘X.X.X’ with the desired Ruby version:
rbenv local X.X.X
This command creates a file named .ruby-version in the project’s root directory, which contains the specified Ruby version. rbenv will use this version whenever you’re working within this project directory.
Conclusion
And that’s it! You’ve learned how to set Ruby versions using rbenv, both globally and locally for specific projects. This skill is essential for Ruby developers, as it allows you to work with different Ruby versions and manage project dependencies efficiently. Happy coding!