In this tutorial we’re going to show you how to get Ruby on Rails installed on your Ubuntu 14.04 stack. Ruby on Rails is a popular model-view-controller web development framework that emphasizes DRY (don’t repeat yourself) and CoC (convention over configuration). If you don’t know what these terms mean, don’t fret — Rails is especially popular with web developers because its easy to get a project started and because there is a large community to help you out when you get stuck. Without further ado, let’s get started.

Prerequisites

We assume that you have a non-root user set up with sudo privileges on a fresh Ubuntu 14.04 Stack. Check out our ‘Getting Started With Your Stack‘ tutorial if you haven’t already. We’re going to be installing rbenv, ruby, and ruby on rails. rbenv is a ruby version management tool that allows you to use multiple versions of ruby between different projects without the trouble of having to manage a million different versions in your executable directory. You simply have to specify which version of ruby you want to use by running ‘rbenv local RUBY_VERSION’ in the directory in which you wish to execute ruby commands. Let’s begin by updating our package list installing a few packages that rbenv needs to work properly.

sudo apt-get update
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 ruby-build ruby-dev git

Installing rbenv and Ruby

Now we need to install rbenv. Execute the following commands on your stack

cd
git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
exec $SHELL

Now, if you enter in type rbenv into your command prompt, you’ll be echoed a few lines of Bash that let you know that the installation proceeded correctly.

Now that you have rbenv installed, we can install a number of different ruby versions if needed. For this tutorial, we’ll be using the latest version of ruby (ruby 2.2.2)

rbenv install -v 2.2.2
rbenv global 2.2.2

The rbenv global 2.2.2 command tells rbenv that we want to use ruby version 2.2.2 globally throughout our system. If we wanted to use version 2.2.1 in some other folder, we would have to first cd into the directory, run rbenv install 2.2.1, followed by rbenv local 2.2.1. To see which versions of ruby you have available to you, run rbenv versions.

Now verify that you’re running ruby 2.2.2 by typing ruby -v into your command line.

Installing Rails

To speed up the rails installation process, run the following command:

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

This tells ruby not to generate local documentation for every ruby gem that you install. It will save a lot of time when installing any gems that you work with during the development of your project. Lets continue and install the ‘bundler’ gem, which helps you manage your project dependencies.

gem install bundler

Now we’re ready to install Ruby on Rails:

gem install rails -v 4.2.0
rbenv rehash

Every time you install a new gem that you must interact with via command line, it is a good idea to run the command ‘rbenv rehash’. Since rbenv has the functionality to manage different versions of ruby and thus different gems across many directories, it must appropriately update its knowledge of new versions of ruby and new gems. This is called ‘rehashing’.

Check to see if Rails has been correctly installed:

rails -v

This command should output ‘Rails 4.2.0′ if the installation proceeded correctly. Now that we have Rails installed we should install a Javascript runtime which Rails uses for some of its features. We’ll install nodejs.

sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs

That’s it! We should have a working installation of Rails available to us now. If you want to test whether it worked run the following commands

cd ~
rails new testing
cd testing
rake db:create
rails server --binding=YOUR_STACKS_IP_ADDRESS

This creates a rails application named ‘testing’ in your home directory, creates the database that rails uses for its models, sets the IP address by which you can access your rails web app, and starts the server (on port 3000 by default). If you point your browser to http://YOUR_STACKS_IP_ADDRESS:3000 you should see the default welcome page provided by Rails. Almost all of your Rails applications will begin with the rails new command. It sets up the directory structure and scaffolds a bare-minimum Rails application that you can begin developing.

Final Words

Congratulations! You have a working Rails environment configured on your server and are ready to begin developing. Check out the official Rails documentation for more information on how to develop in Rails. From all of us at Stack Harbor, ahoy!