O'Reilly logo

Learning Rails 3 by Edd Dumbill, Simon St. Laurent, Eric J Gruber

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 1. Starting Up Ruby on Rails

Before you can use Rails, you have to install it. Even if it’s already installed on your computer, you may need to consider upgrading it. In this chapter, we’ll take a look at some ways of installing Ruby, Rails, and the supporting infrastructure. Please feel very welcome to jump to whatever pieces of this section interest you and skip past those that don’t. Once the software is working, we’ll generate the basic Rails application, which will at least let you know if Rails is working. However you decide to set up Rails, in the end you’re going to have a structure like that shown in Figure 1-1.

The many components of a Rails installation

Figure 1-1. The many components of a Rails installation


All of these options are free. You don’t need to spend any money to use Rails, unless maybe you feel like buying a nice text editor.

The Rails welcome page

Figure 1-2. The Rails welcome page

If You Run Windows, You’re Lucky

Windows users (at last) can get a basic installation of Rails and supporting tools—everything you need to use this book—far more easily than anyone else. EngineYard’s Rails Installer, which you can get at http://railsinstaller.org/ , provides all the key components in a one-click installation. Visit the site, download the installer, and watch the video; after that, you should be ready to move ahead to Starting Up Rails.

Really, it’s that easy! (Well, except that you may have to tell Windows Defender not to block the port Rails uses to present the site. It’s also possible that you’ll have to install developer tools on newer versions of Windows.)


As this book was going to print, an initial version of RailsInstaller appeared for Mac OS X at http://railsinstaller.org/. Macintosh users may also be lucky now. Linux users still await a “coming soon” version.

Getting Started at the Command Line

Installing Rails by hand requires installing Ruby (preferably 1.9.2 or later), installing Gems, and then installing Rails. You will eventually also need to install SQLite, MySQL, or another relational database, though SQLite is already present on the Mac and in many Linux distributions.


As this book was going to print, an initial version of RailsInstaller appeared for Mac OS X at http://railsinstaller.org. Macintosh users may also be lucky now, while Linux users still await a “coming soon” version.

