开发者

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

I'm a beginner programmer g开发者_StackOverflow社区oing 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!


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.


With the Gemfile, you can make sure that any other developers also working on your app have the same version of every gem. It also ensures that you didn't put a different [the_gem] in your gem commmand from what you have in your Gemfile.


Some gem combinations only make sense in specific environments - e.g. a certain db in development and a different one in production.

The Gemfile is a record of what your application needs in each environment, and bundle install installs exactly what is needed, taking care of dependencies.

If any of the gems in your Gemfile is already installed in your current gemset (via gem install), then the bundler will just use them (rather than reinstalling them).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