Gemfile and Gemfile.lock in Ruby

Gemfile

Daval Pargal
davalpargal
2 min readSep 14, 2018

--

A Gemfile is a file we create which is used for describing gem dependencies for Ruby programs. A gem is a collection of Ruby code that we can extract into a “collection” which we can call later. It lets you specify which gems you want to use, and which versions of these gems to use.

Example of a simple Gemfile :

Gemfile.lock

The Gemfile.lock file is where Bundler records the exact versions that were installed. This way, when the same library/project is loaded on another machine, running bundle install will look at the Gemfile.lock and install the exact same versions, rather than just using the Gemfile and installing the most recent versions.

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 .

Running different versions on different machines could lead to broken tests, etc. Never directly edit the lock file.

Example of a Gemfile.lock :

Bundle acts as bouncer to unknown gem versions

To summarize using this meme, Bundle is like a bouncer that has gemfile.lock as the list of invited guests and gemfile is like the invitation card. If bundler finds the invitation card not exactly matching the list, that gem will not be installed and bundler will throw an error.

--

--