Back to Scaling Ruby Applications guides

Understanding Ruby Gems: How to Find, Install, and Use

Stanley Ulili
Updated on September 3, 2025

RubyGems is Ruby's package management system that allows you to easily find, install, and manage libraries (called gems) for your Ruby projects. It eliminates the complexity of manually downloading and configuring third-party code, providing a streamlined way to extend Ruby's functionality.

This article will walk you through mastering Ruby gems and show you how to optimize your Ruby development workflow.

Prerequisites

Before you start using gems effectively, it's helpful to have Ruby installed and basic familiarity with the command line. RubyGems comes bundled with Ruby installations, so if you have Ruby, you already have the gem command available.

Why use Ruby gems?

Before diving into gem usage, it helps to understand what makes them essential for Ruby development:

  • Avoid reinventing the wheel: Need HTTP requests, authentication, or testing tools? Gems provide battle-tested solutions for common problems.
  • Manage dependencies cleanly: Gems handle version conflicts and dependencies automatically, keeping your projects organized.
  • Stay current with updates: Update gems with simple commands to get bug fixes and new features without manual downloads.
  • Share code easily: Package your own reusable code as gems for other developers or future projects.

Gems are essential if you're building anything beyond basic Ruby scripts. They provide the building blocks that make Ruby development productive and enjoyable.

Installing your first gem

In this section, you'll install a gem and see how it extends Ruby's capabilities.

Let's install HTTParty, a popular gem for making HTTP requests:

 
gem install httparty
Output
Successfully installed httparty-0.21.0
1 gem installed

This downloads HTTParty and makes it available to all your Ruby programs. You can verify the installation by listing your gems:

 
gem list httparty
Output
*** LOCAL GEMS ***

httparty (0.23.1)

Now create a simple script to test the gem:

 
mkdir ruby-gems-demo && cd ruby-gems-demo
test_gem.rb
require 'httparty'

response = HTTParty.get('https://jsonplaceholder.typicode.com/posts/1')
puts "Title: #{response['title']}"
puts "Status: #{response.code}"
 
ruby test_gem.rb
Output
Title: sunt aut facere repellat provident occaecati excepturi optio reprehenderit
Status: 200

The gem works immediately after installation, demonstrating how gems extend Ruby's capabilities with just a require statement.

Managing gems with Bundler

While installing gems globally works for simple cases, real projects need better dependency management. Bundler solves this by letting you specify exactly which gems and versions your project needs.

First, install Bundler if you don't have it:

 
gem install bundler

Create a Gemfile in your project to specify dependencies:

Gemfile
source "https://rubygems.org"

gem "httparty", "~> 0.21"
gem "json", "~> 2.6"

Now install the specified gems:

 
bundle install
Output
Fetching gem metadata from https://rubygems.org/...........
Resolving dependencies...
Fetching bigdecimal 3.2.2
Fetching csv 3.3.5
Installing bigdecimal 3.2.2 with native extensions
Installing csv 3.3.5
Bundle complete! 2 Gemfile dependencies, 7 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.

Bundler creates a Gemfile.lock file that records exact versions for reproducible installations. Always commit both Gemfile and Gemfile.lock to version control.

To use bundled gems in your scripts:

bundled_script.rb
require 'bundler/setup'
require 'httparty'
require 'json'

puts "Using HTTParty version: #{HTTParty::VERSION}"
puts "Using JSON version: #{JSON::VERSION}"
 
bundle exec ruby bundled_script.rb
Output
Using HTTParty version: 0.23.1
Using JSON version: 2.13.2

The bundle exec command ensures your script uses the exact gem versions specified in your Gemfile.

Finding the right gems

With over 180,000 gems available, finding the right one requires some strategy. The official RubyGems.org repository is your starting point. When evaluating gems, look for high download counts, recent updates, and clear documentation - these usually indicate popular, well-maintained libraries.

Screenshot 2025-09-03 at 12.55.58 PM.png

Don't stop at the gem page itself. Most gems link to their GitHub repositories where you can see how active the project is. Check recent commits, open issues, and how maintainers respond to problems. A gem with dozens of unresolved issues and no recent activity might cause headaches later:

Screenshot 2025-09-03 at 12.57.28 PM.png

Popular categories typically have several good options:

  • HTTP clients: httparty, faraday, rest-client
  • Testing frameworks: rspec, minitest
  • Web frameworks: rails, sinatra, hanami
  • Database tools: activerecord, sequel
  • Authentication: devise, omniauth

The Ruby community is helpful when you're stuck between choices. Stack Overflow, Ruby forums, and even Reddit discussions often provide real-world experiences with different gems. These insights can reveal performance issues or ease-of-use differences that aren't obvious from documentation alone.

