Skip to content

installing rails 3 1

Daniel Kehoe edited this page May 1, 2012 · 43 revisions

Read This Before Installing Rails 3.1

by Daniel Kehoe

Last updated 24 January 2012

See a New Article for Rails 3.2!

You’ve found an article that explains how to install Rails 3.1. But Rails 3.2 is the newest version!

You should follow this link to learn how to install Rails 3.2:

It offers detailed (and current) instructions on how to install the latest release of Rails with advice and troubleshooting tips.

Follow on Twitter Follow @rails_apps on Twitter for updates and timely Rails tips.

What Was New

You can read more about how the building blocks of the Rails platform work together by reading the article Managing Rails Versions and Gems. It explains the relationships among Ruby, RubyGems, Rails, Rake, Bundler, and gemfiles and offers some advice.

Rails 3.1.3

Rails 3.1.3 was released November 20, 2011, mainly to fix what broke in Rails 3.1.2 (see the announcement). Rails 3.1.2 was released November 17, 2011 to fix a number of small bugs and provide a security fix (detailed in the announcement).

Rails 3.1

Rails 3.1.0 was released August 31, 2011. Eight release candidates were released beginning May 21, 2011. Rails 3.1 introduced the asset pipeline which enables proper organization of CSS and JavaScript. Other features include HTTP streaming, default jQuery, reversible migrations, mountable engines, identity map, prepared statements, Rack::Cache on by default, forced SSL, role-based mass-assignment protection, has_secure_password, and custom serializers. See the Rails 3.1 release candidate announcement for details. There’s a Rails 3.1 Overview from Ryan Bates and a changelog for all the details. Yehuda Katz explains What’s Up With All These Changes in Rails?. The best overview of Rails 3.1 is Michael Hartl’s chapter on Rails 3.1 in his Ruby on Rails Tutorial book.

What You Need to Know: The greatest impact for developers moving from Rails 3.0 to 3.1 is the new location for CSS and JavaScript files and changes to the application layout file. See the official Rails 3.1 Asset Pipeline guide for details.

Rake

Rake is the build tool for Ruby. Rake version 0.9.2 was released on June 5, 2011 and followed by Rake version 0.9.2.2 on October 22, 2011. Rake 0.9.2.2 installs as part of Ruby 1.9.3.

Many gems depend on Rake and conflicting dependencies can be a problem, especially since the release of Rake version 0.9.0 (May 20, 2011). For an explanation, see David Chelimsky’s blog post. If needed, you can specify an earlier version of Rake in your gemfile to force the use of the Rake version specified in your gemfile. It’s good practice to use the command bundle exec rake instead of rake so you’ll use the version of Rake specified in your gemfile (or a dependency specified in the Gemfile.lock file) instead of the default version. For example, instead of rake db:migrate, run bundle exec rake db:migrate.

What You Need to Know: Make sure you are using Rake 0.9.2.2 (or newer) with gem update rake before installing Rails 3.1. And use bundle exec rake instead of rake.

RubyGems

RubyGems is a package management framework for Ruby. RubyGems 1.8.11 was released October 3, 2011. RubyGems 1.8.10 installs as part of Ruby 1.9.3.

With version 1.8.0 (released May 4, 2011), the RubyGems system gem began a series of rapid updates. Releases between 1.8.0 and 1.8.5 generated “noisy” deprecation warnings but most installed gems continued to work. RubyGems 1.8.5 eliminated most of the deprecation warnings. If you think a gem may be failing because of an incompatibility with the newest RubyGems implementation, you can troubleshoot by rolling back to an earlier RubyGems system gem with gem update --system 1.7.2 or gem update --system 1.3.7.

What You Need to Know: Use gem update --system to upgrade to the newest RubyGems system gem.

Update or Install?

Not recommended: You could run gem update rails for the newest version of Rails if you already have Rails installed.

Recommended: You should use rvm, the Ruby Version Manager, to create a new gemset for the new Rails version. Then you can switch between versions if necessary.

Installing Rails 3.1.3

Follow these instructions to set up Rails 3.1.3 as a platform for the Rails example apps or any other apps you may build.

Do You Ruby?

Ruby should be installed on your computer. The version doesn’t matter; you’ll install Ruby 1.9.3 using rvm.

Mac? Windows? Linux?

On a Mac, you’ll need to install XCode from the Mac App Store.

For Windows, start with RailsInstaller from Engine Yard.

For Ubuntu Linux, there are several useful guides:

Install or Update RVM

Use rvm, the Ruby Version Manager, to manage your Rails versions and create a gemset specifically for each application you build. If you encounter conflicting gem dependencies, you can isolate the errors by creating different gemsets with rvm.

The rvm website explains how to install rvm.

On a Mac, you may encounter issues with rvm and XCode 4.2 (or newer) because XCode 4.2 doesn’t use the standard gcc compiler. Here’s a suggested fix.

If you already have rvm installed, update it to the latest version.

$ rvm get latest
$ rvm reload
$ rvm -v

Install Ruby 1.9.3

Check for the current recommended version of Ruby. Ruby 1.9.3 patch level 0 was current when this was written.

If you don’t have it already, use rvm to install the recommended version of Ruby and make it your default:

$ rvm install ruby-1.9.3-p0
$ rvm --default use ruby-1.9.3-p0
$ ruby -v

RubyGems

The RubyGems package management system comes as part of a standard Ruby language installation.

What version of the RubyGems system is installed on your machine? Check with:

