How to Install Ruby on Ubuntu 18.04

Updated on

4 min read

Install Ruby on Ubuntu

Ruby is one of the most popular languages today. It has an elegant syntax and it is the language behind the powerful Ruby on Rails framework.

In this tutorial we will show you three different ways to install Ruby on Ubuntu 18.04 system.

Prerequisites

Before starting with the tutorial, make sure you are logged in as a user with sudo privileges .

Installing Ruby from Ubuntu Repositories

The easiest way to install Ruby on your Ubuntu system is through the apt package manager. At the time of writing, the version in the Ubuntu repositories is 2.5.1 which is the latest stable version of Ruby.

To install Ruby from the default Ubuntu repositories, follow these steps:

  1. First, update the packages index:

    sudo apt update
  2. Install Ruby by typing:

    sudo apt install ruby-full
  3. To verify that the installation it was successful run the following command which will print the Ruby version:

    ruby --version

    The output will look something like this:

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

Installing Ruby using Rbenv

Rbenv is a lightweight Ruby version management tool which allows you to easily switch Ruby versions. By default Rbenv doesn’t handle installing Ruby versions so we also need to install ruby-build which is a tool that helps you to install any version of Ruby you may need. It is available as a standalone program and as a plugin for rbenv.

To install Ruby using the Rbenv script, follow these steps:

  1. First, update the packages index and install the packages required for the ruby-build tool to build Ruby from source:

    sudo apt updatesudo apt install git curl libssl-dev libreadline-dev zlib1g-dev autoconf bison build-essential libyaml-dev libreadline-dev libncurses5-dev libffi-dev libgdbm-dev
  2. Next, run the following curl command to install both rbenv and ruby-build:

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

    The script will clone both rbenv and ruby-build repositories from GitHub to ~/.rbenv directory. The installer script also calls another script which will try to verify the installation. The output of the script will look something like below:

    Ubuntu Install Ruby using Rbenv
  3. Add $HOME/.rbenv/bin to the user PATH .

    If you are using Bash, run:

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

    If you are using Zsh run:

    echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.zshrcecho 'eval "$(rbenv init -)"' >> ~/.zshrcsource ~/.zshrc
  4. Install the latest stable version of Ruby and set it as a default version with:

    rbenv install 2.5.1rbenv global 2.5.1

    To list all available Ruby versions you can use: rbenv install -l

    Verify that Ruby was properly installed by printing the version number:

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

Install Ruby using RVM

RVM is another tool for installing, managing and working with multiple Ruby environments.

To install Ruby using the RVM script, follow these steps:

  1. First install the dependencies required for the RVM utility to build Ruby from source:

    sudo apt updatesudo 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
  2. Run the following commands to install RVM:

    gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDBcurl -sSL https://get.rvm.io | bash -s stable
    Ubuntu Install Ruby using RVM

    To start using RVM you need to run the following command:

    source ~/.rvm/scripts/rvm
  3. Install the latest stable version of Ruby with RVM and set it as the default version with:

    rvm install 2.5.1rvm use 2.5.1 --default

    Verify that Ruby was properly installed by printing the version number:

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

For more information about how to manage your Ruby installations visit the RVM Documentation page .

Conclusion

We have shown you three different ways to install Ruby on your Ubuntu 18.04 server. The method you choose depends on your requirements and preferences. Even though installing the packaged version from the Ubuntu repository is easier, the Rbenv and RVM methods give you more flexibility for adding and removing different Ruby versions on a per user basis.

If you have any questions or feedback, feel free to comment below.