The key is balancing popularity with your specific needs. Sometimes a newer, less popular gem might be a better fit than the most downloaded option.

Project-specific gem management

Different projects often need different gem versions. Bundler handles this automatically, but understanding the workflow helps avoid conflicts.

In each project directory, your Gemfile specifies requirements:

Gemfile
source 'https://rubygems.org'

ruby '3.4.5' # Match your system Ruby version
gem 'httparty', '~> 0.21'
gem 'json', '~> 2.6'
group :development do
gem 'listen', '~> 3.8'
end
group :test do
gem 'rspec', '~> 3.12'
end

The ruby declaration tells Bundler which Ruby version this project expects. When there's a mismatch, Bundler stops installation to prevent compatibility issues. This protects you from installing gems that might not work with your Ruby version.

The ~> operator means "use this version or higher, but don't go to the next major version." This allows bug fixes while preventing breaking changes. For example, ~> 0.21 accepts 0.21.1 and 0.21.5, but not 0.22.0.

When you run bundle install, Bundler reads your Gemfile, resolves all dependencies, installs compatible versions, and creates Gemfile.lock with exact versions. This ensures everyone working on the project uses identical gem versions.

Update your Gemfile to use Ruby 3.4.5, then run:

 
bundle install
Output

Fetching gem metadata from https://rubygems.org/...........
Resolving dependencies...
Fetching diff-lcs 1.6.2
Fetching ffi 1.17.2 (arm64-darwin)
....
Installing rspec-expectations 3.13.5
Installing rspec-mocks 3.13.5
Fetching rspec 3.13.1
Installing listen 3.9.0
Installing rspec 3.13.1
Bundle complete! 4 Gemfile dependencies, 17 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.

Advanced gem management

Once you're comfortable with basic gem usage, you'll encounter situations where the standard workflow isn't enough. Real projects accumulate technical debt, dependencies drift out of date, and performance becomes a concern. Here are techniques that experienced Ruby developers use to keep their gem environments clean and efficient.

Keeping gems updated

Gem updates aren't just about getting new features - they often include critical security patches. The bundle outdated command shows which gems have newer versions available, but more importantly, it shows how far behind you are:

 
bundle outdated
Output
Fetching gem metadata from https://rubygems.org/...........
Resolving dependencies...

Bundle up to date!

This output tells a story. If you see gems that are many minor versions behind, you might be missing important fixes. When updating specific gems, start with the most critical ones - security-related gems should be your first priority:

 
bundle update httparty
Output
Resolving dependencies...
Resolving dependencies...
Bundler attempted to update httparty but its version stayed the same
Bundle updated!

The nuclear option is updating everything at once with bundle update, but this can break your application in subtle ways. Use it only when you have comprehensive tests and time to troubleshoot issues.

Installing gems without documentation

Gem documentation takes up significant disk space and installation time, especially for large gems like Rails. Most developers use online documentation anyway, making local docs unnecessary:

 
gem install gem_name --no-document

To make this permanent, create or modify your ~/.gemrc file:

 
echo 'gem: --no-document' >> ~/.gemrc

This single line can cut gem installation time in half and save gigabytes of disk space over time. Your deployment scripts will thank you for this optimization.

Cleaning up unused versions

Over time, your system accumulates old gem versions. Each bundle update leaves the previous version installed, and eventually you're storing dozens of versions of popular gems:

 
gem cleanup
Output
Cleaning up installed gems...
Clean up complete

This command removes old versions while preserving any that are still required by your projects. It's safe to run regularly and can free up substantial disk space. On a system that's been used for Ruby development for a year, gem cleanup often reclaims several gigabytes.

For more aggressive cleanup, you can see what would be removed without actually doing it:

 
gem cleanup --dry-run

The --dry-run flag (note the hyphen) shows what would be removed without actually doing it, letting you verify the cleanup won't break anything important. In your case, since you have a relatively clean system, there weren't any old gem versions to remove.

On systems that have been used for Ruby development for months or years, this command typically shows dozens of old gem versions that can be safely removed, often freeing up several gigabytes of disk space.

Final thoughts

Ruby gems transform Ruby from a good language into an incredibly productive ecosystem. With gems handling everyday tasks, you can focus on solving your unique problems instead of rebuilding standard functionality.

Start with popular, well-maintained gems for your projects. Use Bundler to manage dependencies cleanly, and don't be afraid to explore new gems that might improve your workflow.

If you want to learn more, check out the official RubyGems guides and explore gems on RubyGems.org.

Got an article suggestion? Let us know
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.