Question

How to Remove Old Ruby Gem Versions and Keep the Latest

rubyrubygems

Question

I have multiple installed versions of the same Ruby gem:

gem list
# rjb (1.3.4, 1.3.3, 1.1.9)

How can I remove the older versions of a gem while keeping only the most recent version installed?

Short Answer

By the end of this page, you will understand how RubyGems stores multiple versions of the same gem, how to uninstall specific old versions, and how to safely clean up outdated gem versions while keeping the latest one available.

Concept

RubyGems allows multiple versions of the same gem to be installed at the same time. This is useful because different projects may depend on different gem versions.

For example, one older application might require rjb 1.1.9, while a newer project uses rjb 1.3.4. Ruby can load the version requested by the project, so keeping multiple versions can prevent compatibility problems.

However, over time, old gem versions can accumulate and clutter your environment. Removing unused versions helps:

  • save disk space
  • reduce confusion when inspecting installed gems
  • simplify system maintenance
  • keep your development environment cleaner

In RubyGems, you can uninstall:

  • a specific version of a gem, or
  • all old versions except the newest using a cleanup command

The key idea is that gem management is version-aware. You are not just removing a gem by name; you are often managing one installed version at a time.

Mental Model

Think of a gem like an app stored in several editions on your computer:

  • rjb 1.1.9 is an older edition
  • rjb 1.3.3 is a newer edition
  • rjb 1.3.4 is the newest edition

RubyGems keeps all these editions on the shelf unless you remove them.

Cleaning old gem versions is like keeping the latest edition of a book and recycling the outdated copies you no longer need. If you know exactly which edition to remove, you can remove it directly. If you want the system to tidy up older copies automatically, you use a cleanup command.

Take Quiz

Syntax and Examples

To remove a specific old gem version, use gem uninstall with the gem name and version.

gem uninstall rjb -v 1.3.3
gem uninstall rjb -v 1.1.9

This keeps version 1.3.4 installed.

If you want RubyGems to remove older versions automatically and keep the latest installed version, use:

gem cleanup rjb

To clean up old versions of all gems, use:

gem cleanup

Example

Suppose your installed versions are:

rjb (1.3.4, 1.3.3, 1.1.9)

Running:

gem cleanup rjb

will typically remove 1.3.3 and 1.1.9, leaving:

rjb (1.3.4)

When to use each command

  • Use gem uninstall <name> -v <version> when you want precise control.
  • Use gem cleanup <name> when you want RubyGems to remove older installed versions for you.

Step by Step Execution

Consider this command:

gem cleanup rjb

Here is what happens conceptually:

  1. RubyGems looks for all installed versions of the rjb gem.
  2. It identifies the newest installed version.
  3. It checks which older versions are no longer needed as the latest installed copy.
  4. It removes those older versions.
  5. The most recent version remains installed.

Trace example

Installed:

rjb (1.3.4, 1.3.3, 1.1.9)

Command:

gem cleanup rjb

Result:

rjb (1.3.4)

Manual version removal trace

Command:

gem uninstall rjb -v 1.1.9

Step by step:

  1. RubyGems finds installed versions of rjb.
  2. It targets only version 1.1.9.
  3. It removes that version.
  4. Versions 1.3.3 and 1.3.4 remain untouched.

Real World Use Cases

Cleaning old gem versions is useful in many practical situations:

  • Local development machines: Remove outdated versions after upgrading a gem.
  • CI environments: Keep build agents cleaner and reduce unnecessary installed packages.
  • Shared servers: Avoid confusion when many gems and versions have built up over time.
  • Docker images: Smaller images can result from removing unneeded gem versions.
  • Troubleshooting: When checking installed gems, seeing only the active version makes debugging easier.

Example scenarios

  • You upgraded bundler, rake, or rails, and older versions are no longer needed.
  • A legacy project was removed, so the gem versions it depended on can now be deleted.
  • A script that audits system dependencies is easier to read when only current versions remain installed.
Take Quiz

Real Codebase Usage

In real projects, developers usually do not remove gems blindly. They consider how the project manages dependencies.

Common patterns

  • Bundler-managed apps: Projects often rely on Gemfile.lock to pin exact gem versions.
  • Safe cleanup after upgrades: Developers update gems, run tests, and only then clean old versions.
  • Environment-specific maintenance: Cleanup may be done in a development shell, CI job, or Docker build step.
  • Validation before deletion: Teams check whether any older app still depends on an older gem version.

Practical workflow

  1. Update dependencies.
  2. Run the application or test suite.
  3. Confirm the new version works.
  4. Run cleanup for unused older versions.

Related maintenance commands

