Gems and Bundler
Ruby's package system is called RubyGems. Gems are reusable libraries you can install and use in your projects. Bundler manages gem dependencies for your project, making sure everyone on your team uses the same versions.
# Install a gem from the command line
# gem install rails
# Use it in your code
require "rails"
# Check installed gems
# gem list
# Check a specific gem
# gem list rails
Try it Yourself ->
The Gemfile
A Gemfile sits in your project root and declares all the gems your project needs. Bundler reads this file and installs the correct versions. This ensures consistency across development, testing, and production.
# Gemfile
source "https://rubygems.org"
gem "rails", "~> 7.0"
gem "pg", "~> 1.4"
gem "puma", "~> 5.0"
group :development, :test do
gem "rspec", "~> 3.12"
gem "pry", "~> 0.14"
end
group :test do
gem "minitest", "~> 5.0"
gem "capybara", "~> 3.0"
end
Try it Yourself ->
bundle install
After creating or updating your Gemfile, run bundle install. Bundler resolves dependencies, downloads gems, and creates a Gemfile.lock that locks exact versions. Never edit Gemfile.lock manually.
# Install all gems from Gemfile
# bundle install
# Add a new gem
# bundle add rspec
# Update a specific gem
# bundle update rails
# Show gem dependencies
# bundle graph
# Open a console with gems loaded
# bundle exec irb
Try it Yourself ->
Creating Your Own Gem
Ruby makes it easy to package your code as a gem. You need a gemspec file that describes your gem, a lib directory for your code, and a version file. Then you can publish to RubyGems.org.
# my_gem.gemspec
Gem::Specification.new do |s|
s.name = "my_gem"
s.version = "0.1.0"
s.summary = "A helpful utility"
s.description = "Does amazing things"
s.authors = ["Your Name"]
s.email = "you@example.com"
s.homepage = "https://github.com/you/my_gem"
s.license = "MIT"
s.files = ["lib/my_gem.rb"]
s.add_dependency "httparty", "~> 0.20"
end
# Build and install locally
# gem build my_gem.gemspec
# gem install my_gem-0.1.0.gem
Try it Yourself ->
Popular Gems You Should Know
The Ruby ecosystem has thousands of gems. Here are some essentials that every Ruby developer encounters. Each one solves a real problem that comes up often in development.
# rails โ full-stack web framework
# sinatra โ lightweight web framework
# rake โ task runner (like Make for Ruby)
# pry โ enhanced REPL with debugging
# nokogiri โ HTML/XML parsing
# httparty โ HTTP client
# bundler โ dependency management
# rspec โ testing framework
# minitest โ built-in testing
# devise โ authentication
# sidekiq โ background jobs
# redis โ Redis client
# You can search for gems at:
# https://rubygems.org
# Over 170,000 gems available!
Try it Yourself ->