Tutorial

How To Install Ruby on Rails with rbenv on Debian 8

Published on December 16, 2016
How To Install Ruby on Rails with rbenv on Debian 8
Not using Debian 8?Choose a different version or distribution.
Debian 8

Introduction

One of the most popular application stacks for developers looking to create sites and web apps is Ruby on Rails. App development is simplified through making use of the Ruby programming language combined with the Rails development framework.

The command-line tool rbenv allows you to install and manage Ruby and Rails. Using rbenv will provide you with a solid environment for developing your Ruby on Rails applications as it will let you move between Ruby versions as needed, keeping your entire team on the same version. The project repository is located on GitHub.

rbenv provides support for specifying application-specific versions of Ruby, lets you change the global Ruby for each user, and allows you to use an environment variable to override the Ruby version.

##Prerequisites

This tutorial will take you through the Ruby and Rails installation process via rbenv on Debian 8. To follow this tutorial, you need to have one Debian 8 server with a [non-root user] (https://www.digitalocean.com/community/tutorials/initial-server-setup-with-debian-8).

##Update and install dependencies

First, we should update apt-get since this is the first time we will be using apt in this session. This will ensure that the local package cache is updated.

  1. sudo apt-get update

Next, let’s install the dependencies required for rbenv and Ruby with apt-get:

  1. sudo apt-get install autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm3 libgdbm-dev

Because we’ll be cloning rbenv from Git, we’ll install Git as well:

  1. sudo apt-get install git-core

Once we have all of the required system dependencies installed, we can move onto the installation of rbenv itself.

##Install rbenv

Now we are ready to install rbenv. Let’s clone the rbenv repository from Git. You should complete these steps from the user account from which you plan to run Ruby.

  1. git clone https://github.com/rbenv/rbenv.git ~/.rbenv

From here, you should add ~/.rbenv/bin to your $PATH so that you can use rbenv’s command line utility. Also adding ~/.rbenv/bin/rbenv init to your ~/.bash_profile will let you load rbenv automatically.

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

Next, source rbenv by typing:

  1. source ~/.bashrc

You can check to see if rbenv was set up properly by using the type command, which will display more information about rbenv:

  1. type rbenv

Your terminal window should output the following:

Output
rbenv is a function rbenv () { local command; command="$1"; if [ "$#" -gt 0 ]; then shift; fi; case "$command" in rehash | shell) eval "$(rbenv "sh-$command" "$@")" ;; *) command rbenv "$command" "$@" ;; esac }

