Last updated 8 April 2014
Learn tips and tricks for managing Rails and Ruby gems. Relieve your headaches when versions collide.
This is a guide for developers using the example apps from the Rails Apps repository. Others may find it helpful as well.
Before you install one of the Rails example apps, there are a few things you should know about managing the pieces of the Rails platform. Often, everything “just works,” but when it doesn’t, here’s what you need to review. Read on if you’d like to know how all the moving parts work together.
See Installing Rails for detailed instructions and advice.
The Ruby on Rails platform is a living ecosystem: a community of developers, their projects, and complex dependencies among their projects. At times (often before and after the release of a major new version of Rails) the ecosystem enters a state of disequilibrium and important pieces no longer work together easily. These times of disequilibrium are valuable for the ecosystem because, as a whole, developers innovate and Rails improves during these times.
For you as a developer, it may feel like a trainwreck. You productivity will stall as you try to sort out what is broken and how to fix it. But you’ll find help here.
The example apps from the Rails Apps repository help you keep current with changing Rails and gem versions.
Each example 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. Many developers use the apps, report problems as they arise, and propose solutions as GitHub issues.
These examples provide a working Rails app with a detailed tutorial that can be used to create a starter app. Each of these example apps can be created using the Rails Composer tool (“like the ‘rails new’ command on steroids”):
|Example Applications for Rails 4.1||Tutorial||Comments|
|Learn Rails||Learn Ruby on Rails||introduction to Rails for beginners|
|Rails and Bootstrap||Rails Bootstrap Tutorial||starter app for Rails and Bootstrap|
|Rails and Foundation||Rails Foundation Tutorial||starter app for Rails and Zurb Foundation|
|OmniAuth and Rails||OmniAuth Tutorial||OmniAuth for authentication|
|Devise and Rails||Devise Tutorial||Devise for authentication|
|Pundit and Rails||Rails Pundit Tutorial||Pundit for authorization|
Ruby is the programming language used for Rails projects.
What version of Ruby is installed on your machine? Check with:
$ ruby -v
For the example apps it should be version 2.1.2.
You can check for the current recommended version of Ruby.
See instructions for Installing Rails to install a newer version of Ruby if necessary.
RubyGems is a package management framework for Ruby. A gem is a packaged Ruby application or library. You can read more about gems if you wish. Gems come from a wonderful pool of open source love but can be a headache to track and integrate.
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
For the example apps it should be version 1.8.17 or newer.
The RubyGems package management system is part of a standard Ruby language installation. It can be updated by installing the rubygems-update gem.
You can check for the current rubygems-update gem.
gem update --system to upgrade the RubyGems system.
As of release 1.8.0 (4 May 2011), the RubyGems system gem began a series of rapid updates (with controversy and a promised resolution). The example apps all work with version 1.8.15 but if you install a gem and find it doesn’t work, roll back to an older version of RubyGems:
gem update --system 1.7.2
gem update --system 1.3.7
Rails itself is a gem, which means it can be installed and updated like any other gem.
What version of Rails is installed on your machine? Check with:
$ rails -v
You can check for the newest released Rails version.
For the example apps it should be version 3.2.
See instructions for Installing Rails 3.2 to install or update Rails. I recommend that you create a new gemset using rvm before installing a new version of Rails (rather than using
gem update rails). With rvm, you can switch gemsets to run your application under an earlier version of Rails.
Rake is a software task management or “build” tool used extensively by Rails and many gems.
What version of rake is installed on your machine? Check with:
$ rake --version
You can check for the newest released rake gem.
For apps built with Rails 3.2, use rake 0.9.2.2 or newer.
If you are using an older version of Rake, you should update Rake to 0.9.2 or newer immediately after creating a new gemset and before installing Rails 3.2.
Here’s how to update Rake:
$ gem update rake $ rake --version
If you see this error message when installing one of the Rails Apps examples, you should create a new gemset, update Rake, and create a fresh install of Rails 3.2:
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.
See an explanation by David Chelimsky in his blog post Rake 0.9 and Gem Version Constraints about collisions among versions of Rake and gems.
The Ruby Bundler manages an application’s gems. Bundler works by reading a Gemfile in an application’s root directory, downloading and unpacking specified versions of each required gem, resolving any dependencies among gems, and creating a Gemfile.lock file to force use of the specified gem versions.
Each of the example apps is provided with a gemfile that specifies a known working set of required gems.
After creating an example app, run
bundle install to install all the necessary gems.
If you add a new gem to the gemfile, run
bundle install again.
If you want to force an upgrade to all the gems that have newer versions, run
bundle update. When you update a gem, bundler will not update a dependency of that gem if another gem still depends on it.
You will specify the gems you need in your application’s Gemfile. If you don’t specify a version, your application will use the newest gem version installed on your machine.
If you run
bundle update, each gem will be updated to the newest available. You may benefit from the features (or the bug fixes) of the newest version. Or you may discover integration issues with other gems which cause failures. To avoid surprises and manage gem updates cautiously, you can specify gem versions in your Gemfile (described in the RubyGems manual).
In the gemfiles for the example apps, you’ll often see “optimistic version constraint” specified with the
>= operator like this:
gem 'devise', '>= 1.3.4'
This means, “use any version of the Devise gem that is newer than version 1.3.4.” It will force an upgrade of the Devise gem to version 1.3.4 the first time you run
bundle update and check and install newer versions each subsequent time you run
bundle update. That’s a good scheme for keeping your gems up-to-date unless there is a known issue with a gem.
In the some gemfiles you may see “pessimistic version constraint” like this:
gem 'library', '~> 2.2'
which is shorthand for
gem 'library', '>= 2.2.0', '< 3.0'
which means use any gem version newer than 2.2.0 but don’t install 3.0 or anything newer.
We seldom specify pessimistic version constraint for gems in the example apps unless a gem developer recommends it.
See recommendations about using pessimistic version constraint in a blog post by Dan Croak titled Twiddle Wakka (yes, the
~> operator is variously known as the “twiddle wakka,” “tildemator,” or “spermy operator.”)
In the example apps, we use “absolute version constraint” to specify a known working version of a gem if we’ve seen reported problems with newer versions. Absolute version constraint looks like this:
gem 'library', '2.2'
I recommend installing rvm, the Ruby Version Manager. It’s essential if you must use multiple versions of Rails. And rvm makes it easy to switch sets of gems if you suspect a problem with incompatibilities among gems.
The rvm website explains how to install rvm.
See instructions for Installing Rails 3.2 to install Rails and create a new gemset using rvm.
RVM recognizes .ruby-version and .ruby-gemset files in a directory and loads a specified gemset.
Here’s how to create .ruby-version and .ruby-gemset files if you’re using a gemset named “ruby-2.1.2@myapp”:
$ echo "ruby-2.1.2" > .ruby-version $ echo "myapp" > .ruby-gemset
Using .ruby-version and .ruby-gemset files 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.
For Windows users juggling different versions of Ruby, pik is a tool similar to RVM. The pik set of commands for managing multiple rubies is similar to that of RVM.
The readme section on the repository home page gives the requirements for running pik, the most notable of which is that it requires Mat’s Ruby Interpreter version 1.8.7-p330. Make sure to install this version as your “default” Ruby version, which can be done most easily using RubyInstaller. In addition to Mat’s Ruby Interpreter, RubyInstaller also includes DevKit and other useful tools for Ruby programmers using Windows.
Pik can be installed with Windows Installer or by simply typing:
C:\gem install pikonce RubyInstaller 1.8.7-p330 has been installed and configured.