Install Ruby on Ubuntu

Install Ruby on Ubuntu

Sagar Sharma
Sagar Sharma

Table of Contents

Ruby is one of the most popular programming languages and is mainly used to develop web applications.

And if you don't care about having the most recent version of Ruby, it can easily be installed with the following command:

sudo apt install ruby-full

While writing, it gave me Ruby version 3.0.2.

But what if you want to have the most recent version of Ruby on Ubuntu?

To do so, you can follow the given tutorial.

Install the latest version of Ruby on Ubuntu

While there are various ways to install the latest version of Ruby, I will show you how you can use the rbenv utility to have the latest version.

For those who don't know, rbenv is a CLI utility that allows users to install and manage the ruby environments.

So let's start with installing the rbenv utility.

First, install git and other dependencies:

sudo apt install git curl libssl-dev libreadline-dev zlib1g-dev libyaml-dev

Now, clone the repository and save the file contents inside the rbenv directory using the following command:

git clone ~/.rbenv

Then, add the PATH variable of the rbenv directory to the .bashrc using the given command.

echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc

Next, use the following command to initialize the rbenv in the current shell session whenever a new terminal window is opened:

echo 'eval "$(rbenv init -)"' >> ~/.bashrc

And finally, source the .bashrc file to take effect from the changes you've made:

source ~/.bashrc

Now, you'll have to install the ruby-build plugin.

To do so, first, clone the repo and save the contents inside .rbenv/plugins/ruby-build using the following command:

git clone ~/.rbenv/plugins/ruby-build

After that, add the PATH variable to the .bashrc file:

echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc

And finally, source the bashrc file:

source ~/.bashrc

The configuration part is over here. Now you can use the following command to install the latest version of Ruby on Ubuntu:

rbenv install $(rbenv install -l | grep -v - | tail -1)

It will install the latest version but if you have any other version installed, your system will still utilize the old version.

To make the latest version default, use the given command:

rbenv global $(rbenv versions --bare | grep -v - | tail -1)

And now, if you check the installed version of Ruby, it will show the latest stable version enabled on your system:

ruby -v
check the installed verison of ruby on ubuntu

Running the first program in Ruby

It is a tradition to run the Hello World program to kick off the programming journey,

So I will do the same with Ruby.

Here, I will be using the nano text editor but you can use any of your preferred ones.

First, to create and open the Ruby file, use the following command:

nano hello.rb

And paste the following line which will print the "Hello World" in the output:

#First program, fingers crossed!
puts "Hello, World!"

Now, save the changes and exit from the nano text editor.

Once done, you can run the hello world program using the following:

ruby hello.rb
run ruby programs in Ubuntu

And there you have it! Let me know if it helped you.

Uninstall rbenv and Ruby from Ubuntu

If there is no need for you to have rbenv and Ruby, removing them is a good idea and in this section, I will explain how you can remove rbenv and Ruby from Ubuntu.

First, you'll have to remove the Ruby using the rbenv.

And to do so, you'd have to list the installed version of the Ruby:

rbenv versions
list installed version of ruby using rbenv in ubuntu

Once you know the installed version, you can remove it using the following:

rbenv uninstall <ruby_version>

For me, the command would look like this:

rbenv uninstall 3.2.2
Uninstall ruby from Ubuntu using rbenv

If you have multiple versions of Ruby, you'd have to use the above command multiple times.

Once you are done removing Ruby versions, you can remove rbenv by removing the .rbenv directory itself:

rm -rf ~/.rbenv

Now, you'd have to remove the PATH variables that you added while installing otherwise it will throw an error at every new bash prompt:

rbenv error in ubuntu

To do so, first, open the bashrc file:

nano ~/.bashrc

Jump to the end of the file in nano using Alt + / and remove the lines that are associated with rbenv:

Once done, save changes and exit from the nano text editor.

To take effect from the changes, source the bashrc file:

source ~/.bashrc

That's it!

Just getting started with Linux?

If you are a beginner and want to start with basics, we have a dedicated series for that purpose:

Linux Command Tutorials for Absolute Beginners
Never used Linux commands before? No worries. This tutorial series is for absolute beginners to the Linux terminal.

I hope your queries are solved!

Sagar Sharma

Sagar Sharma

A software engineer who loves to tinker with hardware till it gets crashed. While reviving my crashed system, you can find me reading literature, manga, or watering my plants.