This tutorial will walk you through the steps of installing Ruby on a Debian 9 system. 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.

There are several different ways of installing Ruby on Debian. In the following sections, we will show how to install Ruby using the Rbenv and RVM script and from the default Debian repositories.

Prerequisites

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

Method 1: Install Ruby from Debian Repositories

The easiest way to install Ruby on your Debian system is through the apt package manager. At the time of writing, the version included in the Debian repositories is 2.3.3 which will EOL soon.

  1. First, refresh the packages list with:

    sudo apt update
  2. Install the ruby-full package by running the following command:

    sudo apt install ruby-full
  3. Once the installation is completed, you can verify that it was successful by printing the Ruby version:

    ruby --version

    The output will look something like this:

    ruby 2.3.3p222 (2016-11-21) [x86_64-linux-gnu]

Method 2: Install 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. ruby-build 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.

  1. Install the dependencies 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 scripts:

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

    On successful installation, the script will print something like this:

  3. Before starting using rbenv we need to add $HOME/.rbenv/bin to our PATH.

    If you are using Bash, type:

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

    If you are using Zsh type:

    echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.zshrcecho 'eval "$(rbenv init -)"' >> ~/.zshrcsource ~/.zshrc
  4. Now that rbenv is installed on our system we can easily install the latest stable version of Ruby and set it as default version with:

    rbenv install 2.5.1rbenv global 2.5.1

    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]

Method 3: Install Ruby using RVM

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

  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. Next, run the following commands to add the GPG key and install RVM:

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

    On successful installation, the script will print something like this:

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

    source ~/.rvm/scripts/rvm
  4. Install the latest stable version of Ruby with RVM and set it as 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 Debian 9 server. The method you choose depends on your requirements and preferences. Even though installing the packaged version from the Debian repository is easier, the Rbenv and RVM methods gives 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.