RubyGems, Bundler and Dependency Management
38.1 RubyGems
gem install nokogiri
gem install rails -v 8.0.0
gem list; gem outdated; gem update; gem uninstall nokogiri
gem search faraday --remote
gem info faraday; gem contents faraday; gem env38.2 Bundler
Version constraints: "= 1.2", ">= 1.2", "~> 1.2" (≥1.2, <2.0), "~> 1.2.3" (≥1.2.3, <1.3).
Gemfile.lock: commit it for applications. For gems, don't rely on it (constraints go in the gemspec).
# Gemfile
source "https://rubygems.org"
ruby "3.4.1"
gem "sinatra", "~> 4.0" # pessimistic constraint: >= 4.0, < 5.0
gem "puma", ">= 6.4"
gem "pg", require: false
gem "my_lib", path: "../my_lib"
gem "rack", github: "rack/rack", branch: "main"
group :development, :test do
gem "rspec", "~> 3.13"
gem "rubocop", require: false
gem "debug"
endbundle init # create a Gemfile
bundle install # install + write Gemfile.lock (commit it for apps)
bundle exec rspec # run with the bundle's gem versions
bundle add faraday # add a dependency
bundle update rack # update one gem (conservative)
bundle outdated
bundle config set --local path vendor/bundle
bundle lock --add-platform x86_64-linux
bundle exec ruby app.rbrequire "bundler/setup" # in scripts: use the Gemfile's versions
require "bundler/inline" # single-file scripts with dependencies
gemfile do
source "https://rubygems.org"
gem "colorize"
end