How to Install Ruby on Debian 9

By 

Published on

4 min read

Install Ruby on Debian

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:

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

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

    Terminal
    ruby --version

    The output will look something like this:

    output
    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:

    Terminal
    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
  2. Next, run the following curl command to install both rbenv and ruby-build scripts:

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

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

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

    If you are using Bash, type:

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

    If you are using Zsh type:

    Terminal
    echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.zshrc
    echo 'eval "$(rbenv init -)"' >> ~/.zshrc
    source ~/.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:

    Terminal
    rbenv install 2.5.1
    rbenv global 2.5.1

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

    Terminal
    ruby -v
    output
    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:

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

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

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

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

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

    Terminal
    rvm install ruby
    rvm --default use ruby

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

    Terminal
    ruby -v
    output
    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 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.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page