How To Change Ruby Version Rbenv

As a Ruby developer, there may be times when you need to switch between
different versions of Ruby for different projects. In this blog post, we
will discuss how to change the Ruby version using
rbenv, a popular Ruby version management tool.

Prerequisites

Before we start, make sure that you have the following installed on your
machine:

  • Ruby
  • rbenv
  • ruby-build plugin for rbenv

If you don’t have rbenv installed, you can follow the installation
instructions in the
rbenv repository.

Step 1: List Available Ruby Versions

First, let’s see which Ruby versions are available for installation. To
do this, run the following command:

      rbenv install --list
    

This will display a list of Ruby versions that you can install using
rbenv.

Step 2: Install a New Ruby Version

To install a new Ruby version, simply run the following command, where
version is the Ruby version you want to install:

      rbenv install version
    

For example, to install Ruby 2.7.1, you would run:

      rbenv install 2.7.1
    

The installation process may take a while. Once it’s complete, you can
verify that the new Ruby version is installed by running:

      rbenv versions
    

This will list all the installed Ruby versions on your machine, with an
asterisk next to the currently active version.

Step 3: Set the Ruby Version for Your Project

Now that you have the desired Ruby version installed, you can set it as
the default version for your project. First, navigate to your project
directory using the command line. Then, run the following command,
replacing version with the Ruby version you want to use:

      rbenv local version
    

For example, to set Ruby 2.7.1 as the default version for your project,
you would run:

      rbenv local 2.7.1
    

This will create a file called .ruby-version in your
project directory, specifying the Ruby version to be used. When you
start a new terminal session in your project directory, rbenv will
automatically switch to the Ruby version specified in this file.

Step 4: Verify the New Ruby Version

Finally, you can verify that the new Ruby version is being used by
running:

      ruby -v
    

This will display the currently active Ruby version, which should match
the version you set in your project directory.

Conclusion

In this blog post, we have shown you how to change the Ruby version for
your project using rbenv. With rbenv, it’s simple to manage multiple Ruby
versions and switch between them as needed. We hope this guide helps you
improve your Ruby development workflow!