How To Change Ruby Version

In this blog post, we will learn how to change Ruby version on your local
development environment. This is essential when you are working on multiple
projects that require different versions of Ruby. We will be using
RVM (Ruby Version Manager) and RBENV (Ruby
Environment) for this tutorial.

Using RVM (Ruby Version Manager)

RVM is a command-line tool that allows you to easily install, manage, and
work with multiple ruby environments. Here’s how to change the Ruby version
using RVM:

1. Install RVM

If you don’t already have RVM installed, you can install it by running the
following commands in your terminal:

    curl -sSL https://get.rvm.io | bash -s stable
    source ~/.rvm/scripts/rvm
    

2. Install the Ruby version you want to use

To install a specific Ruby version, you can use the rvm install
command followed by the version number. For example, to install Ruby
2.7.1, run:

    rvm install 2.7.1
    

3. Set the Ruby version for your project

To change the Ruby version for your project, navigate to the project
directory in the terminal and run the following command:

    rvm use 2.7.1
    

Using RBENV (Ruby Environment)

RBENV is another popular tool to manage multiple Ruby versions. Here’s how to
change the Ruby version using RBENV:

1. Install RBENV

If you don’t have RBENV installed, you can install it by running the
following command in your terminal:

    git clone https://github.com/rbenv/rbenv.git ~/.rbenv
    echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
    echo 'eval "$(rbenv init -)"' >> ~/.bashrc
    exec $SHELL
    git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
    echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
    exec $SHELL
    

2. Install the Ruby version you want to use

To install a specific Ruby version, you can use the rbenv install
command followed by the version number. For example, to install Ruby
2.7.1, run:

    rbenv install 2.7.1
    

3. Set the Ruby version for your project

To change the Ruby version for your project, navigate to the project
directory in the terminal and run the following command:

    rbenv local 2.7.1
    

That’s it! You can now easily switch between different Ruby versions using
either RVM or RBENV. Happy coding!