| name | ruby-gems-bundler |
| user-invocable | false |
| description | Use when working with Ruby gems, Bundler for dependency management, creating gemspecs, and publishing gems to RubyGems. |
| allowed-tools | ["Bash","Read","Write","Edit"] |
Ruby Gems and Bundler
Master Ruby's package management system with gems and Bundler. Learn to manage dependencies, create gems, and publish to RubyGems.
Bundler Basics
Gemfile
source 'https://rubygems.org'
ruby '3.3.0'
gem 'rails', '~> 7.1'
gem 'pg', '>= 1.1'
gem 'puma', '~> 6.0'
group :development, :test do
gem 'rspec-rails'
gem 'factory_bot_rails'
gem 'faker'
end
group :development do
gem 'rubocop'
gem 'rubocop-rails'
end
group :test do
gem 'capybara'
gem 'selenium-webdriver'
end
gem 'my_gem', git: 'https://github.com/user/my_gem.git'
gem 'local_gem', path: '../local_gem'
gem 'experimental_gem', git: 'https://github.com/user/repo.git', branch: 'develop'
gem 'sidekiq', require: 'sidekiq/web'
gem 'bootsnap', require: false
Version Constraints
gem 'rails', '7.1.0'
gem 'rails', '~> 7.1.0'
gem 'rails', '~> 7.1'
gem 'pg', '>= 1.1'
gem 'ruby-version', '>= 1.0', '< 2.0'
gem 'nokogiri', '>= 1.10', '< 2.0'
Bundler Commands
bundle install
bundle install --path vendor/bundle
bundle update
bundle update rails
bundle outdated
bundle show rails
bundle exec rspec
bundle open rails
bundle check
bundle clean
bundle list
bundle viz
Gemfile.lock
The Gemfile.lock file locks gem versions for consistent installations:
rm Gemfile.lock
bundle install
Creating Gems
Gem Structure
bundle gem my_gem
my_gem/
├── lib/
│ ├── my_gem/
│ │ └── version.rb
│ └── my_gem.rb
├── spec/
│ ├── my_gem_spec.rb
│ └── spec_helper.rb
├── bin/
│ ├── console
│ └── setup
├── .gitignore
├── Gemfile
├── LICENSE.txt
├── my_gem.gemspec
├── Rakefile
└── README.md
Gemspec
require_relative 'lib/my_gem/version'
Gem::Specification.new do |spec|
spec.name = "my_gem"
spec.version = MyGem::VERSION
spec.authors = ["Your Name"]
spec.email = ["your.email@example.com"]
spec.summary = "Short summary of gem"
spec.description = "Longer description of what gem does"
spec.homepage = "https://github.com/username/my_gem"
spec.license = "MIT"
spec.required_ruby_version = ">= 3.0.0"
spec.metadata["homepage_uri"] = spec.homepage
spec.metadata["source_code_uri"] = "https://github.com/username/my_gem"
spec.metadata["changelog_uri"] = "https://github.com/username/my_gem/CHANGELOG.md"
spec.files = Dir.chdir(File.expand_path(__dir__)) do
`git ls-files -z`.split("\x0").reject do |f|
f.match(%r{\A(?:test|spec|features)/})
end
end
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
spec.add_dependency "activesupport", "~> 7.0"
spec.add_dependency "nokogiri", ">= 1.10"
spec.add_development_dependency ,
spec.add_development_dependency ,
Version File
module MyGem
VERSION = "0.1.0"
end
Main Library File
require_relative "my_gem/version"
require_relative "my_gem/core"
require_relative "my_gem/helpers"
module MyGem
class Error < StandardError; end
def self.configure
yield configuration
end
def self.configuration
@configuration ||= Configuration.new
end
class Configuration
attr_accessor :api_key, :timeout
def initialize
@api_key = nil
@timeout = 30
end
end
end
Building and Publishing
Build Gem
gem build my_gem.gemspec
gem install ./my_gem-0.1.0.gem
gem uninstall my_gem
Publish to RubyGems
gem push my_gem-0.1.0.gem
gem push my_gem-0.2.0.gem
gem yank my_gem -v 0.1.0
gem unyank my_gem -v 0.1.0
Versioning
module MyGem
VERSION = "1.0.0"
end
RubyGems Commands
gem list
gem search rails
gem info rails
gem dependency rails
gem update
gem update rails
gem cleanup
gem env
gem install rails -v 7.1.0
gem install rails --no-document
gem uninstall rails
gem fetch rails
Gem Groups
group :development do
gem 'pry'
end
group :test do
gem 'rspec'
end
group :development, :test do
gem 'factory_bot'
end
bundle install --without production
Bundler.require(:default, :development)
Gem Sources
source 'https://rubygems.org'
source 'https://gems.example.com' do
gem 'private_gem'
end
gem 'my_gem', git: 'https://github.com/user/my_gem.git'
gem 'my_gem', git: 'https://github.com/user/my_gem.git', tag: 'v1.0'
gem 'my_gem', git: 'https://github.com/user/my_gem.git', branch: 'main'
gem 'my_gem', git: 'https://github.com/user/my_gem.git', ref: 'abc123'
gem 'my_gem', github: 'user/my_gem'
gem 'my_gem', path: '../my_gem'
Requiring Gems
require 'my_gem'
gem 'my_gem', require: false
require 'my_gem'
gem 'sidekiq', require: 'sidekiq/web'
Platform-Specific Gems
gem 'pg', platforms: :ruby
gem 'sqlite3', platforms: [:mingw, :mswin, :x64_mingw]
platforms :ruby do
gem 'pg'
gem 'nokogiri'
end
platforms :jruby do
gem 'activerecord-jdbc-adapter'
end
Private Gems
Using Private Gem Server
source 'https://rubygems.org'
source 'https://gems.mycompany.com' do
gem 'private_gem'
end
Using Git Credentials
machine github.com
login your-username
password your-token
Gem Development
Using bundle console
bin/console
bundle console
Running Tests
rake spec
bundle exec rspec
Local Development
gem 'my_gem', path: '../my_gem'
bundle config local.my_gem ../my_gem
Best Practices
- Always commit Gemfile.lock to version control
- Use pessimistic versioning (~>) for stability
- Keep gems updated but test thoroughly
- Use groups to separate dev/test/production gems
- Specify Ruby version in Gemfile for consistency
- Use bundle exec to ensure correct gem versions
- Document gem dependencies and why they're needed
- Test gems locally before publishing
- Follow semantic versioning for your gems
- Keep gemspecs clean and well-documented
Anti-Patterns
❌ Don't commit vendor/bundle to git (use .gitignore)
❌ Don't use require: false unnecessarily - adds manual work
❌ Don't specify exact versions unless absolutely necessary
❌ Don't push untested gem versions to RubyGems
❌ Don't include unnecessary files in gem packages
❌ Don't hardcode credentials in gemspec or Gemfile
Troubleshooting
bundle clean --force
rm Gemfile.lock && bundle install
bundle exec gem dependency
bundle install --verbose
bundle show rails
bundle list
Related Skills
- ruby-oop - For structuring gem code
- ruby-metaprogramming - Used in many gems
- ruby-standard-library - Core Ruby functionality