Tutorial

How To Install Ruby on Rails with rbenv on Ubuntu 14.04

Published on March 14, 2015
How To Install Ruby on Rails with rbenv on Ubuntu 14.04
Not using Ubuntu 14.04?Choose a different version or distribution.
Ubuntu 14.04

Introduction

Ruby on Rails is an extremely popular open-source web framework that provides a great way to write web applications with Ruby.

This tutorial will show you how to install Ruby on Rails on Ubuntu 14.04, using rbenv. This will provide you with a solid environment for developing your Ruby on Rails applications. rbenv provides an easy way to install and manage various versions of Ruby, and it is simpler and less intrusive than RVM. This will help you ensure that the Ruby version you are developing against matches your production environment.

Prerequisites

Before installing rbenv, you must have access to a superuser account on an Ubuntu 14.04 server. Follow steps 1-3 of this tutorial, if you need help setting this up: Initial Server Setup on Ubuntu 14.04

When you have the prerequisites out of the way, let’s move on to installing rbenv.

Install rbenv

Let’s install rbenv, which we will use to install and manage our Ruby installation.

First, update apt-get:

  1. sudo apt-get update

Install the rbenv and Ruby dependencies with apt-get:

  1. sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev python-software-properties libffi-dev

Now we are ready to install rbenv. The easiest way to do that is to run these commands, as the user that will be using Ruby:

  1. cd
  2. git clone git://github.com/sstephenson/rbenv.git .rbenv
  3. echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
  4. echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
  5. git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
  6. echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bash_profile
  7. source ~/.bash_profile

Note: On Ubuntu Desktop, replace all occurrences .bash_profile in the above code block with .bashrc.

This installs rbenv into your home directory, and sets the appropriate environment variables that will allow rbenv to the active version of Ruby.

Now we’re ready to install Ruby.

Install Ruby

Before using rbenv, determine which version of Ruby that you want to install. We will install the latest version, at the time of this writing, Ruby 2.2.3. You can look up the latest version of Ruby by going to the Ruby Downloads page.

As the user that will be using Ruby, install it with these commands:

  1. rbenv install -v 2.2.3
  2. rbenv global 2.2.3

The global sub-command sets the default version of Ruby that all of your shells will use. If you want to install and use a different version, simply run the rbenv commands with a different version number.

Verify that Ruby was installed properly with this command:

  1. ruby -v

It is likely that you will not want Rubygems to generate local documentation for each gem that you install, as this process can be lengthy. To disable this, run this command:

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

You will also want to install the bundler gem, to manage your application dependencies:

  1. gem install bundler

Now that Ruby is installed, let’s install Rails.

Install Rails

As the same user, install Rails with this command (you may specify a specific version with the -v option):

  1. gem install rails

Whenever you install a new version of Ruby or a gem that provides commands, you should run the rehash sub-command. This will install shims for all Ruby executables known to rbenv, which will allow you to use the executables:

  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.

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.

Add the Node.js PPA to apt-get:

  1. sudo add-apt-repository ppa:chris-lea/node.js

Then update apt-get and install the Node.js package:

  1. sudo apt-get update
  2. sudo apt-get install nodejs

Congratulations! Ruby on Rails is now installed on your system.

Optional Steps

If you’re looking to improve your setup, here are a few suggestions:

Configure Git

A good version control system is essential when coding applications. Follow the How To Set Up Git section of the How To Install Git tutorial.

Install a Database

Rails uses sqlite3 as its default database, which may not meet the requirements of your application. You may want to install an RDBMS, such as MySQL or PostgreSQL, for this purpose.

For example, if you want to use MySQL as your database, install MySQL with apt-get:

  1. sudo apt-get install mysql-server mysql-client libmysqlclient-dev

Then install the mysql2 gem, like this:

  1. gem install mysql2

Now you can use MySQL with your Rails application. Be sure to configure MySQL and your Rails application properly.

Create a Test Application (Optional)

If you want to make sure that your Ruby on Rails installation went smoothly, you can quickly create a test application to test it out. For simplicity, our test application will use sqlite3 for its database.

Create a new Rails application in your home directory:

  1. cd ~
  2. rails new testapp

Then move into the application’s directory:

  1. cd testapp

Create the sqlite3 database:

  1. rake db:create

If you don’t already know the public IP address of your server, look it up with this command:

  1. ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'

Copy the IPv4 address to your clipboard, then use it with this command to start your Rails application (substitute the highlighted part with the IP address):

  1. rails server --binding=server_public_IP

