How to Install Ruby on Debian

Ruby is a free, open-source, cross-platform, and dynamic programming language behind Ruby on Rails framework. The highly readable and clear syntax allows us to develop faster and more efficiently. So it is an excellent choice for beginners and experienced developers. Ruby is also known as Matz designed by Yukihiro Matsumoto. It runs on different operating systems, such as Windows, Mac OS and Linux.

In this tutorial, we will learn how to install Ruby using the Rbenv, RVM script and from the Debian repository on Debian 10.

Requirements

  • A server running Debian 10.
  • A root password is setup to your server.

Getting Started

Before starting, you will need to update your system with the latest version. You can do this by running the following command:

apt-get update -y
apt-get upgrade -y

Once your server is updated, restart your server to apply the changes.

Install Ruby with RVM

Install Ruby with RVM is the best method to install the latest version of Ruby on your system. You can manage and work with multiple Ruby environments with RVM. First, you will need to install the dependencies required to build Ruby from the source. You can install all the required dependencies with the following command:

apt-get install curl gnupg gnupg2 build-essential libgdbm-dev libncurses5-dev automake libtool bison libffi-dev -y

Once all the packages are installed, Download and import the RVM GPG keys with the following command:

curl -sSL https://rvm.io/mpapis.asc | gpg --import -

You should get the following output:

gpg: key 3804BB82D39DC0E3: 47 signatures not checked due to missing keys
gpg: /root/.gnupg/trustdb.gpg: trustdb created
gpg: key 3804BB82D39DC0E3: public key "Michal Papis (RVM signing) <[email protected]>" imported
gpg: Total number processed: 1
curl -sSL https://rvm.io/pkuczynski.asc | gpg --import -

You should get the following output:

gpg: key 105BD0E739499BDB: public key "Piotr Kuczynski <[email protected]>" imported
gpg: Total number processed: 1
gpg:               imported: 1

Next, install RVM by running the following command:

curl -sSL https://get.rvm.io | bash -s stable

Once the installation is completed, you should get the following output:

GPG verified '/usr/local/rvm/archives/rvm-1.29.9.tgz'
Creating group 'rvm'
Installing RVM to /usr/local/rvm/
Installation of RVM in /usr/local/rvm/ is almost complete:

  * First you need to add all users that will be using rvm to 'rvm' group,
    and logout - login again, anyone using rvm will be operating with `umask u=rwx,g=rwx,o=rx`.

  * To start using RVM you need to run `source /etc/profile.d/rvm.sh`
    in all your open shell windows, in rare cases you need to reopen all shell windows.
  * Please do NOT forget to add your users to the rvm group.
     The installer no longer auto-adds root or users to the rvm group. Admins must do this.
     Also, please note that group memberships are ONLY evaluated at login time.
     This means that users must log out then back in before group membership takes effect!
Thanks for installing RVM ????
Please consider donating to our open collective to help us maintain RVM.

????  Donate: https://opencollective.com/rvm/donate

Next, activate the RVM with the following command:

source /etc/profile.d/rvm.sh

You can now install the latest version of Ruby with the following command:

rvm install ruby

You should get the following output:

ruby-2.6.3 - #generating global wrappers.......
ruby-2.6.3 - #gemset created /usr/local/rvm/gems/ruby-2.6.3
ruby-2.6.3 - #importing gemsetfile /usr/local/rvm/gemsets/default.gems evaluated to empty gem list
ruby-2.6.3 - #generating default wrappers.......
ruby-2.6.3 - #adjusting #shebangs for (gem irb erb ri rdoc testrb rake).
Install of ruby-2.6.3 - #complete 
Ruby was built without documentation, to build it run: rvm docs generate-ri

You can also verify the Ruby version with the following command:

ruby -v

You should see the following output:

ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-linux]

If you want to install Bundler to manage application gem dependencies, run the following command:

gem install bundler

You can also install specific Ruby version as per your requirements. For example, to install Ruby version 2.6.0 run the following command:

rvm install ruby-2.6.0

Next, change the default Ruby version with the following command:

rvm --default use ruby-2.6.0

If you want to remove Ruby from your system run the following command:

rvm remove ruby-2.6.3
rvm remove ruby-2.6.0

Install Ruby from Debian Repository

Install Ruby from the APT repository is the easiest method. But, it will not install the latest version of Ruby.

You can install Ruby by running the following command:

apt-get install ruby-full -y

Once installed, check the version of Ruby with the following command:

ruby -v

You should see the following output:

ruby 2.5.5p157 (2019-03-15 revision 67260) [x86_64-linux-gnu]

You can remove the Ruby package from your system by running the following command:

apt-get remove ruby-full
apt-get autoremove

Install Ruby with Rbenv

You can also install Ruby with Rbenv. Rbenv allows you to switch Ruby versions as per your requirements.

First, you will need to install dependencies required by Rbenv to build Ruby from the source. You can install all the required dependencies with the following command:

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

Once all the packages are installed, run the following command to install both rbenv and ruby-build scripts:

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

You should see the following output:

Running doctor script to verify installation...
Checking for `rbenv' in PATH: not found
  You seem to have rbenv installed in `/root/.rbenv/bin', but that
  directory is not present in PATH. Please add it to PATH by configuring
  your `~/.bashrc', `~/.zshrc', or `~/.config/fish/config.fish'.

Next, you will need to add Rbenv path environment variable to .bashshrc file. You can add it with the following command:

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

Next, activate the environment variable with the following command:

source ~/.bashrc

At this point, Rbenv is installed on your system. You can now install the latest version of Ruby with the following command:

rbenv install 2.6.0

Once installed, set this version as a default version with the following command:

rbenv global 2.6.0

You can now verify the installed version of Ruby with the following command:

ruby -v

You should see the following output:

ruby 2.6.0p0 (2018-12-25 revision 66547) [x86_64-linux]

Conclusion

Congratulations! You have successfully installed Ruby on Debian 10 server. I hope you now have enough knowledge to install different Ruby versions on a per-user basis. Feel free to ask me if you have any questions.

Share this page:

0 Comment(s)