Bundler
Bundler is a Ruby gem dependency manager that ensures consistent gem versions across development, testing, and production environments.
Installation
# Install bundler gem globally
gem install bundler
# Check version
bundle --version
# Update bundler
gem update bundler
Gemfile Management
Basic Gemfile
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '3.2.0'
# Core gems
gem 'rails', '~> 7.0.0'
gem 'sqlite3', '~> 1.4'
gem 'puma', '~> 5.0'
gem 'sass-rails', '~> 6'
gem 'webpacker', '~> 5.0'
# Database
gem 'pg', '~> 1.1'
gem 'redis', '~> 4.0'
# Development gems
group :development do
gem 'web-console', '>= 4.1.0'
gem 'rack-mini-profiler', '~> 2.0'
gem 'byebug', platforms: [:mri, :mingw]
end
# Test gems
group :test do
gem 'rspec-rails'
gem 'factory_bot_rails'
gem 'capybara'
end
Essential Commands
# Install dependencies
bundle install
# Install without development/test gems
bundle install --without development test
# Install in vendor/bundle
bundle install --path vendor/bundle
# Update all gems
bundle update
# Update specific gem
bundle update rails
# List installed gems
bundle list
# Show gem information
bundle show rails
bundle show --paths
# Verify gemfile
bundle check
# Execute command in bundle context
bundle exec rspec
# Generate Gemfile.lock
bundle lock
Gem Version Constraints
|Operator|Example|Meaning|
|---------|-------------|
|=|gem 'rails', '= 7.0.0'|Exact version|
|~>|gem 'rails', '~> 7.0'|Pessimistic (7.0.x)|
|>=|gem 'rails', '>= 7.0.0'|Greater or equal|
|>|gem 'rails', '> 7.0.0'|Greater than|
|<=|gem 'rails', '<= 7.0.0'|Less or equal|
|<|gem 'rails', '< 7.0.0'|Less than|
Grouping Gems
# Development only
group :development do
gem 'rails-erd'
gem 'annotate'
end
# Testing only
group :test do
gem 'rspec-rails'
gem 'factory_bot_rails'
end
# Development and test
group :development, :test do
gem 'pry-rails'
gem 'dotenv-rails'
end
# Production only
group :production do
gem 'pg'
gem 'rails_12factor'
end
Gemfile.lock
# Lock specific versions
bundle lock
# Update with new compatible versions
bundle lock --update
# Update specific gem
bundle lock --update rails
# Remove lock file
rm Gemfile.lock
# Regenerate
bundle install
Common Workflows
New Rails Project
# Create new Rails app
rails new myapp
cd myapp
# Install gems
bundle install
# Generate scaffold
bundle exec rails generate scaffold Post title:string content:text
bundle exec rails db:migrate
Adding New Gem
# Add to Gemfile
echo "gem 'devise'" >> Gemfile
# Install
bundle install
# Or use bundle add
bundle add devise
Updating Gems
# Update all gems
bundle update
# Show outdated gems
bundle outdated
# Update specific gem
bundle update rails
# Clean up unused gems
bundle clean
Bundler Configuration
.bundle/config
---
BUNDLE_PATH: vendor/bundle
BUNDLE_JOBS: 4
BUNDLE_RETRY: 3
Command Line Config
# Set path
bundle config set --local path 'vendor/bundle'
# Set jobs
bundle config set jobs 4
# Unset config
bundle config unset path
Troubleshooting
# Clear bundler cache
bundle clean --force
# Rebuild gems
bundle install --redownload
# Check for conflicts
bundle check
# Install missing gems
bundle install --missing
# Verbose output
bundle install --verbose
# Update gems to latest compatible
bundle update
Docker Integration
Dockerfile
FROM ruby:3.2
WORKDIR /app
# Install dependencies
RUN apt-get update && apt-get install -y postgresql-client
# Copy Gemfile
COPY Gemfile Gemfile.lock ./
# Install gems
RUN bundle install --jobs 4 --retry 3
COPY . .
CMD ["rails", "server", "-b", "0.0.0.0"]
CI/CD Integration
GitHub Actions
name: Tests
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: ruby/setup-ruby@v1
with:
ruby-version: 3.2
bundler-cache: true
- run: bundle exec rspec
Best Practices
- Always commit Gemfile.lock to version control
- Use pessimistic version constraints (~>) for stability
- Review Gemfile regularly for security updates
- Run
bundle auditto check for vulnerabilities - Use separate Gemfiles for different environments if needed
- Keep gems updated with
bundle outdated - Use
bundle cleanto remove unused gems
Resources
Last updated: 2025-07-06|Edit on GitHub