How To Install Ruby on Rails with rbenv on Ubuntu 16.04

How To Install Ruby on Rails with rbenv on Ubuntu 16.04
Not using Ubuntu 16.04?Choose a different version or distribution.
Ubuntu 16.04


Ruby on Rails is one of the most popular application stacks for developers looking to create sites and web apps. The Ruby programming language, combined with the Rails development framework, makes app development simple.

You can easily install Ruby and Rails with the command-line tool rbenv. Using rbenv will provide you with a solid environment for developing your Ruby on Rails applications as it will let you easily switch Ruby versions, keeping your entire team on the same version. The project repository is located on [GitHub] (https://github.com/rbenv/rbenv).

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.

This tutorial will take you through the Ruby and Rails installation process via rbenv.


To follow this tutorial, you need:

##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

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 ~/.basrc file 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:

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.5.0, and once it’s done installing, we can set it as our default version with the global sub-command:

  1. rbenv install 2.5.0
  2. rbenv global 2.5.0

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

Verify that Ruby was properly installed by checking your version number:

  1. ruby -v

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

ruby 2.5.0p0 (2017-12-25 revision 61468) [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

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, with sammy being the name of the user:


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

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, like Rails does, you should run:

  1. rbenv rehash

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:

Rails 5.1.5

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, typing this will uninstall Ruby version 2.1.3:

  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.


We have covered the basics of how to install rbenv and Ruby on Rails here so that you can use multiple Ruby environments. For your next steps, you can learn more about making those environments more robust. For more scalability, centralization, and control in your Ruby on Rails application, you may want to use it with [PostgreSQL] (https://www.digitalocean.com/community/tutorials/how-to-use-postgresql-with-your-ruby-on-rails-application-on-ubuntu-14-04) or [MySQL] (https://www.digitalocean.com/community/tutorials/how-to-use-mysql-with-your-ruby-on-rails-application-on-ubuntu-14-04) rather than its default sqlite3 database. 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 us

About the authors

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?

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!

I found it useful to run the following commands before starting the gems section of the tutorial.

create the .gemrc file

“$ touch ~/.gemrc”

add rubygems.org to sources

“$ gem source -a https://rubygems.org

I also visited the nodesource repo on github to adapt the tutorial for installing the 7.x version which was my preference.

Nice job on the tutorial, I found it very helpful.

Thanks, it helped a lot. Gratitude :)

How to install ‘start command prompt with ruby’ terminal in Ubuntu 16.04?

Great tutorial!, I am using ruby on rails for the first time on a virtual machine and was able to do it and fix some issues thanks to your post. Do you have any other tutorials about developing on ruby on rails? I signed up thanks to this tutorial. lol

I’m on Ubuntu server 16.04 LTS and had to append my PATH in .bashrc with .rbenv/rbenv/bin instead of .rbenv/bin, as mentioned above.

When running

rbenv install 2.3.1

the build may fail with

make: *** [rdoc] Error 137

This can be overcome by adding the variable to disable the doc building step of the install.

RUBY_CONFIGURE_OPTS=--disable-install-doc rbenv install 2.3.1

However, I am not familiar with what the repercussions of disabling the doc building step are.

How about sudo apt-get install rbenv ruby-build ?

I needed to restart my shell session for the changes to .bashrc to be refreshed in Ubuntu 16.04.

Shouldn’t the second line begin with ‘sudo’? Otherwise this worked great.

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!

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

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

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel