Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

158 lines (148 sloc) 11.694 kB
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Rails Error: You have already activated &#183; RailsApps</title>
<link href="https://plus.google.com/u/0/b/117374718581973393536/117374718581973393536/posts/" rel="publisher" />
<link rel="stylesheet" href="http://railsapps.github.com/css/bootstrap.css" type="text/css" charset="utf-8" />
<link rel="stylesheet" href="http://railsapps.github.com/css/screen.css" type="text/css" charset="utf-8" />
<link rel="stylesheet" href="http://railsapps.github.com/css/gollum.css" type="text/css" charset="utf-8" />
<link rel="stylesheet" href="http://railsapps.github.com/css/site.css" type="text/css" charset="utf-8" />
<link rel="stylesheet" href="http://railsapps.github.com/css/syntax.css" type="text/css" charset="utf-8" />
<script src="http://code.jquery.com/jquery-1.6.min.js" type="text/javascript"></script>
<script src="http://railsapps.github.com/javascript/jquery.text_selection-1.0.0.min.js" type="text/javascript"></script>
<script src="http://railsapps.github.com/javascript/jquery.previewable_comment_form.js" type="text/javascript"></script>
<script src="http://railsapps.github.com/javascript/jquery.tabs.js" type="text/javascript"></script>
<script src="http://railsapps.github.com/javascript/gollum.js" type="text/javascript"></script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-5109366-14']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body>
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a href="http://railsapps.github.com/" class="brand">RailsApps Project</a>
<ul class="pull-right nav">
<li><a href="http://blog.railsapps.org/" class="twitter">Blog</a></li>
<li><a href="http://twitter.com/rails_apps" class="twitter">Twitter</a></li>
<li><a href="https://plus.google.com/117374718581973393536" class="google">Google +</a></li>
<li><a href="https://github.com/RailsApps" class="github">GitHub Repository</a></li>
</ul>
</div>
</div>
</div>
<div class="container">
<div class="content wikistyle gollum textile">
<h1>Rails Error: “You have already activated (…)”</h1>
<h4>by Daniel Kehoe</h4>
<p><em>Last updated 12 May 2012</em></p>
<p>Here’s help for the error “You have already activated (…) but your Gemfile requires (…)”.</p>
<p>This is a note for developers using the starter apps from the <a href="http://railsapps.github.com/">Rails Apps</a> repository. Others have found it helpful as well.</p>
<h2>Error</h2>
<p>You may be running a command such as:</p>
<pre>
rake db:migrate
</pre>
<p>and seeing this error:</p>
<pre>
You have already activated (some gem version), but your Gemfile requires (some newer gem version). Consider using bundle exec.
</pre>
<p>You may also be running a script, such as generating an application from an application template, where rake or other gems are activated by the script:</p>
<pre>
rails new myapp -m (some application template)
</pre>
<p>and seeing this error:</p>
<pre>
The template (some application template) could not be loaded. Error: You have already activated (some gem version), but your Gemfile requires (some gem version). Using bundle exec may solve this.
</pre>
<h2>What is Happening</h2>
<p>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.</p>
<p>Rails uses <a href="http://gembundler.com/">Bundler</a> 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 <code>bundle install</code> downloads gems specified in your Gemfile and installs the gems as part of the system-installed Ruby version.</p>
<p>If newer versions of gems exist, and particular versions are not specified in your Gemfile, you can run <code>bundle update</code> 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 <code>bundle update</code>.</p>
<p>Many Rails developers use Wayne Seguin’s rvm, the <a href="https://rvm.io/">Ruby Version Manager</a>, 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.”</p>
<p>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.</p>
<p>In the simplest case, this could happen when you run <code>bundle install</code> 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.</p>
<p>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:</p>
<pre>
$ bundle install
$ rake db:migrate
</pre>
<p>You’ll see:</p>
<pre>
You have already activated rake 0.9.0, but your Gemfile requires rake 0.8.7. Consider using bundle exec.
</pre>
<p>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.</p>
<p>As Yehuda Katz explains (in <a href="http://yehudakatz.com/2011/05/30/gem-versioning-and-bundler-doing-it-right/">Gem Versioning and Bundler: Doing it Right</a>), “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.”</p>
<p>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.</p>
<h2>Simple Solution for a Simple Case: “bundle exec”</h2>
<p>For the simplest case, the solution is simple. Prepend the command with <code>bundle exec</code>.</p>
<p>For example, if you saw the error when you ran:</p>
<pre>
rake db:migrate
</pre>
<p>try running:</p>
<pre>
bundle exec rake db:migrate
</pre>
<p>Using <code>bundle exec</code> invokes Bundler and loads the versions of gems specified by your Gemfile before attempting to run an executable provided by a gem. With <code>bundle exec</code> 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, <code>bundle exec rake db:migrate</code> will use the version of rake specified in the Gemfile to execute the command. If you don’t use <code>bundle exec</code>, 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).</p>
<p>If you’re using rvm, the <a href="https://rvm.io/">Ruby Version Manager</a>, you don’t need to use <code>bundle exec</code> because rvm versions 1.11.0 and newer include the <a href="http://mpapis.github.com/rubygems-bundler/">rubygems-bundler</a> gem to handle this. See <a href="https://rvm.io/integration/bundler/">rvm and bundler integration</a>.</p>
<p><em>Doesn’t work for you? Please add to the comments below.</em></p>
<h2>Simple Solution for a Complex Case: Try Again</h2>
<p>If you’re seeing the error as a result of running a script, such as generating an application from an application template, the problem is more complex. You can’t add <code>bundle exec</code> because the executable that attempts to load a gem is hidden inside the script.</p>
<p>However, there is a easy but non-obvious solution: Simply run the script a second time.</p>
<p>For example if you run the command:</p>
<pre>
rails new myapp -m (some application template)
</pre>
<p>and see the error, try running the command again:</p>
<pre>
rails new myapp -m (some application template)
</pre>
<p>In my experience, the script will succeed the second time you try.</p>
<p><em>Doesn’t work for you? Please add to the comments below.</em></p>
</div><!-- class="content" -->
<div class="comments">
<div class="content wikistyle gollum">
<h2>Comments and Issues</h2>
</div>
<p>Is this helpful? Please add a comment below. Your encouragement fuels the project.</p>
<p>Did you find an error? Or couldn't get something to work? For the example apps and tutorials, please create a GitHub issue in the repository for the example app. Creating a GitHub issue is the best way to make sure a problem is investigated and fixed.</p>
<div id="disqus_thread"></div>
<script type="text/javascript">
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = 'railsapps'; // required: replace example with your forum shortname
/* * * DON'T EDIT BELOW THIS LINE * * */
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
</div><!-- class="comments" -->
<div class="footer row">
<div class="span4">
<h3>Credits</h3>
<p><a href="http://danielkehoe.com/">Daniel Kehoe</a> initiated the <a href="http://railsapps.github.com/">RailsApps Project</a>. Thanks to all the users and contributors.</p>
</div>
<div class="span4">
<h3>Wiki</h3>
<p>Corrections? Additions? You can edit this page <a href="https://github.com/RailsApps/railsapps.github.com/wiki/_pages">on the wiki</a>.</p>
</div>
<div class="span4">
<h3>Last edit</h3>
<p>by <b>Daniel Kehoe</b>, 2012-07-20 23:08:42</p>
</div>
</div>
</div>
</body>
</html>
Jump to Line
Something went wrong with that request. Please try again.