$ gem -v

Check for the newest version of RubyGems. It should be version 1.8.11 or newer.

Use gem update --system to upgrade the RubyGems system.

See the article Managing Rails Versions and Gems if you have problems with gem incompatibilities.

Create a Rails 3.1.3 Gemset

Create a default Rails 3.1.3 gemset. It’s advisable to create a new gemset for each application you build. But to get started, create just one new Rails 3.1.3 gemset as your default.

$ rvm ruby-1.9.3-p0@rails313 --create --default

To see a list of the gemsets you have installed:

$ rvm gemset list

And see a list of the gems included with the standard Ruby installation:

$ gem list

Update Rake

Rake version 0.9.2.2 comes with Ruby 1.9.3.

Make sure you are using the newest version of Rake before you install Rails 3.1.3.

$ gem update rake
$ rake --version

Install Rails 3.1.3

Now you can install Rails 3.1.3.

Check for the current version of Rails.

If you want the most recent stable release:

$ gem install rails
$ rails -v

If you want the newest beta version or release candidate, you can install with --pre.

$ gem install rails --pre
$ rails -v

Or, as an alternative to --pre, for the newest version:

$ gem install rails -v ">=3.1.3"
$ rails -v

Or you can get a specific version (sometimes the newest version is broken).

For example, if you want the Rails 3.1.0 final release, install with --version=3.1.0.

JavaScript Runtime for Ubuntu

For Rails 3.1, a JavaScript runtime is needed for development on Linux Ubuntu. It is not needed for Mac OS X or Windows.

For development on Linux Ubuntu, it’s best to install the Node.js server-side JavaScript environment:

sudo apt-get install nodejs

and set it in your $PATH.

If you don’t install Node.js, you’ll need to add this to the Gemfile for each Rails application you build:

gem 'therubyracer'

Generate a Rails App

You can create a test application:

$ rails new testapp

Switch to the application root directory to examine and test what you’ve built.

$ cd testapp

Quick Test

For a “smoke test” to see if everything runs, display a list of Rake tasks.

$ bundle exec rake -T

Remember, it’s good practice to run bundle exec rake instead of rake in case a gem specified in your gemfile (or a dependency in the Gemfile.lock file) relies on a version of Rake that’s different from the newest one you’ve installed.

More Information

See the Rails Guides for an introduction to building and running a Rails app.

See a list of resources for Rails if you’re just getting started.

Get a Starter App

The starter apps from the Rails Apps repository provide good examples of Rails 3.1.3 apps.

Plus, by using one of these starter apps, you can minimize the effort needed to stay current with changing Rails and gem versions.

You can see an Example Rails 3.1.3 Gemfile from the starter apps.

Starter Apps

Each app provides a set of useful, popular Rails gems integrated into a working application. Each example is known to work and can serve as your personal “reference implementation”. Each is an open source project. Dozens of developers use the apps, report problems as they arise, and propose solutions as GitHub issues. There is a tutorial for each one so there’s no mystery code.

Author Starter App Tutorial App Template Comments
Daniel Kehoe Devise, RSpec, Cucumber Tutorial App Template Uses Devise for authentication with ActiveRecord and SQLite for a database
Daniel Kehoe Twitter Bootstrap, Devise, CanCan Tutorial App Template Devise for authentication, CanCan for authorization, Twitter Bootstrap for CSS
Daniel Kehoe Startup Prelaunch Signup Tutorial App Template For a startup prelaunch signup site
Daniel Kehoe Devise, Mongoid Tutorial App Template Uses Devise for authentication with a MongoDB datastore
Daniel Kehoe OmniAuth, Mongoid Tutorial App Template OmniAuth for authentication with a MongoDB datastore
Daniel Kehoe Subdomains, Devise, Mongoid Tutorial App Template Basecamp-style subdomains with Devise and MongoDB

These starter apps were created with the Rails Apps Composer gem.

Install from an Application Template

You can use the files in the Rails Application Templates repository to build the starter apps.

Use the "rails new myapp -m" command to generate a Rails web application from a template.

You can add the -T -O flags to skip Test::Unit files and Active Record files.

See the README or tutorial to learn how to install, configure and run the starter application.

Deploy to Heroku

Heroku provides low cost, easily configured Rails application hosting. For your convenience, here are instructions for Deploying to Heroku with Rails 3.1.

Using the .rvmrc file for Project-Specific Gemsets

After you’ve created an rvm gemset that you’ll use for a project and begun to build the app, you can create an .rvmrc file in the application’s root directory. RVM recgonizes an .rvmrc file in a directory and loads the gemset specified inside.

Here’s how to create an .rvmrc file if you’re using a gemset named “ruby192@rails31”:

$ echo "rvm ruby192@rails31" > .rvmrc

Using an .rvmrc file means you’ll automatically be using the correct Rails and gem version when you run your application on your local machine. This works best if you create an rvm gemset specifically for your application.

Troubleshooting

Problems? Check the issues.

Problems with Rake

If you see:

The template ... could not be loaded.
Error: You have already activated rake 0.8.7, but your Gemfile requires rake 0.9.x.
Consider using bundle exec.

If you are using Ruby 1.9.2, you must update the standard Ruby installation from Rake 0.8.7 to Rake 0.9.2 (or newer) before using the application templates to generate a new Rails app. It’s advisable to update to Ruby 1.9.3 which will provide Rake 0.9.2.2.

Clone this wiki locally