How to Install Ruby on Rails on Debian 9 Stretch Linux

Introduction

Ruby on Rails is the web framework that revolutionized web development a few years ago and powers many of the hottest start-ups today. It allows developers to rapidly develop working prototypes and even full sites without having to reinvent the wheel or worry about loads of configuration.

Ruby runs best on Unix-like systems, making Linux an excellent choice for developing for Rails. Debian Stretch comes loaded with up-to-date version of Ruby and Rails as well as providing support for the popular RVM Ruby manager.

Installing Ruby and Rails

There are two basic ways to install Ruby and Rails on Debian Stretch. The first is to use RVM(Ruby Version Manager). It allows you to change and select any current version of Ruby and compartmentalize installs.

The other option is to use the packages in the Debian repositories. They are stable and kept relatively current. They also can be used system wide.



The RVM Way

Before actually installing RVM, there are a couple of dependencies that need to be installed. So, it’s best to get that out of the way first.

# apt install build-essential curl nodejs

Now you need to add the RVM GPG keys. This will ensure secure downloads through RVM.

$gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3

Once you’ve added the key, you can use cURL to get the RVM script and install Ruby.

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

The script will take some time to run. RVM compiles Ruby from source. It’s not too big, but it will still need some time to build.

The RVM script should automatically add itself and Ruby to your Bash path. Closing and re-opening your terminal should make Ruby available. If not, you can run the command below to force it to be added.

$ source ~/.rvm/scripts/rvm

You can test out whether or not it has been added and is working properly by running RVM’s help command.

$ rvm help

RVM should have pulled in and built the latest stable release of Ruby. To check what that is, run Ruby’s version command.

$ ruby -v

If you want or need a different version of Ruby than the one installed, you can list all of the ones available to RVM.

$ rvm list known

To install one of them run rvm install followed by the version number that you want.

$ rvm install 2.3

In order to specify which version of Ruby you want to use, just tell RVM.

$ rvm use 2.3

If you want to make that version the default, add that at the end.

$ rvm use 2.3 --default

So, that may be a lot to take in, but with that range of options, it’s clear why a lot of Rails developers prefer to use RVM.

Now that you have Ruby installed, you can use Ruby’s gem package management system to install Rails. Gems work similarly to Linux packages, and can be installed with a single command.

$ gem install rails

If you need a specific version, specify that.

$ gem install rails -v 4.2

Rails will take a few minutes to install. It’s fairly large. Afterward, you will be ready to start a Rails project.



The Debian Way

The Debian way of installing Ruby and Rails is much, much easier. That convenience comes at a cut in flexibility. If you plan on keeping Ruby consistent with the latest stable versions, it might work better to use the Debian packages.

To install Ruby and Rails, just use apt.

# apt install ruby rails

Starting Your Project

With Ruby and Rails both installed, setting up a Rails project is super easy. Just cd to the directory where you want to start it, and run the command provided by Rails to create a new project.

$ cd /directory/containing/site/
$ rails new yourproject

Rails will create a new folder and fill it with all of the files and folders necessary for a Ruby on Rails project.

You can test that everything is installed and working properly entering the new project folder and running the built-in development server that comes with Rails.

$ cd yourproject
$ rails s

You can open up your browser and navigate to localhost:300. You should see the Rails welcome page.

Ruby on Rails running on Debian Stretch

Conclusion

Getting up and running with Rails on Debian Stretch is fairly easy. RVM provides a ton of options, but requires some work to set up. Debian has everything you need in its repositories, but they limit the control you have over your development environment. Either way, Stretch makes a great Rails development platform.