Gems and Bundler

gem install

A gem is a packaged Ruby library with code and metadata. The gem command installs and lists these packages on your machine.

Check the tool itself first, then install one package by name to try it out quickly in a script.

gem -v

3.6.9

gem install minitest

Successfully installed minitest-6.0.6
1 gem installed

The Gemfile

The Gemfile lists the gems one project needs, with optional version limits. Bundler reads it to build a consistent set.

Keep this file in version control so every machine installs the same stack. The idioms from Duck typing and core idioms stay the same no matter which gems you add.

source "https://rubygems.org" gem "minitest", "~> 5.20"

Bundle install

bundle install reads the Gemfile and installs every missing gem. Run it after cloning a project and after editing the Gemfile.

Bundler resolves versions so all gems fit together, then remembers the result for the next section.

bundle -v

Bundler version 4.0.23

bundle install

Fetching gem metadata from https://rubygems.org/...
Resolving dependencies...
Bundle complete! 1 Gemfile dependency, 1 gem now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.

The lockfile

Bundler writes exact versions into Gemfile.lock. This file freezes the whole tree, including dependencies of dependencies.

Commit the lockfile for apps so servers and teammates run identical code. Libraries usually leave it out because they support version ranges. More Ruby tooling is linked from the Ruby overview.

GEM specs: minitest (5.27.0)

Next: Rake tasks

Article author: Arthur Isaev