You first need to install rbenv and then use it to install Ruby v2.7.
Open your terminal and run the following command:
$ git clone https://github.com/rbenv/rbenv.git ~/.rbenv
We simply clone rbenv GitHub repository into the ~/.rbenv folder.
You need to have Git installed on your system. If not, simply run sudo apt install git from your command line.
Next, run the following commands to set up rbenv:
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
$ echo 'eval "$(rbenv init -)"' >> ~/.bashrc
$ exec $SHELL
Next, you need to download the ruby-build plugin into the plugins folder of rbenv as follows:
$ git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build
The ruby-build plugin adds the install command to rbenv.
Note: You can also install rbenv and ruby-build using the sudo apt-get install rbenv ruby-build command.
Finally, you can install Ruby 2.7.1 using the following command:
$ rbenv install 2.7.1
$ rbenv global 2.7.1
This is the output in the terminal:
Installed ruby-2.7.1 to /home/<user_name>/.rbenv/versions/2.7.1
If your installation fails, make sure you install the libreadline-dev and zlib1g-dev dependencies:
$ sudo apt-get install -y libreadline-dev zlib1g-dev
You can check out your Ruby version using the following command:
$ ruby -v
Next, you need to install bundler with gem:
$ gem install bundler
This will install bundler v2.0.2.
After installing bundler, you need to run:
$ rbenv rehash