Packages (RubyGems & Bundler) in Ruby
Manage Ruby dependencies using RubyGems and Bundler, and understand Gemfile and Gemfile.lock.
What is RubyGems?
RubyGems is Ruby's built-in package manager. A "gem" is a packaged Ruby library or tool. The gem command lets you install, update, and remove gems from your system.
1. Installing a Gem
Install a package from RubyGems.org.
2. Listing Installed Gems
See what's installed.
3. Uninstalling a Gem
Remove an installed gem.
4. Using a Gem in Code
require loads the gem.
Bundler
Bundler manages gem dependencies for a project. It ensures every developer and deployment uses the same gem versions. Bundler reads a Gemfile to know what gems are needed.
1. Installing Bundler
Bundler is itself a gem.
2. The Gemfile
Declare project dependencies.
3. bundle install
Install all gems in the Gemfile.
4. Gemfile.lock
Pin exact versions for reproducible builds.
Common Bundler Commands
These are the most common Bundler commands you will use in any Ruby project.
1. bundle exec
Run a command using the project's gems.
2. bundle add
Add a gem and install it.
3. bundle update
Update gems to newer allowed versions.
4. bundle outdated
See which gems have newer versions available.
Loading Gems in Your App
In a script, use require explicitly. In a Bundler-managed app (like Sinatra or Rails), Bundler.require loads all Gemfile gems automatically.
1. Explicit require
Load one gem manually.
2. Bundler.require — Load All Gems
Auto-load all Gemfile gems.