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.
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.
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.
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.)
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):
gem install railsSimonMacBook: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
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.)
If you’re ever wondering which gems (and which versions of gems) are
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
gem server, and
visiting the URL (usually http://localhost:8808)
that command reports. When you’re done, you can turn off the server with
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:
run bundle installFetching source index for https://rubygems.org/ Using rake (0.9.2.2) 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.
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.
rails server=> Booting WEBrick => Rails 3.2.1 application starting in development on http://0.0.0.0:3000 => 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.
rails server binds
localhost at 0.0.0.0 or
127.0.0.1, 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.
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
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.
What’s the name of the Ruby application packaging utility and how do you install Rails with it?
In what instances would you avoid WEBrick?
Why should you install a particular version of Ruby on your platform when Ruby already comes installed?
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
WEBrick is great for testing your Rails applications, but definitely not the best choice for deployments where performance matters.
Rails only works well on certain versions of Ruby, including 1.8.7 and 1.9.2.