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.9is an older editionrjb 1.3.3is a newer editionrjb 1.3.4is 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.
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:
- RubyGems looks for all installed versions of the
rjbgem. - It identifies the newest installed version.
- It checks which older versions are no longer needed as the latest installed copy.
- It removes those older versions.
- 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:
- RubyGems finds installed versions of
rjb. - It targets only version
1.1.9. - It removes that version.
- Versions
1.3.3and1.3.4remain 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, orrails, 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.
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.lockto 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
- Update dependencies.
- Run the application or test suite.
- Confirm the new version works.
- 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.lockin 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
GemfileandGemfile.lock - use
bundle execwhen appropriate
Mistake 4: Expecting cleanup to upgrade gems
gem cleanup removes old versions. It does install the newest available version from the internet.
Comparisons
| Task | Command | Best when |
|---|---|---|
| Remove one exact version | gem uninstall rjb -v 1.3.3 | You want precise control |
| Remove old versions of one gem | gem cleanup rjb | You want to keep the latest version only |
| Remove old versions of all gems | gem cleanup | You want to clean your full gem environment |
gem uninstall vs gem cleanup
| Command | What it does | Risk level | Control |
|---|---|---|---|
gem uninstall |
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 uninstallgives precise control.gem cleanupis the quick way to remove outdated installed versions.- Always verify that no existing project still depends on the older version.
gem cleanupcleans old versions; it does not upgrade gems.
Safe habit
- Check installed versions.
- Confirm project dependencies.
- Remove specific old versions or run cleanup.
- 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.
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
- List installed versions of a gem using
gem list. - Remove one specific old version with
gem uninstall -v. - Use
gem cleanupon a gem that has multiple versions installed. - Verify afterward that the latest version still remains installed.
Keep learning
Related questions
Calling a Class Method from an Instance in Ruby
Learn how to call a class method from an instance in Ruby using self.class, with examples, pitfalls, and practical usage patterns.
Calling an Overridden Monkey-Patched Method in Ruby
Learn how to call the original method when monkey patching in Ruby, including alias_method patterns, examples, pitfalls, and practical usage.
Convert a Unix Timestamp to Ruby DateTime
Learn how to convert Unix timestamps to Ruby DateTime and Time objects, with examples, differences, pitfalls, and practical Ruby usage.