Ruby Gems and Bundler
## Learning Objectives
- Understand RubyGems structure
- Use gem command line tool
- Manage dependencies with Bundler
- Create and distribute gems
## RubyGems Overview
RubyGems is Ruby's package manager. Gems are reusable libraries distributed as packages.
### Finding Gems
```bash
gem search rails # Search for gems
gem list # List installed gems
gem info rails # Show gem info
```
### Installing Gems
```bash
gem install gem_name
gem install gem_name --version '~> 2.0'
gem install rails --no-document # Skip documentation
```
### Using Installed Gems
```ruby
require 'gem_name'
# Or specify version
gem 'rake', '~> 13.0'
require 'rake'
```
## Bundler
Bundler manages gem dependencies for Ruby projects.
### Gemfile
Create a `Gemfile` in your project root:
```ruby
source 'https://rubygems.org'
# Specify gem with version
gem 'rails', '~> 7.0'
# Gem without version (uses latest)
gem 'puma'
# Gem with options
gem 'mysql2', '>= 0.4.4', '< 1.0'
# Git repository
gem 'capistrano', git: 'https://github.com/capistrano/capistrano.git'
# Local path
gem 'my gem', path: '~/code/my_gem'
# Groups
group :development, :test do
gem 'rspec'
gem 'pry'
end
```
### Install Dependencies
```bash
bundle install
bundle install --jobs=4 # Parallel installation
bundle update # Update all gems
```
### Gemfile.lock
After `bundle install`, `Gemfile.lock` is created with exact versions:
```ruby
GEM
remote: https://rubygems.org/
specs:
rails (7.0.0)
actionmailbox (= 7.0.0)
...
rspec (3.10.0)
...
PLATFORMS
ruby
DEPENDENCIES
rails (~> 7.0)
rspec
BUNDLED WITH
2.3.0
```
### Using Gems in Code
```ruby
require 'bundler/setup'
# Or let Bundler setup automatically
require 'bundler'
Bundler.require
# In a script
require 'bundler/setup'
```
### Running Gems
```bash
bundle exec rspec
bundle exec rake db:migrate
bundle exec ruby script.rb
```
## Bundler Commands
```bash
bundle install # Install dependencies
bundle update # Update gems
bundle exec CMD # Run command in bundle context
bundle list # List installed gems
bundle show GEM # Show gem path
bundle check # Verify dependencies
bundle outdated # Check for outdated gems
bundle init # Create new Gemfile
```
## Creating a Gem
### Structure
```text
my_gem/
├── my_gem.gemspec
├── lib/
│ └── my_gem/
│ ├── version.rb
│ └── my_gem.rb
├── spec/
│ └── my_gem_spec.rb
└── README.md
```
### Gemspec File
```ruby
Gem::Specification.new do |s|
s.name = 'my_gem'
s.version = '1.0.0'
s.authors = ['Your Name']
s.email = 'your@email.com'
s.summary = 'A sample gem'
s.description = 'A longer description'
s.homepage = 'https://github.com/you/my_gem'
s.license = 'MIT'
s.files = `git ls-files -z`.split("\x0")
s.require_paths = ['lib']
s.required_ruby_version = '>= 2.4'
s.add_dependency 'json'
s.add_development_dependency 'rspec', '~> 3.0'
end
```
### Main Library File
```ruby
# lib/my_gem.rb
require 'my_gem/version'
module MyGem
class Error < StandardError; end
def self.greet(name)
"Hello, #{name}!"
end
end
```
```ruby
# lib/my_gem/version.rb
module MyGem
VERSION = '1.0.0'
end
```
### Build and Install
```bash
gem build my_gem.gemspec
gem install ./my_gem-1.0.0.gem
gem push my_gem-1.0.0.gem # Publish to RubyGems.org
```
## Common Gems
### Web Frameworks
| Gem | Description |
|-----|-------------|
| rails | Full-stack web framework |
| sinatra | Lightweight web framework |
| rack | Web server interface |
### Database
| Gem | Description |
|-----|-------------|
| activerecord | ORM (part of Rails) |
| sequel | Lightweight ORM |
| pg | PostgreSQL adapter |
### Testing
| Gem | Description |
|-----|-------------|
| rspec | BDD testing framework |
| minitest | Unit testing framework |
| capybara | Integration testing |
| factory_bot | Test fixtures |
### Utilities
| Gem | Description |
|-----|-------------|
| pry | REPL and debugger |
| bundler | Dependency manager |
| rake | Task automation |
| faraday | HTTP client |
## .gemspec vs Gemfile
| Aspect | gemspec | Gemfile |
|--------|---------|---------|
| Purpose | Package definition | Dependency management |
| Used by | gem build/install | bundle install |
| Versioning | In file | Lock file |
| Platform | gem install | bundle exec |
## Summary
- RubyGems is the package manager for Ruby
- `gem install` installs globally
- Bundler manages project dependencies via Gemfile
- `bundle install` installs from Gemfile
- `bundle exec` runs commands in bundle context
- Gemspec defines gem metadata and dependencies
- `gem build` and `gem push` for publishing
Comments
Comments powered by Giscus
To enable comments, add your Giscus embed code here.
Learn more about Giscus →