How to install Ruby on Rails on Ubuntu

Just a few commands will get you started with Ruby on Rails on Ubuntu. If you want to use a different Linux distribution, most of this tutorial will still be correct. You will have to translate all of the apt-get statements and package names to your distribution of choice. Configuration file paths and layouts may also differ. If you would like more information about how to set up a Linux VM using our system, please visit our other tutorials.

Step 1: Install Ruby Version Manager

First, fire up a fresh terminal and ensure your package lists are up-to-date:

sudo apt-get update  

Now we need to use curl. If you’re running one of our pre-installed images then you can skip this step, but if you don’t already have it…

sudo apt-get install curl  

…which will allow us to install Ruby Version Manager – the tool we’ll use to get the latest version of Ruby.

curl -L https://get.rvm.io | bash -s stable  

Now we need to load RVM and make sure it’s added to .bashrc. The filepath will change according to user, but if you’re logged in as root, run these two commands:

echo '[[ -s "/usr/local/rvm/scripts/rvm" ]] && source "/usr/local/rvm/scripts/rvm"' >> ~/.bashrc  
source ~/.bashrc  
Info If, on the other hand, you’re logged in as a standard user, it should be simpler:
source ~/.rvm/scripts/rvm  

RVM’s dependencies can then be installed with one command.

rvm requirements  

Step 2: Install Ruby

Now we use RVM to install Ruby itself…

rvm install ruby  

…and tell it to use the latest version (the one you’ve just downloaded) by default:

rvm use ruby --default  

Step 3: Install RubyGems

rvm rubygems current  

RubyGems is a package manager for Ruby, similar to EasyInstall for Python.

Step 4: Install Rails

gem install rails  

Step 5: Create your application

rails new blog  

This will create the skeleton of a new application called “blog”, with all the necessary files and directories. It then runs the “bundle install” command automatically upon completion, which might take some time to execute.

Step 6: Start your server

rails server  

This will start a server listening on port 3000.

Beware !! You might receive an execjs error at this point, as compiling CoffeeScript to JavaScript requires a Javascript runtime. You can either get around this by installing node.js or by navigating to the Gemfile file in the top of your new “blog” directory and using a text editor to uncomment the line containing ‘therubyracer’ that will look something like this:
gem 'therubyracer', platforms: :ruby  

To revel in your creation, go to http://your-IP-address:3000 and you’ll see:Ruby Welcome Screen

Info You can find your IP address under the server information on your Elastichosts control panel. or by checking ifconfig, or by using:
curl canihazip.com | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'  

To get Rails saying “Hello”, you need to create at minimum a controller and a view.

Step 7: Hello World

rails generate controller welcome index  

This creates an index.html.erb file and route to it (This is the view), and a controller (called welcome_controller.rb). It also installs, amongst other things, a helper, SCSS and CoffeeScript files, but if we open the index file in a text editor:

nano app/views/welcome/index.html.erb  

and write something appropriate like:

Hello World  

we are almost there. All we need to do is make sure this file will be pointed to when users navigate to your root URL, in our case, http://your-IP-address:3000, instead of the “Welcome aboard” page. We do this by changing the config/routes.rb

nano config/routes.rb  

Uncomment the line containing

root welcome#index  

What this does is well explained by the Rubyonrails.org guide – “The root “welcome#index” tells Rails to map requests to the root of the application to the welcome controller’s index action and get “welcome/index” tells Rails to map requests to http://your-ip:3000/welcome/index to the welcome controller’s index action. This was created earlier when you ran the controller generator (rails generate controller welcome index).”

Step 8: Bask in the glory of your own awesomeness

Now just navigate to http://your-IP-address:3000 once again to see your Ruby on Rails application in action!

Further reading:

http://guides.rubyonrails.org/ http://ruby.railstutorial.org/chapters/a-demo-app