Ruby comes standard on a number of Linux and Macintosh platforms. To see whether it’s there, and what version it has, enter ruby -v at the command prompt. You’ll want Ruby 1.8.7 or 1.9.2, so you may need to update it to a more recent version:

  • On Mac OS X, Snow Leopard (10.6) and Lion (10.7) include Ruby 1.8.7, and Leopard (10.5) includes Ruby 1.8.6, but the previous version of OS X included Ruby 1.8.2. If you’re on Tiger (10.4) or an earlier version of OS X, you’ll need to update Ruby itself, a challenge that’s beyond the scope of this book. You may want to investigate MacPorts, and the directions at http://nowiknow.wordpress.com/2007/10/07/install-ruby-on-rails-for-mac/. For a more comprehensive installation, explore http://paulsturgess.co.uk/articles/show/46/. (You should ignore the versions of Rails installed with OS X - they’re guaranteed to be out of date.)

  • Most distributions of Linux include Ruby, but you’ll want to use your package manager to make sure it’s updated to 1.9.2. Some, notably Ubuntu and Debian, will name the gem command gem1.9.

  • For Windows, unless you’re a hardened tinkerer, it’s much easier to use Rails Installer. If you’re feeling strong, the One-Click Ruby Installer (http://rubyinstaller.rubyforge.org/) is probably your easiest option, though there are other alternatives, including Cygwin (http://www.cygwin.com/), which brings a lot of the Unix environment to Windows.

A saner long-term approach to installing Ruby and Rails also includes installing rvm, the Ruby Version Manager, which frees you from having to worry about what version of Ruby your system decided it should have as well as giving you better options for managing a clean work environment. You can find out more about rvm at http://rvm.beginrescueend.com/. (It was created by Wayne E. Seguin, the same person who created Rails Installer.) If that doesn’t seem right to you, you can also find out more on how to install Ruby on a variety of platforms, see http://www.ruby-lang.org/en/downloads/.


If rvm isn’t for you, you may also want to explore rbenv (https://github.com/sstephenson/rbenv/), a much smaller and simpler approach to switching between versions of Ruby.

RubyGems (often just called Gems) is also starting to come standard on a number of platforms, most recently on Mac OS X Leopard and Snow Leopard, but if you need to install Gems, see the RubyGems User Guide’s instructions at http://www.rubygems.org/read/chapter/3/.


If you use MacPorts, apt-get, or a similar package installer, you may want to use it only to install Ruby, and then proceed from the command line. You certainly can install Gems and Rails with these tools, but Gems can update itself, which can make for very confusing package update issues.

Once you have RubyGems installed, Rails and its many dependencies are just a command away (though the output has grown more verbose with every version of Rails):

~ simonstl$ gem install rails
	SimonMacBook:living_book_2010_rails_3 simonstl$ gem install rails
	Fetching: i18n-0.6.0.gem (100%)
	Fetching: [many more]..
	Depending on your version of ruby, you may need to install ruby rdoc/ri data:

	<= 1.8.6 : unsupported
	 = 1.8.7 : gem install rdoc-data; rdoc-data --install
	 = 1.9.1 : gem install rdoc-data; rdoc-data --install
	>= 1.9.2 : nothing to do! Yay!
	Fetching: railties-3.2.3.gem (100%)
	Fetching: bundler-1.0.22.gem (100%)
	Fetching: rails-3.2.3.gem (100%)
	Successfully installed i18n-0.6.0
	Successfully installed multi_json-1.1.0
	Successfully installed activesupport-3.2.3
	Successfully installed builder-3.0.0
	Successfully installed activemodel-3.2.3
	Successfully installed rack-1.4.1
	Successfully installed rack-cache-1.2
	Successfully installed rack-test-0.6.1
	Successfully installed journey-1.0.1
	Successfully installed hike-1.2.1
	Successfully installed tilt-1.3.3
	Successfully installed sprockets-2.1.2
	Successfully installed erubis-2.7.0
	Successfully installed actionpack-3.2.3
	Successfully installed arel-3.0.0
	Successfully installed tzinfo-0.3.31
	Successfully installed activerecord-3.2.3
	Successfully installed activeresource-3.2.3
	Successfully installed mime-types-1.17.2
	Successfully installed polyglot-0.3.3
	Successfully installed treetop-1.4.10
	Successfully installed mail-2.4.4
	Successfully installed actionmailer-3.2.3
	Successfully installed thor-0.14.6
	Successfully installed rack-ssl-1.3.2
	Successfully installed json-1.6.5
	Successfully installed rdoc-3.12
	Successfully installed railties-3.2.3
	Successfully installed bundler-1.0.22
	Successfully installed rails-3.2.3
	30 gems installed
	Installing ri documentation for i18n-0.6.0...
    [lots more documentation notices]

You may need to use sudo, which gives your command the power of the root (administrative) account, if you’re working in an environment that requires root access for the installation—otherwise, you can just type gem install rails. That will install the latest version of Rails, which may be more recent than 3.2.3, as well as all of its dependencies. gem install rails will install the latest official release of Rails, which at present is 3.2.3. It will not install any Rails betas. (To see which version of Rails is installed, enter rails -v at the command line.)

You may also need to install the sqlite3 gem, which isn’t automatically installed by the Rails gem but is needed for development. That’s gem install sqlite3.

If you’re ever wondering which gems (and which versions of gems) are installed, type gem list --local. For more information on gems, just type gem, or visit http://rubygems.rubyforge.org/.


You can see the documentation that gems have installed by running the command gem server, and visiting the URL (usually http://localhost:8808) that command reports. When you’re done, you can turn off the server with Ctrl-C.

Once you have Rails installed, you can create a Rails application easily from the command line. Here’s what it looks like in its extended glory, but you don’t need to read it every time:

~ $ rails new hello01
      create  README.rdoc
      create  Rakefile
      create  config.ru
      create  .gitignore
      create  Gemfile
      create  app
      create  app/assets/images/rails.png
      create  app/assets/javascripts/application.js
      create  app/assets/stylesheets/application.css
      create  app/controllers/application_controller.rb
      create  app/helpers/application_helper.rb
      create  app/mailers
      create  app/models
      create  app/views/layouts/application.html.erb
      create  app/mailers/.gitkeep
      create  app/models/.gitkeep
      create  config
      create  config/routes.rb
      create  config/application.rb
      create  config/environment.rb
      create  config/environments
      create  config/environments/development.rb
      create  config/environments/production.rb
      create  config/environments/test.rb
      create  config/initializers
      create  config/initializers/backtrace_silencers.rb
      create  config/initializers/inflections.rb
      create  config/initializers/mime_types.rb
      create  config/initializers/secret_token.rb
      create  config/initializers/session_store.rb
      create  config/initializers/wrap_parameters.rb
      create  config/locales
      create  config/locales/en.yml
      create  config/boot.rb
      create  config/database.yml
      create  db
      create  db/seeds.rb
      create  doc
      create  doc/README_FOR_APP
      create  lib
      create  lib/tasks
      create  lib/tasks/.gitkeep
      create  lib/assets
      create  lib/assets/.gitkeep
      create  log
      create  log/.gitkeep
      create  public
      create  public/404.html
      create  public/422.html
      create  public/500.html
      create  public/favicon.ico
      create  public/index.html
      create  public/robots.txt
      create  script
      create  script/rails
      create  test/fixtures
      create  test/fixtures/.gitkeep
      create  test/functional
      create  test/functional/.gitkeep
      create  test/integration
      create  test/integration/.gitkeep
      create  test/unit
      create  test/unit/.gitkeep
      create  test/performance/browsing_test.rb
      create  test/test_helper.rb
      create  tmp/cache
      create  tmp/cache/assets
      create  vendor/assets/javascripts
      create  vendor/assets/javascripts/.gitkeep
      create  vendor/assets/stylesheets
      create  vendor/assets/stylesheets/.gitkeep
      create  vendor/plugins
      create  vendor/plugins/.gitkeep
         run  bundle install
Fetching source index for https://rubygems.org/
Using rake ( 
Using i18n (0.6.0) 
Using multi_json (1.1.0) 
Using activesupport (3.2.1) 
Using builder (3.0.0) 
Using activemodel (3.2.1) 
Using erubis (2.7.0) 
Using journey (1.0.1) 
Using rack (1.4.1) 
Using rack-cache (1.1) 
Using rack-test (0.6.1) 
Using hike (1.2.1) 
Using tilt (1.3.3) 
Using sprockets (2.1.2) 
Using actionpack (3.2.1) 
Using mime-types (1.17.2) 
Using polyglot (0.3.3) 
Using treetop (1.4.10) 
Using mail (2.4.1) 
Using actionmailer (3.2.1) 
Using arel (3.0.0) 
Using tzinfo (0.3.31) 
Using activerecord (3.2.1) 
Using activeresource (3.2.1) 
Using bundler (1.0.22) 
Using coffee-script-source (1.2.0) 
Using execjs (1.3.0) 
Using coffee-script (2.2.0) 
Using rack-ssl (1.3.2) 
Using json (1.6.5) 
Using rdoc (3.12) 
Using thor (0.14.6) 
Using railties (3.2.1) 
Installing coffee-rails (3.2.2) 
Installing jquery-rails (2.0.0) 
Using rails (3.2.1) 
Using sass (3.1.15) 
Installing sass-rails (3.2.4) 
Using sqlite3 (1.3.5) 
Using uglifier (1.2.3) 
Your bundle is complete! Use `bundle show [gemname]` to see where a 
bundled gem is installed.

This also gets longer and longer with each new version of Rails. Also, the bundle install piece may pause for a long moment.


Rails application directories are just ordinary directories. You can move them, obliterate them and start over, or do whatever you need to do with ordinary file-management tools. Each application directory is also completely independent—the general “Rails environment” just generates these applications.

Starting Up Rails

To start Rails, you’ll need to move into the directory you just created—cd hello01—and then issue your first command to get the WEBrick server busy running your application:

~ $ rails server
=> Booting WEBrick
=> Rails 3.2.1 application starting in development on
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2012-02-20 08:48:06] INFO  WEBrick 1.3.1
[2012-02-20 08:48:06] INFO  ruby 1.9.2 (2010-12-25) [x86_64-darwin10.5.0]
[2012-02-20 08:48:06] INFO  WEBrick::HTTPServer#start: pid=89377 port=3000

Rails is now running, and you can watch any errors it encounters through the extensive logging you’ll see in this window.


By default, rails server binds only to localhost at or, and the application isn’t visible from other computers. Normally, that’s a security feature, not a bug, though you can specify an address for the server to use with the -b option (and -p for a specific port) if you want to make it visible.

For more details on options for using rails server, just enter rails server -h.

If you now visit http://localhost:3000, you’ll see the same welcome screen shown previously in Figure 1-2. When you’re ready to stop Rails, you can just press Ctrl-C.


You frequently can leave Rails running while coding. In development mode, you can make many changes to your application with the server running, and you won’t have to restart the server to see them. If you change configuration, add scopes, or install gems, though, you’ll need to restart.

WEBrick (http://www.webrick.org/) is written in Ruby and bundled with recent releases of Ruby. It’s very convenient for Ruby development, with or without Rails. It’s an excellent testing server, but not designed for large scale deployment.

If you’ve never used Ruby before, now would be a good time to explore Appendix A, which teaches some key components of the language inside of a very simple Rails application.


Depending on how you set up your Rails environment and how you use Bundler, described in Chapter 17, you may need to preface your calls to rails, rake, and similar mechanisms with bundle exec to make sure you’re running exactly the version of the tools you expect to be running. If this seems like a lot of extra typing, visit http://robots.thoughtbot.com/post/15346721484/use-bundlers-binstubs to learn about binstubs, a way to avoid this.

Test Your Knowledge


  1. What’s the name of the Ruby application packaging utility and how do you install Rails with it?

  2. In what instances would you avoid WEBrick?

  3. Why should you install a particular version of Ruby on your platform when Ruby already comes installed?


  1. RubyGems, or just “gems,” which is run with the gem command, is Ruby’s application packager. To install the latest version of Rails and all its dependencies, just type gem install rails.

  2. WEBrick is great for testing your Rails applications, but definitely not the best choice for deployments where performance matters.

  3. Rails only works well on certain versions of Ruby, including 1.8.7 and 1.9.2.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required