Ruby keeps every gem version, and every Ruby you installed
Gems accumulate per Ruby version, and version managers keep each Ruby in full. The commands that clean both, and what breaks if you get it wrong.
Ruby's storage problem has two layers. Gems are kept at every version any project has ever needed, and if you use a version manager, that entire collection exists separately for every Ruby you have installed.
Measure both layers
du -sh ~/.rbenv ~/.rvm ~/.gem 2>/dev/null
gem env home
du -sh "$(gem env home)" 2>/dev/nullgem env home prints the gem directory for the Ruby currently active, which is the one you are about to clean. With a version manager there is one of these per installed Ruby, and the others are not affected by anything you run today.
The command that removes old versions
gem cleanup --dryrun
gem cleanupgem cleanup removes old versions of installed gems, keeping the newest of each. Run the dry run first and read it, because a project pinned to an older version through a Gemfile.lock will need that version reinstalled afterwards. In a Bundler project that is one bundle install away, which is why this is usually safe rather than always safe.
The documentation nobody reads
Every gem installs its documentation by default, and on a machine with hundreds of gems that is a meaningful share of the folder. Nothing on a modern setup reads it locally.
du -sh "$(gem env home)/doc" 2>/dev/null
gem cleanup && rm -rf "$(gem env home)/doc"
echo 'gem: --no-document' >> ~/.gemrc # stop it happening againRubies you no longer run
Each installed Ruby is a full build plus its own gems. List them and remove what nothing pins:
rbenv versions # or: rvm list
cat ~/**/.ruby-version 2>/dev/null | sort -uThe second line prints every Ruby version pinned by a project on the machine. Anything installed that is not in that list is a candidate for removal, which is the same approach that works for the Node versions nvm keeps.
Common questions
How do I clean up old Ruby gems?
Run gem cleanup, which removes older versions of installed gems and keeps the newest of each. Use gem cleanup --dryrun first to see what would go, since a project pinned to an older version will need it reinstalled.
Where are Ruby gems stored on a Mac?
Run gem env home to print the location for the Ruby currently active. With rbenv or rvm there is a separate gem directory per installed Ruby, which is why the total is larger than any single directory suggests.
Can I delete gem documentation?
Yes. Local documentation is installed by default and almost never read. Removing the doc directory inside the gem home is safe, and adding gem: --no-document to ~/.gemrc stops it being generated again.
Is it safe to remove an old Ruby version?
Yes, provided no project pins it. Check the .ruby-version files across your projects first. Removing a Ruby also removes the gems installed under it, and both are reinstallable.