Ruby on Rails: Introduction and Setup
What is Ruby on Rails?
Before we start “riding rails” – let’s get to grips with what the hell we are doing.Ruby is a lightweight, high level programming language similar to Python and Perl. It’s very object oriented,is open source and is very easy to learn. So easy to learn in fact, that instead of learning Ruby and then Rails – we are going to go straight in and learn different bits of Ruby as necessary (think of these tutorials almost as “applied tutorials” – we’ll be learning things as we go along to create cool stuff).
Ruby on Rails (often shortened to RoR or just Rails) is an open source application framework for Ruby. This means it provides a structure for creating websites and applications using Ruby (and some other bits).
Rails is designed to make web application development very easy. It has a “convention over configuration” principle which means that the programmer only has to write code for areas that are not considered as “standard”. Not only does this make programming a Rails application extremely fast, but it also makes the code and application structure relatively easy to understand.
The only real downsides with Rails are that not all web hosts support Ruby on Rails applications (although there obviously are a lot that do) and that some would argue that it doesn’t scale well when there are millions of simultaneous users using the application (although there are workarounds for this).
Setting Up (Ruby and Rails)
To work in Ruby on Rails – we need to setup Ruby (+ RubyGems) and Rails. Ruby is the first thing we have to setup.Setting up Ruby is different on different platforms and although is arguably more difficult to setup on Mac and Linux – the Mac and Linux are however much more flexible (with RVM (Ruby Version Manager)).
Mac and Linux
Setting up Ruby on Rails on a Mac or Linux machine is very easy. There is also an amazing application called RVM that we are going to use, which allows us to manage and switch between multiple Ruby and Rails environments.To install RVM (We’ll actually want to do this first) simply run the following command in the terminal:
bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)
After the install has completed, you should be instructed to add a line to your bashrc file. On OS X (Mac) this file is in the etc folder and named “bashrc” but on Linux this can vary (you may want to Google where yours is located if you don’t know) – Simply add the line that the terminal told you to add to this file. For me, it was the following (try this if you have issues after finishing the RVM instructions):
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function
Once you’ve done this, you’ll probably want to run the file to make the changes to your bashrc – you can do this by typing “source” followed by your bashrc file name (In the case of OS X this will be “bashrc”) while in the same directory as it. Now we should check that RVM has installed correctly. To do this we open a new terminal window and type “rvm –v”. If a version number is returned, then RVM is installed correctly – otherwise, try and re-follow the steps above and do some troubleshooting to get RVM installed properly.
Now that RVM is installed – we have to install a version of Ruby that we’re going to want to use in RVM. We can do this via the following command:
rvm install ruby-1.9.2-p290
In this case, I’ve chosen Ruby 1.9.2 p290 (it’s the latest version when I’m writing this) however you might want to install the latest version of Ruby that's available when your reading this.
Let’s also set this Ruby version to the default for RVM usage:
rvm --default use 1.9.2
Now Ruby is installed – let’s create a gemset (certain environment with versions of Ruby and Rails) that will hold our main RoR environment. At the time of writing this – the most recently version of Rails is 3.1 and so I will use this in my gemset name (you may or may not want to use the latest version of rails when you’re reading this (I’ll be teaching with rails 3.1 though so you might just want to install this):
rvm 1.9.2@rails3.1 --create
Now just confirm that it created properly we can list our gemsets:
rvm gemset list
If the gemset you created appears then we’ve set it up correctly!
Before we install rails (In my case, I’ll be installing 3.1.1.rc1 – the latest Rails 3.1 version) we should make sure we have the latest version of the rake gem as this is vital to rails 3.1 working properly:
gem update rake
Ok. So now it’s time to install rails (finally!). I’m going to use the “--pre” parameter to install the release candidate version (since at the time of writing this, Rails 3.1 hasn’t been fully released yet), but you may or may not want to use the latest version of rails when you’re reading this. Note: If this tutorial becomes really outdated, you may want to specify a version number with the “--version” parameter (for example “--version=3.1.1.rc1”).
The command (with the –pre parameter) is as follows:
gem install rails --pre
After this has run – that’s it!
Let’s just double check everything has installed by typing the following:
gem list
If the rails gem and rake gems appear and are all up to date – then you’ve configured your rails environment successfully!
Windows
Setting up Ruby on Rails for Windows is a bit different (and arguably easier).To start off with we need to install Ruby. We can do this using the Ruby Installer at the following URL and downloading the version you want (probably the latest): http://rubyinstaller.org/
After going through the graphical installation process (it’s a simple installer – just keep pressing next and the like the progress) then we need to install rails.
Just before we do this we should make sure we have the latest version of the rake gem to avoid issues with rails. We can do this by opening up the command prompt (Go to run and then type “cmd” or in Windows Vista or above you can just type “cmd” into the search bar and run it) and typing in the following:
gem update rake
With this latest version we should now be able to install Rails. I’m going to use the “--pre” parameter to install the release candidate version (since Rails 3.1 is still in release candidate while I’m writing this tutorial), but you may or may not want to use the latest version of rails when you’re reading this. Note: If this tutorial becomes really outdated, you may want to specify a version number with the “--version” parameter (for example “--version=3.1.1.rc1”).
The command (with the --pre parameter) is as follows:
gem install rails --pre
That’s it! We now have our rails environment all setup for work!
We should probably just verify that everything worked OK first by running:
gem list
If the rails gem and rake gems appear and are all up to date – then you’ve configured your rails environment successfully!
Terminology
Ok – so before we’ve even begun creating anything, I’ve already started throwing around some terminology and names of different things. I feel I should just explain some things in the following few paragraphs.RubyGems is a package manager for Ruby, which provides a standard for distributing Ruby programs and libraries.
Downloading a gem is just downloading some code to your environment that can do stuff. We’ll learn more about gems when we get onto them (actually using them in our applications!).
It’s also worth noting that any command that I specify to run in Terminal (or command prompt) that begins with “gem” is to do with RubyGems. For example the “gem update X” command, updates gem X is an update is available.
Back to Ruby on Rails

