[Rails] What is the difference between bundle install and bundle update?

Difference between bundle install and bundle update

What is the prior knowledge, Gemfile, Gemfile.lock, and Bundler needed to understand these two? I will write about.

What is Bundler?

A gem that manages a gem. Thanks to Bundler, you can use bundle install </ code> and bundle update </ code>.

What is a Gemfile?

An image like a "blueprint" for installing gem.

What is Gemfile.lock?

An image like the "result diagram" after actually installing the gem. Since gems are often related to each other, necessary gems are generated in addition to the gems described in the gemfile. In that case, Bundler will automatically install the required gem and write it in Gemfile.lock.

Why do you need Gemfile.lock?

Gemfile is a blueprint. Only the gem to install is described. Also, gems related to gems are not described, and the versions of gems installed in the production environment and the development environment cannot be unified. However, by using Gemfile.lock, you can use the same gem and gem version in any environment.

About bundle install and bundle update

Now that I have gained prior knowledge, I will move on to the main subject.

bundle install Install gem based on Gemfile.lock. At this time, if there is a gem that is not described in Gemfile.lock and is described in Gemfile, install that gem and the gem related to that gem, and then install Gemfile.lock.

bundle update Install gem based on Gemfile. Then update Gemfile.lock.

About the proper use of the two commands

bundle install </ code> → Used when writing a new gem in a new environment or Gemfile. bundle update </ code> → Used when updating the gem version. (Because bundle install does not update the gem in Gemfile.lock)

Summary

- bundle install </ code> refers to Gemfile.lock to install the gem. Also, If Gemfile.lock does not exist, create Gemfile.lock after installing gem based on Gemfile. - bundle update </ code> ignores Gemfile.lock, installs gem based on Gemfile, and then updates Gemfile.lock. -Since Gemfile.lock is ignored, unlike bundle install </ code>, the dependency (version etc.) between gems is renewed.

I see. Basically, it seems that bundle install is often used during actual development. Use with caution when using bundle update.

Recommended Posts