Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 398
dartfrog Avatar asked Sep 01 '11 17:09

dartfrog


People also ask

What is the difference between gem install and bundle install?

Almost seems like running 'gem install' adds it to the global available gems (and hence terminal can run the package's commands), whereas adding it to the gemfile and running bundle install only adds it to the application. Similar to npm install --global. that's basically it.

What does bundle gem do?

Bundler provides a consistent environment for Ruby projects by tracking and installing the exact gems and versions that are needed. Bundler is an exit from dependency hell, and ensures that the gems you need are present in development, staging, and production. Starting work on a project is as simple as bundle install .

What is bundle install in Rails?

Bundler makes sure that Ruby applications always use the exact gems and versions that you need while keeping a consistent environment and gem dependencies satisfied. This is done by ensuring that the gems you need are present in the development, staging, and production stages.

What is the goal of running bundle install?

When we run bundle install in a project , if there is no Gemfile. lock exist, Bundler will fetch all remote sources, resolve dependencies and install all needed gems.


1 Answers

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.

like image 64
dexter Avatar answered Oct 16 '22 20:10

dexter