Install Ruby with Rbenv on Ubuntu

Install Ruby with Rbenv on Ubuntu
Photo by Joshua Fuller / Unsplash

Ruby is a popular programming language that is widely used for developing web applications, scripting, and system administration tasks. If you are using Ubuntu as your operating system, you may want to install Ruby on your machine to start building your own applications. In this article, we will walk you through the process of installing Ruby on Ubuntu using rbenv.

Step 1: Install Prerequisites

Before you start installing Ruby on Ubuntu, make sure that your system has all the necessary dependencies installed. To do this, open a terminal window and run the following command:

sudo apt-get update
sudo apt-get install build-essential libssl-dev libreadline-dev zlib1g-dev

This command will update the package repository on your system and install the essential packages required for building Ruby from source.

Step 2: Install rbenv

rbenv is a tool that allows you to install multiple versions of Ruby on your system and switch between them as needed. To install rbenv on Ubuntu, you can use the following commands:

git clone https://github.com/rbenv/rbenv.git ~/.rbenv
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build

echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
exec $SHELL

These commands will clone the rbenv repository to your home directory, add the rbenv bin directory to your PATH environment variable, and set up rbenv for use in your current shell.

Step 3: Install Ruby

With rbenv installed, you can now use it to install the desired version of Ruby. To see a list of available Ruby versions, you can run the following command:

rbenv install -l

This command will list all the available Ruby versions that you can install with rbenv.

To install a specific version of Ruby, you can use the following command:

rbenv install <version>

Replace <version> with the desired version number. For example, to install Ruby version 3.2.1, you would run the following command:

rbenv install 3.2.1

This command will download the specified version of Ruby and compile it from the source.

Step 4: Set the Default Ruby Version

Once you have installed the desired version of Ruby, you can set it as the default version for your system using the following command:

rbenv global <version>

Replace <version> with the version of Ruby you just installed. For example, to set Ruby version 3.2.1 as the default version, you would run the following command:

rbenv global 3.2.1

This command will set the specified version of Ruby as the global version for your system.

Step 5: Verify the Installation

To verify that Ruby has been installed correctly on your system, you can run the following command:

ruby -v

This command will print the version of Ruby that is currently installed on your system. If the installation was successful, you should see the version number that you just installed.

Conclusion

In this article, we have shown you how to install Ruby on Ubuntu using rbenv. With Ruby installed, you can start building your own web applications, scripts, and system administration tasks using this powerful programming language.