Installing Ruby on Rails

The Apple Macintosh is the platform of choice for developing Ruby on Rails apps. However, the reality is that most of us struggling developers don’t have the money to spare in buying expensive Apple computers.

This section will cover how to install a Ruby on Rails development environment on Windows and Linux.

Installing on Windows

Download and use the latest RailsInstaller from the linked website. This installer already includes Ruby 1.9.3, SQLite 3 as well as Git and essential tools for building native extensions in Windows.

The latest stable version, 2.2.3, is using Rails 3.2 so you will have to manually call RubyGems, Ruby’s package manager in order to get the Rails version that we need. Open a terminal via the Command Prompt with Ruby and Rails shortcut and run the following command:

C:\Sites> gem install rails -v=4.0.5

This will also install documentation around the latter half of the installation and that may take some time to finish. If you’re in a hurry, you can safely skip that part by telling RubyGems to skip documentation:

C:\Sites> gem install rails -v=4.0.5 --no-ri --no-rdoc

RailsFTW

A smaller alternative to RailsInstaller would be RailsFTW. This installer does not contain Git and build tools, but it’s smaller, uses the faster Ruby 2.0, doesn’t require internet access, and includes MySQL support.

Don’t worry about these installers using slightly older versions of Rails; version 4.0.3 will still work with the examples in this book.

Installing on Linux

Installing RVM

There are several ways of installing Ruby in Linux. Here we will be using Ruby Version Manager (RVM) which will handle the installation for us. RVM requires Git, curl and essential build tools, while Ruby and SQLite have their own requirements which RVM will install for us when we install Ruby. That said, let’s install RVM’s prerequisites:

$ sudo apt-get install curl build-essential git-core 

(For the sake of simplicity, we’ll assume you’re using Ubuntu for the entire Installing on Linux section.)

After installing the required packages, we install RVM using the following command:

$ \curl -sSL https://get.rvm.io | bash -s stable

Open a new terminal window to startup RVM and run the following command to install the latest version of Ruby 2.1:

$ rvm install 2.1

After RVM installs Ruby and SQLite 3, you must now tell RVM to set the default Ruby version to 2.1 rather than the default system Ruby. (Note you may need to set your terminal to “Run command as a login shell” for the command below to work.)

$ rvm 2.1 --default

Installing Rails

Installing Rails is a matter of calling RubyGems, Ruby’s package manager. Thanks to RVM, we do not need to have admin rights to install Rails:

$ gem install rails -v=4.0.5 --no-ri --no-rdoc

This is the same command we used to update RailsInstaller to Rails 4.0.5.

(We don’t need to install SQLite for Ruby because Rails does this already when we create a new SQLite app.)

Installing NodeJS

At version 3.1, Rails introduced the Asset Pipeline which make serving asset files (e.g. images, CSS, JS, etc) more reliably in addition to providing a means to insert preprocessing (e.g. minifying, compression) for these files. Because of this, a new Rails 4.0 app will by default require a JavaScript runtime installed along with Rails.

Linux users can install a JavaScript runtime in the form of NodeJS:

$ sudo apt-get install nodejs

Windows XP/Vista/7 users already have a JavaScript runtime as part of the OS so they don’t need to install anything aside from RailsInstaller or RailsFTW. Windows 8’s JavaScript runtime, on the other hand, is incompatible with Rails so you also need to install NodeJS from the linked site above.

Linux on Windows

Compared to Mac OS X and Linux, certain Rails commands are noticeably slower in Windows. This is more pronounced once you start using RSpec.

If your computer is fairly new (i.e. multi-core processor and 2 or more GB of RAM are cheap nowadays), you might want to consider running Linux in a virtual machine; Ruby can run faster in Linux even though it’s just a virtual machine.

There are free virtualization software available on the Internet. The most popular ones are Oracle VirtualBox and VMware Player.

As for the Linux distribution, we recommend Ubuntu so that you could just follow the installation instructions above for installing Ruby on Rails.

IDE

ASP.NET and Java EE developers might be surprised to know that the most popular “IDE” for Ruby on Rails is TextMate, a relatively simple text editor on the Mac. As we shall see later, there are some reasons why Ruby on Rails does not require full-fledged IDEs for development.

Any text editor will do fine for Ruby on Rails development. For Windows, Redcar and Sublime Text 2 are good choices because they’re free (as of this writing), has Ruby syntax highlighting, and can handle Linux and Mac line breaks properly (unlike Notepad). Linux users can use vim or emacs; both editors have steep learning curves but they can be more productive than IDEs once you get the hang of them.

If you still insist on using an IDE, there’s Aptana RadRails and JetBrains RubyMine.

API Documentation

This document is meant to be more of a training manual than a reference manual. It will not contain detailed descriptions of each function available in Ruby on Rails, instead, you will have to refer to the official API documentation for those details.

Apart from the official Rails API docs, there are other alternative Ruby and Rails documentation sites that provide aditional features apart from basic lookup e.g. ApiDock and Omniref.