Ruby Bundler
Dependency management for Ruby projects
📦 What is Bundler?
Bundler manages gem dependencies for Ruby projects. It ensures consistent gem versions across different environments by tracking dependencies in a Gemfile. Bundler simplifies installation and prevents version conflicts in your applications.
# Gemfile
source 'https://rubygems.org'
gem 'rails', '~> 7.0'
gem 'pg', '~> 1.4'
Command:
$ bundle install
Key Bundler Features
Gemfile
Define project dependencies
gem 'sinatra'
Gemfile.lock
Lock exact gem versions
sinatra (3.0.5)
Install
Install all dependencies
bundle install
Update
Update gem versions
bundle update
🔹 Creating a Gemfile
The Gemfile lists all gems your project needs. Create it in your project root directory and specify gem names with optional version constraints for better dependency control.
# Gemfile
source 'https://rubygems.org'
# Specify exact version
gem 'rails', '7.0.4'
# Allow minor updates
gem 'puma', '~> 6.0'
# Any version
gem 'rake'
# Development only
group :development do
gem 'pry'
end
File Created:
✓ Gemfile created in project root
🔹 Installing Dependencies
Use bundle install to download and install all gems listed in your Gemfile. Bundler creates a Gemfile.lock file that records exact versions for consistent installations across environments.
# Install all gems
bundle install
# Short version
bundle
# Install to specific path
bundle install --path vendor/bundle
# Skip production gems
bundle install --without production
Output:
Fetching gem metadata from https://rubygems.org/
Resolving dependencies...
Installing rails 7.0.4
Bundle complete! 3 Gemfile dependencies, 45 gems now installed.
🔹 Using Bundler in Code
Add Bundler setup to your Ruby files to load all gems from your Gemfile. This ensures your application uses the correct gem versions specified in Gemfile.lock.
# app.rb
require 'bundler/setup'
Bundler.require(:default)
# Now all gems from Gemfile are loaded
require 'sinatra'
get '/' do
'Hello from Sinatra with Bundler!'
end
Output:
Server running on http://localhost:4567
🔹 Common Bundler Commands
Bundler provides several commands to manage your project dependencies effectively. These commands help you install, update, check, and troubleshoot gems in your Ruby applications.
Essential Commands:
- bundle install - Install all dependencies
- bundle update - Update gems to latest versions
- bundle exec - Run command with bundled gems
- bundle show - Show gem installation path
- bundle outdated - List outdated gems
# Run rake with bundled gems
bundle exec rake db:migrate
# Show where a gem is installed
bundle show rails
# Check for outdated gems
bundle outdated
Output:
/usr/local/lib/ruby/gems/3.2.0/gems/rails-7.0.4
🔹 Version Constraints
Bundler supports various version constraint operators to control which gem versions can be installed. This helps maintain compatibility while allowing safe updates to your dependencies.
# Gemfile version constraints
# Exact version
gem 'rails', '7.0.4'
# Greater than or equal
gem 'rake', '>= 13.0'
# Pessimistic operator (recommended)
gem 'puma', '~> 6.0' # >= 6.0 and < 7.0
# Multiple constraints
gem 'nokogiri', '>= 1.13', '< 1.15'
# Latest version
gem 'httparty'
Explanation:
~> 6.0 means: >= 6.0.0 and < 7.0.0
~> 6.0.5 means: >= 6.0.5 and < 6.1.0
🔹 Gem Groups
Organize gems into groups for different environments like development, test, and production. This allows you to install only the gems needed for specific environments, reducing overhead.
# Gemfile with groups
source 'https://rubygems.org'
gem 'rails'
group :development do
gem 'pry'
gem 'spring'
end
group :test do
gem 'rspec'
gem 'factory_bot'
end
group :production do
gem 'pg'
end
Install Command:
$ bundle install --without production