How to Install Ruby on Ubuntu 20.04 | 18.04

This brief tutorial shows students and new users how to install Ruby on Ubuntu 20.04 | 18.04.

Ruby is a popular, high-level programming language with focus on simplicity. It has elegant syntax that is to read and easy to write and is behind the powerful Ruby on Rails Framework.

There are multiple ways to install Ruby on Ubuntu. The simplest is using Ubuntu built-in apt package manager. The version of Ruby in Ubuntu packages manager is usually the latest stable version.

In most cases, this should be good for developing apps in Ruby or using it in Rails framework.

If you’re a student or new user looking for a Linux system to start learning on, the easiest place to start is Ubuntu Linux OS…. It’s a great Linux operating system for beginners and folks looking for easier Linux distribution to use.

To install Ruby on Ubuntu, follow the steps below:

Install Ruby via Apt

As mentioned above, the easiest way to install Ruby on Ubuntu is via apt package manager. To do that, simply run the commands below:

sudo apt update
sudo apt install ruby-full

To verify if Ruby is installed, run the commands below:

ruby --version

It should output similar line as below:

Ouput:
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux-gnu]

Installing Ruby via Rbenv

Another way to install Ruby is via Rbenv. Rbenv is a tool that allows you to switch between different versions of Ruby, but doesn’t allow you to install Ruby.

To get Ruby, we’ll need to use ruby-build. To install Ruby-build, run the commands below to install dependencies.

sudo apt update
sudo apt install git curl libssl-dev libreadline-dev zlib1g-dev autoconf bison build-essential libyaml-dev libreadline-dev libncurses5-dev libffi-dev libgdbm-dev

After that, run the commands below to clone both Rbenv and Ruby-build repositories at Github.

curl -sL https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-installer | bash -

Next run the commands below if you’re running Bash Shell in Ubuntu.

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

If you’re running Z Shell, run the lines below:

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

After the steps above, you can install any available version of Ruby. To list the available versions, run the commands below:

rbenv install -l

Then install the version you want by using the commands below. For this post, we’re installing Ruby 2.5.1.

rbenv install 2.5.1
rbenv global 2.5.1

Replace the version number with correct one that suite your environment.

To verify the Ruby version, run the commands below:

ruby -v

It should output similar line as below:

Output:
ruby 2.5.1p206 (2019-10-01 revision 67816) [x86_64-linux]

Installing Ruby via RVM

RVM is another tool for installing and managing Ruby on Linux systems, including Ubuntu.

To install RVM and use it to get Ruby, follow the steps below:

First install dependencies by running the commands below:

sudo apt update
sudo apt install curl g++ gcc autoconf automake bison libc6-dev libffi-dev libgdbm-dev libncurses5-dev libsqlite3-dev libtool libyaml-dev make pkg-config sqlite3 zlib1g-dev libgmp-dev libreadline-dev libssl-dev

Then run the commands below to install RVM.

gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
curl -sSL https://get.rvm.io | bash -s stable

After that, run the command below to activate RVM

source ~/.rvm/scripts/rvm

To install Ruby, run the commands below:

rvm install 2.5.1
rvm use 2.5.1 --default

To verify if Ruby is installed, run the commands below:

ruby -v

It should output similar line as below:

Output:
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]

Conclusion:

These are the many ways to install Ruby on Ubuntu. If you find errors in the steps above, please let us know by commenting in the form below:

Thanks,

You may also like the post below:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.