If it is working properly, your Rails application should be running on port 3000 of the public IP address of your server. Visit your Rails application by going there in a web browser:

http://server_public_IP:3000

If you see the Rails “Welcome aboard” page, your Ruby on Rails installation is working properly!

Conclusion

You’re now ready to start developing your new Ruby on Rails application. Good luck!

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 author(s)

Mitchell Anicas
Mitchell Anicas
See author profile
Category:
Tutorial

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
45 Comments
Leave a comment...

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 ran through the configuration and it seemed that all went well. However when I tried bundle install under my app I get Your Ruby version is 1.9.3, but your Gemfile specified 2.2.1 error. I guess some configuration must have gone wrong?

Never mind I got the error. I didn’t run rbenv rehash and it must have not recognized the new ruby executables somehow.

I have the same error and I dont know what to do. Could you answer?

When I do ruby -v I see ruby 2.2.1

This command did not source the new bash_profile:

exec $SHELL

Fixed it with:

source ~/.bash_profile

On my server install Ubuntu 14.04 I had to append the paths to ~/.bashrc

In my case it didn’t work to export the path to .bash_profile. I had to do it in .bashrc instead and it worked fine.

This comment has been deleted

    For me, the step where I do rbenv install 2.2.1 leads to Installing ruby-2.2.1, and that process doesn’t seem to complete ever.

    It’s been running for half an hour now without success.

    Mitchell Anicas
    DigitalOcean Employee
    DigitalOcean Employee badge
    May 4, 2015

    The install process can take a long time. Try running the install in verbose mode, so you can see what’s going on:

    rbenv install -v 2.2.1
    

    Yeah, had already solved it by running in verbose mode combined with configuring proper KeepAlive, thanks!

    Install rbenv echo ‘export PATH=“$HOME/.rbenv/bin:$PATH”’ >> ~/.bash_profile bash_profile is error, bashrc is correct on ubuntu. Ubuntu Desktop note: Modify your ~/.bashrc instead of ~/.bash_profile. https://github.com/sstephenson/rbenv

    Getting an error while installing rails $ gem install rails -v 4.2.0 ERROR: While executing gem … (Errno::EACCES) Permission denied - /var/lib/gems

    please help !

    Mitchell Anicas
    DigitalOcean Employee
    DigitalOcean Employee badge
    June 3, 2015

    Did you already have ruby installed before following this tutorial? It sounds like the ownership of your files are incorrect. A quick fix would be to add yourself to the group that owns /var/lib/gems or use sudo. It’s better to fix the permissions though.

    I am running into same problem. how do I change permissions. Please help me out.

    No, I installed Ruby by following this tutorial only. I’ve fixed the problem of ownership of files. Thanks…!

    super minor, but in the Install rbenv section, you write:

    git clone git://github.com/sstephenson/rbenv.git .rbenv

    but (for robustness/just in case) it might be better as:

    git clone git://github.com/sstephenson/rbenv.git ~/.rbenv ???

    and great article, thank you!

    Mitchell Anicas
    DigitalOcean Employee
    DigitalOcean Employee badge
    August 17, 2015

    Hey there. The first command in the script (cd) changes the working directory to the home directory, so it should be fine either way.

    Thanks!

    Followed all instructions, using .bashrc at all times, and sourcing this file produces an error:

    source ~/.bashrc 
    The program 'rbenv' is currently not installed. You can install it by typing:
    sudo apt-get install rbenv
    

    However, .bashrc seems to be OK, and the executable is there:

    tail ~/.bashrc 
    export PATH="$HOME/.rbenv/bin:$PATH"
    eval "$(rbenv init -)"
    export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"
    
    ls ~/git/.rbenv/bin/
    rbenv  ruby-local-exec
    

    Don’t know what could be wrong here …

    Mitchell Anicas
    DigitalOcean Employee
    DigitalOcean Employee badge
    September 22, 2015

    Move .rbenv to your home directory:

    1. mv ~/git/.rbenv ~

    Thanks! That did it!

    Serge Lobatch
    DigitalOcean Employee
    DigitalOcean Employee badge
    September 24, 2015

    This comment has been deleted

      Serge Lobatch
      DigitalOcean Employee
      DigitalOcean Employee badge
      September 24, 2015

      Thanks, this helped me out too!

      You could update the following to ensure its installed in the correct location : from:

      cd
      git clone git://github.com/sstephenson/rbenv.git .rbenv
      

      to

      git clone git://github.com/sstephenson/rbenv.git ~/.rbenv
      

      jerry3746 about 5 years ago I installed Rails and experimented with it. Now I am ready to build an application with Rails. For 5 days now I have installed and uninstalled my Ubuntu 14.04 OS trying to get Rails to work on it. This morning once again I reinstalled my Ubuntu found your site and 30 minutes later I have a working Rails program. Thank you very much.

      How is this tutorial different from this one: https://www.digitalocean.com/community/tutorials/how-to-use-the-ruby-on-rails-one-click-application-on-digitalocean and which one is recommended? Should we install unicorn and nginx and all of that too?

      Thanks!

      Mitchell Anicas
      DigitalOcean Employee
      DigitalOcean Employee badge
      October 25, 2015

      This tutorial pretty much only sets up a development environment. The tutorial (and one-click image) that you linked is good if you want to actually deploy your application into production.

      Why ruby version is not being displayed while I run command: ruby -v

      When I ran : rbenv version, it shows 2.2.1 (set by /home/ati/.rbenv/version)

      Please let me know where did I wrong?

      Mitchell Anicas
      DigitalOcean Employee
      DigitalOcean Employee badge
      November 19, 2015

      Did you run rbenv global 2.2.1?

      On Ubuntu Server 14.04. Changing the path to ~/.bashrc allowed my ruby installation to be linked correctly.

      Also a note to the author, why not update the article to install v2.2.3? https://gorails.com/setup/ubuntu/14.04

      This tutorial is great, but just wanted to leave a small note: if you try to run it in the Rails One-Click App you’ll run into problems, as that app uses rvm.

      You’ll need to remove rvm with rvm implode .

      Man u a awesome you saved me

      Can someone please teach me how to run the rails server in the background, so when i close the console/ssh session, the server service will still be up? thank you in advance

      I’ve got an error by trying to run a Ruby app.

      root@discourse:~/project# rails server --binding=107.170.118.89
      => Booting WEBrick
      => Rails 4.2.6 application starting in development on http://107.170.118.89:3000
      => Run `rails server -h` for more startup options
      => Ctrl-C to shutdown server
      A server is already running. Check /root/project/tmp/pids/server.pid.
      Exiting
      root@discourse:~/project# http://107.170.118.89:3000
      -bash: http://107.170.118.89:3000: No such file or directory
      
      

      As of today, I had to do a few extra steps to get the latest ruby 2.3.1 built: #rbenv issue, curl issues with finding certificates sudo apt-get install ca-certificates sudo mkdir -p /etc/pki/tls/certs/ sudo ln -s /etc/ssl/certs/ca-certificates.crt /etc/pki/tls/certs/ca-bundle.crt

      The chris-lea repository for nodejs is pretty outdated, and I was getting an apt-get error when trying to do an update today. Here’s an alternative nodejs repository I found: https://github.com/nodesource/distributions#installation-instructions

      This comment has been deleted

        Ruby 2.2.3 failed to compile on my Ubuntu 14.04 server machine.

        I installed some additional libraries: sudo apt-get install autoconf bison libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libgdbm3 libgdbm-dev

        and installed Ruby 2.4.0 with success: rbenv install 2.4.0

        I installed a current version of nodejs:

        add nodejs version 7.x repo:

        curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash -

        install nodejs 7.x:

        sudo apt-get install -y nodejs

        Currently failing when I source .bashrc.

        : invalid option/bin/rbenv: line 2: set: - set: usage: set [-abefhkmnptuvxBCHP] [-o option-name] [–] [arg …] ‘: not a valid identifierv: line 3: unset: CDPATH /home/ray/.rbenv/bin/rbenv: line 4: $'\r': command not found /home/ray/.rbenv/bin/rbenv: line 15: syntax error near unexpected token $’{\r’’ 'home/ray/.rbenv/bin/rbenv: line 15: `abort() {

        Disregard - had to fix the scripts with dos2unix.

        The chris-lea repository for nodejs is pretty outdated, and I was getting an apt-get error when trying to do an update today. Here’s an alternative nodejs repository I found: https://github.com/nodesource/distributions#installation-instructions

        This comment has been deleted

          Ruby 2.2.3 failed to compile on my Ubuntu 14.04 server machine.

          I installed some additional libraries: sudo apt-get install autoconf bison libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libgdbm3 libgdbm-dev

          and installed Ruby 2.4.0 with success: rbenv install 2.4.0

          I installed a current version of nodejs:

          add nodejs version 7.x repo:

          curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash -

          install nodejs 7.x:

          sudo apt-get install -y nodejs

          Currently failing when I source .bashrc.

          : invalid option/bin/rbenv: line 2: set: - set: usage: set [-abefhkmnptuvxBCHP] [-o option-name] [–] [arg …] ‘: not a valid identifierv: line 3: unset: CDPATH /home/ray/.rbenv/bin/rbenv: line 4: $'\r': command not found /home/ray/.rbenv/bin/rbenv: line 15: syntax error near unexpected token $’{\r’’ 'home/ray/.rbenv/bin/rbenv: line 15: `abort() {

          Disregard - had to fix the scripts with dos2unix.

          The chris-lea repository for nodejs is pretty outdated, and I was getting an apt-get error when trying to do an update today. Here’s an alternative nodejs repository I found: https://github.com/nodesource/distributions#installation-instructions

          This comment has been deleted

            Ruby 2.2.3 failed to compile on my Ubuntu 14.04 server machine.

            I installed some additional libraries: sudo apt-get install autoconf bison libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libgdbm3 libgdbm-dev

            and installed Ruby 2.4.0 with success: rbenv install 2.4.0

            I installed a current version of nodejs:

            add nodejs version 7.x repo:

            curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash -

            install nodejs 7.x:

            sudo apt-get install -y nodejs

            Currently failing when I source .bashrc.

            : invalid option/bin/rbenv: line 2: set: - set: usage: set [-abefhkmnptuvxBCHP] [-o option-name] [–] [arg …] ‘: not a valid identifierv: line 3: unset: CDPATH /home/ray/.rbenv/bin/rbenv: line 4: $'\r': command not found /home/ray/.rbenv/bin/rbenv: line 15: syntax error near unexpected token $’{\r’’ 'home/ray/.rbenv/bin/rbenv: line 15: `abort() {

            Disregard - had to fix the scripts with dos2unix.

            The chris-lea repository for nodejs is pretty outdated, and I was getting an apt-get error when trying to do an update today. Here’s an alternative nodejs repository I found: https://github.com/nodesource/distributions#installation-instructions

            This comment has been deleted

              Ruby 2.2.3 failed to compile on my Ubuntu 14.04 server machine.

              I installed some additional libraries: sudo apt-get install autoconf bison libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libgdbm3 libgdbm-dev

              and installed Ruby 2.4.0 with success: rbenv install 2.4.0

              I installed a current version of nodejs:

              add nodejs version 7.x repo:

              curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash -

              install nodejs 7.x:

              sudo apt-get install -y nodejs

              Currently failing when I source .bashrc.

              : invalid option/bin/rbenv: line 2: set: - set: usage: set [-abefhkmnptuvxBCHP] [-o option-name] [–] [arg …] ‘: not a valid identifierv: line 3: unset: CDPATH /home/ray/.rbenv/bin/rbenv: line 4: $'\r': command not found /home/ray/.rbenv/bin/rbenv: line 15: syntax error near unexpected token $’{\r’’ 'home/ray/.rbenv/bin/rbenv: line 15: `abort() {

              The chris-lea repository for nodejs is pretty outdated, and I was getting an apt-get error when trying to do an update today. Here’s an alternative nodejs repository I found: https://github.com/nodesource/distributions#installation-instructions

              This comment has been deleted

                Ruby 2.2.3 failed to compile on my Ubuntu 14.04 server machine.

                I installed some additional libraries: sudo apt-get install autoconf bison libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libgdbm3 libgdbm-dev

                and installed Ruby 2.4.0 with success: rbenv install 2.4.0

                I installed a current version of nodejs:

                add nodejs version 7.x repo:

                curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash -

                install nodejs 7.x:

                sudo apt-get install -y nodejs

                Currently failing when I source .bashrc.

                : invalid option/bin/rbenv: line 2: set: - set: usage: set [-abefhkmnptuvxBCHP] [-o option-name] [–] [arg …] ‘: not a valid identifierv: line 3: unset: CDPATH /home/ray/.rbenv/bin/rbenv: line 4: $'\r': command not found /home/ray/.rbenv/bin/rbenv: line 15: syntax error near unexpected token $’{\r’’ 'home/ray/.rbenv/bin/rbenv: line 15: `abort() {

                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.