Developers commonly combine this concept with:

  • checking installed versions using gem list
  • verifying dependency resolution with Bundler
  • uninstalling a specific version if cleanup is too broad

In short, real codebases treat gem cleanup as part of dependency maintenance, not just a one-off command.

Common Mistakes

A common beginner mistake is assuming only one version of a gem can be installed. RubyGems allows multiple versions, so seeing several versions is normal.

Mistake 1: Removing the whole gem accidentally

Broken approach:

gem uninstall rjb

Depending on your setup, this may prompt for multiple versions or remove more than you intended.

Better approach:

gem uninstall rjb -v 1.1.9
gem uninstall rjb -v 1.3.3

Mistake 2: Cleaning without checking project dependencies

If an older project still requires an older version, deleting it may break that project.

How to avoid it:

  • check your Gemfile.lock in existing projects
  • confirm which version is actually used
  • run tests after cleanup

Mistake 3: Forgetting that Bundler may manage versions

A gem installed globally is not always the same thing as the gem version your project uses through Bundler.

How to avoid it:

  • inspect Gemfile and Gemfile.lock
  • use bundle exec when appropriate

Mistake 4: Expecting cleanup to upgrade gems

gem cleanup removes old versions. It does not install the newest available version from the internet.

To upgrade, you would install or update separately, then clean old versions.

Mistake 5: Running commands without permissions

On some systems, you may need elevated permissions depending on how Ruby and gems were installed.

How to avoid it:

  • prefer user-managed Ruby environments like rbenv or RVM
  • understand whether gems are installed system-wide or per-user

Comparisons

TaskCommandBest when
Remove one exact versiongem uninstall rjb -v 1.3.3You want precise control
Remove old versions of one gemgem cleanup rjbYou want to keep the latest version only
Remove old versions of all gemsgem cleanupYou want to clean your full gem environment

gem uninstall vs gem cleanup

CommandWhat it doesRisk levelControl
gem uninstallRemoves the version you specifyLower if version is specifiedHigh
gem cleanupRemoves older installed versions automaticallyMedium if you have legacy projectsMedium

Manual cleanup vs automatic cleanup

ApproachProsCons
Manual with gem uninstall -vExact and predictableSlower if many versions exist
Automatic with gem cleanupFast and convenientEasier to remove versions an older project still needs
Take Quiz

Cheat Sheet

# List installed gems
gem list

# Remove a specific version of a gem
gem uninstall GEM_NAME -v VERSION

# Example
gem uninstall rjb -v 1.3.3

# Remove old versions of one gem, keep the latest
gem cleanup GEM_NAME

# Example
gem cleanup rjb

# Remove old versions of all gems
gem cleanup

Key rules

  • RubyGems can store multiple installed versions of the same gem.
  • gem uninstall gives precise control.
  • gem cleanup is the quick way to remove outdated installed versions.
  • Always verify that no existing project still depends on the older version.
  • gem cleanup cleans old versions; it does not upgrade gems.

Safe habit

  1. Check installed versions.
  2. Confirm project dependencies.
  3. Remove specific old versions or run cleanup.
  4. Test your projects.

FAQ

How do I remove old versions of a Ruby gem but keep the newest one?

Use:

gem cleanup GEM_NAME

For example:

gem cleanup rjb

Can I uninstall just one specific gem version?

Yes. Use:

gem uninstall GEM_NAME -v VERSION

Example:

gem uninstall rjb -v 1.1.9

Does gem cleanup upgrade my gem?

No. It only removes older installed versions. It does not fetch or install newer releases.

Why does Ruby allow multiple versions of the same gem?

Different projects may require different gem versions. Keeping multiple versions helps maintain compatibility.

Is it safe to run gem cleanup?

Usually yes, but check whether older projects still depend on older gem versions before removing them.

Should I use gem cleanup or gem uninstall?

Use gem uninstall when you want exact control. Use gem cleanup when you want a quick cleanup of outdated versions.

What if my project breaks after removing an older version?

Reinstall the required version and check your project's dependency files such as Gemfile.lock.

Take Quiz

Mini Project

Description

Create a small Ruby environment maintenance checklist using gem commands. The goal is to practice listing installed gem versions, removing a specific old version, and cleaning up outdated versions safely. This mirrors a real maintenance task developers perform after upgrading dependencies.

Goal

Practice managing installed Ruby gem versions without removing the latest version by mistake.

Requirements

  1. List installed versions of a gem using gem list.
  2. Remove one specific old version with gem uninstall -v.
  3. Use gem cleanup on a gem that has multiple versions installed.
  4. Verify afterward that the latest version still remains installed.
Take Quiz

Keep learning

Related questions

View more