Why "bundle install" a gem instead of "gem install" for a rails 3 app?

dartfrog picture dartfrog · Sep 1, 2011 · Viewed 11.1k times · Source

I'm a beginner programmer going through the railstutorial by michael hartl, and notice that the process for using gems in the application is through adding it to the gemfile, and then doing a:

$ bundle install

Any reason why one wouldn't just do a:

$ [sudo] gem install [the_gem]

and then just add it to the Gem file? I know this was somewhat the process back in rails 2.

Thanks!

Answer

dexter picture dexter · Sep 1, 2011

Using bundler instead of gem command to directly install your gems gives you a whole lot of benefits.

In this specific case where you suggest using the gem command to install and adding it later to the Gemfile, bundler will resolve all the dependencies for you when you install a gem, which you might have to manually resolve otherwise.

To give you an example, let's take the following dependencies:

sunspot_rails
  nokogiri (>= 1.2.0)

webrat
  nokogiri (>= 1.3) 

Both webrat and sunspot_rails gems require different versions of nokogiri as a dependency. If you just use the gem command to install them, it might install both versions of nokogiri or worse complain about version conflicts. Bundler will be wise enough to resolve this dependency conflict and install the right version (say nokogiri 1.3) and make both sunspot_rails and webrat happy!

Sorry about the long explanation. But, hope you get the point! :)

And btw you should have a look at this file Gemfile.lock to see what bundler does behind the scenes for you.