In order to use the rbenv install command, which simplifies the installation process for new versions of Ruby, you should install [ruby-build] (https://github.com/rbenv/ruby-build), which we will install as a plugin for rbenv through Git:

  1. git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build

At this point, you should have both rbenv and ruby-build installed, and we can move on to installing Ruby.

##Install Ruby

With the ruby-build rbenv plugin now installed, we can install whatever versions of Ruby that we may need through a simple command. First, let’s list all the available versions of Ruby:

  1. rbenv install -l

The output of that command should be a long list of versions that you can choose to install.

We’ll now install a particular version of Ruby. It’s important to keep in mind that installing Ruby can be a lengthy process, so be prepared for the installation to take some time to complete.

As an example here, let’s install Ruby version 2.3.3:

  1. rbenv install 2.3.3

If you would like to install and use a different version, run the rbenv commands with a different version number, as in rbenv install 2.3.0 and rbenv global 2.3.0.

The installation process may take some time. You should receive similar output, with sammy being the name of the user, once the installation is complete:

Output
-> https://cache.ruby-lang.org/pub/ruby/2.3/ruby-2.3.3.tar.bz2 Installing ruby-2.3.3... Installed ruby-2.3.3 to /home/sammy/.rbenv/versions/2.3.3

Now set the set the version we just installed it as our default version with the global sub-command:

  1. rbenv global 2.3.3

Verify that everything is all ready to go by using the ruby command to check the version number:

  1. ruby -v

If you installed version 2.3.3 of Ruby, your output to the above command should look something like this:

Output
ruby 2.3.3p222 (2016-11-21 revision 56859) [x86_64-linux]

You now have at least one version of Ruby installed and have set your default Ruby version. Next, we will set up gems and Rails.

##Working with Gems

Gems are packages that extend the functionality of Ruby. We will want to install Rails through the gem command.

So that the process of installing Rails is less lengthy, we will turn off local documentation for each gem we install. We will also install the bundler gem to manage application dependencies:

  1. echo "gem: --no-document" > ~/.gemrc
  2. gem install bundler

When that installation process is complete, you should receive similar output:

Output
Fetching: bundler-1.13.6.gem (100%) Successfully installed bundler-1.13.6 1 gem installed

You can use the gem env command (the subcommand env is short for environment) to learn more about the environment and configuration of gems. You can check the location where gems are being installed by using the home argument, which will show the pathway to where gems are installed on your server.

  1. gem env home

Your output should look something like this:

/home/sammy/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0

Once we have gems set up, we can move on to install Rails.

##Install Rails

As the same user, you can install the most recent version of Rails with the gem install command:

  1. gem install rails

You’ll receive output throughout this installation process with confirmation that Rails was successfully installed by the end.

If you would like to install a specific version of Rails, you can list the valid versions of Rails by doing a search, which will output a long list of possible versions. We can then install a specific version, such as 4.2.7:

  1. gem search '^rails$' --all
  2. gem install rails -v 4.2.7

rbenv works by creating a directory of shims, which point to the files used by the Ruby version that’s currently enabled. Through the rehash sub-command, rbenv maintains shims in that directory to match every Ruby command across every installed version of Ruby on your server. Whenever you install a new version of Ruby or a gem that provides commands, you should run:

  1. rbenv rehash

Since there’s no output when this is successful, we can verify that Rails has been installed properly by printing its version, with this command:

  1. rails -v

If it installed properly, you will see the version of Rails that was installed. We can now continue to set up our Ruby on Rails environment.

##Install JavaScript Runtime

A few Rails features, such as the Asset Pipeline, depend on a JavaScript Runtime. We will install Node.js to provide this functionality.

We can first move to a writable directory such as /tmp. From there, let’s verify the Node.js script by outputting it to a file, then read it with less:

  1. cd /tmp
  2. \curl -sSL https://deb.nodesource.com/setup_6.x -o nodejs.sh
  3. less nodejs.sh

Once we are satisfied with the Node.js script, we can exit out of less by typing q.

We can now install the NodeSource Node.js v6.x repo:

  1. cat /tmp/nodejs.sh | sudo -E bash -

The -E flag used here will preserve the user’s existing environment variables.

Once installation is complete, we can use apt-get to install Node.Js:

  1. sudo apt-get install -y nodejs

At this point, you can begin testing your Ruby on Rails installation and start to develop web applications.

##Updating rbenv

As we installed rbenv manually using Git, we can upgrade our installation to the most recent version at any time:

  1. cd ~/.rbenv
  2. git pull

This will ensure that we are using the most up-to-date version of rbenv available.

##Uninstalling Ruby versions

As you download more versions of Ruby, you may accumulate more versions than you would like in your ~/.rbenv/versions directory.

Use the ruby-build plugin to automate the removal process with the uninstall subcommand. For example, if we had installed Ruby 2.1.3, typing this would uninstall it:

  1. rbenv uninstall 2.1.3

With the rbenv uninstall command you can clean up your versions of Ruby so that you do not have more installed than you are currently using.

##Learning More

We have covered the basics of how to install rbenv and Ruby on Rails here so that you can use multiple Ruby environments. As your needs grow, you can also learn how to [scale Ruby on Rails applications across multiple servers] (https://www.digitalocean.com/community/tutorials/how-to-scale-ruby-on-rails-applications-across-multiple-droplets-part-1).

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
2 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

It works perfectly even for later version :) I’ve used stretch .

Hello, i need help here. Whenever i try to install ruby 2.4.0 on debian 8 by either rbenv or from source. I get this messge when running make or installling via rbenv…

configuring zlib
make[1]: Entering directory '/root/ruby-2.4.0'
exts.mk:1468: *** missing separator (did you mean TAB instead of 8 spaces?).  Stop.
make[1]: Leaving directory '/root/ruby-2.4.0'
uncommon.mk:220: recipe for target 'build-ext' failed
make: *** [build-ext] Error 2

Please help :/

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.