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.
Simple example of a gemfile:
source 'https://rubygems.org' gem 'nokogiri' gem 'rails', '3.0.0.beta3' gem 'rack', '>=1.0' gem 'thin', '~>1.1'
Gemfile in the root of the directory containing the associated code. For instance, in a Rails application, place the
Gemfile in the same directory as the
Gems are installed using a program called Bundler.
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 Rubyprojects 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.
Exmple of Gemfile.lock:
GEM remote: https://rubygems.org/ specs: actioncable (5.2.1) actionpack (= 5.2.1) nio4r (~> 2.0) websocket-driver (>= 0.6.1) actionmailer (5.2.1) actionpack (= 5.2.1) actionview (= 5.2.1) activejob (= 5.2.1) mail (~> 2.5, >= 2.5.4) rails-dom-testing (~> 2.0) actionpack (5.2.1) actionview (= 5.2.1) activesupport (= 5.2.1) rack (~> 2.0) rack-test (>= 0.6.3) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.0, >= 1.0.2) actionview (5.2.1) activesupport (= 5.2.1) builder (~> 3.1) erubi (~> 1.4) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.0, >= 1.0.3) activejob (5.2.1) activesupport (= 5.2.1) globalid (>= 0.3.6) activemodel (5.2.1) activesupport (= 5.2.1) activerecord (5.2.1) activemodel (= 5.2.1) activesupport (= 5.2.1) arel (>= 9.0) activestorage (5.2.1) actionpack (= 5.2.1) activerecord (= 5.2.1) marcel (~> 0.3.1) PLATFORMS ruby DEPENDENCIES bootsnap (>= 1.1.0) byebug capybara (>= 2.15) chromedriver-helper coffee-rails (~> 4.2) jbuilder (~> 2.5) listen (>= 3.0.5, < 3.2) puma (~> 3.11) rails (~> 5.2.1) rspec-rails RUBY VERSION ruby 2.5.1p57 BUNDLED WITH 1.16.3
Most of this post is lifted directly from Gemfile & Gemfile.lock in Ruby by David Pargal, because I found it to be a very good and comprehensive article for beginners. Why copy it? Simply because the Internet is a dynamic place and for the benefit of my learning, I’d hate for the original source to disappear from the Internet, which, of course, I have no control over.
Other bits –
Copyright 2021. All Rights Reserved. Adam Patel