Rails Error: “Could not be loaded… You have already activated…”

by Daniel Kehoe

Last updated 15 December 2012

Here’s help for the error “You have already activated (…) but your Gemfile requires (…)”.

This is a note for developers using the Rails Composer tool. Others may find it helpful as well.

Quick Fix

Try removing the gem that is a problem, specifying the version number:

gem uninstall some_gem -v 3.0.4

And install the newest version of the gem:

gem install some_gem

Read on if you want to know what is happening.

Errors with the Rails Composer Tool or the rails_apps_composer Gem

If you are using the Rails Composer tool, or the rails_apps_composer gem, or generating an application from an application template using the rails new command:

rails new myapp -m (some application template)

and seeing this error:

Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.
    composer  Running 'after bundler' callbacks.
The template [...] could not be loaded.
Error: You have already activated ..., but your Gemfile requires .... 
Using bundle exec may solve this.

See the section Rails Composer below.

Other Error Cases

You may be running a command such as:

rake db:migrate

and seeing this error:

You have already activated <some gem version>, but your Gemfile requires <some newer gem version>. Using bundle exec may solve this.

See the section Other Cases below.

Rails Composer

The error is caused by conflicting gem versions (see the section What is Happening for details). Ignore the misleading hint in the error message: “using bundle exec” will not solve this.

When you use the rails new command, the Rails gem and all its related gems must be present, either in your system environment, or (if you use rvm) from a default rvm gemset. The Rails Composer tool wants to use the newest available gem versions. Occasionally one of the gems (often a gem required as a dependency for another gem) has been recently updated. If that’s the case, rails new uses the Rails gemset (containing the outdated gem) and then tries to run the Rails Composer application template with a new set of gems (containing an updated gem). The outdated gem has already been activated so the application template cannot be loaded, resulting in the error you see.

Try removing the outdated gem from the initial gemset:

$ gem uninstall some_gem

And install the newest version of the gem:

gem install some_gem

Then run the rails new command again.

To fully resolve the problem, you may need to install rvm and create and use a new gemset before you run the rails new command again. Installing Rails into a new gemset before running rails new will populate the gemset with the most current available gems (including any gems required by a gem dependency).

$ rvm ruby-1.9.3@my_new_gemset --create --default

Doesn’t work for you? Please add to the comments below.

Other Cases

For other cases, the solution is simple. Prepend the command with bundle exec.

For example, if you saw the error when you ran:

rake db:migrate

try running:

bundle exec rake db:migrate

Using bundle exec invokes Bundler and loads the versions of gems specified by your Gemfile before attempting to run an executable provided by a gem. With bundle exec you’ll use the versions of the gems declared in your Gemfile. So instead of using whatever version of rake is currently active on your system, bundle exec rake db:migrate will use the version of rake specified in the Gemfile to execute the command. If you don’t use bundle exec, you have to hope that the “system executbale” version of the gem is the same as the version specified in the Gemfile (or the same as a dependency of a gem specified in the Gemfile).

If you’re using rvm, you shouldn’t see this problem because rvm versions 1.11.0 and newer include the rubygems-bundler gem to handle this. With the rubygems-bundler gem, you don’t need to use bundle exec. See rvm and bundler integration.

Doesn’t work for you? Please add to the comments below.

What is Happening

RubyGems is a package manager for the Ruby programming language that provides a standard format for distributing Ruby programs and libraries (in a self-contained format called a “gem”). Gems add functionality to a Rails app. The gems you use in a Rails application are specified in a Gemfile that is part of your application.

Rails uses Bundler to manage gem versions. Bundler makes sure the versions of gems specified in your Gemfile (and other gems which are dependencies of your specified gems) are used when you develop or deploy your Rails application. The command bundle install downloads gems specified in your Gemfile and installs the gems as part of the system-installed Ruby version.

If newer versions of gems exist, and particular versions are not specified in your Gemfile, you can run bundle update to install the newer gem versions. Often, gems that are not specified in your Gemfile, but are dependencies of other gems, will be updated by bundle update.

Many Rails developers use Wayne Seguin’s rvm, the Ruby Version Manager, to make it easy to run different versions of Ruby on one computer. The rvm utility also makes it easy to create different sets of gems and switch between “gemsets.”

The error “You have already activated (some gem version), but your Gemfile requires (some newer gem version)” results when one version of a gem has been recently used (loaded into memory) and a different version is requested. Rather than force a different version to load, RubyGems aborts and shows the error.

In the simplest case, this could happen when you run bundle install and Bundler loads gems specified by the Gemfile into the shell environment. Then, if you use the command line to execute a command offered by a gem, RubyGems may attempt to use a system version of the gem and will abort because you’ve already loaded a different version of the gem.

For example, if your Gemfile specifies a gem that depends on rake-0.8.7 but you’ve installed a version of Ruby that comes with rake-0.9.0 and you try:

$ bundle install
$ rake db:migrate

You’ll see:

You have already activated rake 0.9.0, but your Gemfile requires rake 0.8.7. Consider using bundle exec.

You’ll always see this error if you have a newer version of rake activated on your machine than the one specified in a project’s Gemfile.

As Yehuda Katz explains (in Gem Versioning and Bundler: Doing it Right), “Bundler’s sandbox relies on its ability to be present at the very beginning of the Ruby process, and to therefore have the ability to ensure that the versions of all loaded libraries will reflect the ones listed in the Gemfile.lock. By running a system executable, you are executing Ruby code before Bundler can modify the load path and replace the normal Rubygems loading mechanism, allowing arbitrary unmanaged gems to get loaded into memory.”

In a more complex case, you may have activated (loaded into memory) a gem by running a command on the command line or running an application and subsequently running a script such as an application template that requires a different gem version. In this case, RubyGems will abort and the script will fail.

Comments

Is this helpful? Your encouragement fuels the project. Please tweet or add a comment. Couldn't get something to work? For the example apps and tutorials, it's best to open an issue on GitHub so we can help you.

comments powered by Disqus