Uninstall old versions of Ruby gems

RubyGem

Ruby Problem Overview


I have several versions of a Ruby gem:

$ gem list
rjb (1.3.4, 1.3.3, 1.1.9)

How can I remove old versions but keep the most recent?

Ruby Solutions


Solution 1 - Ruby

# remove all old versions of the gem
gem cleanup rjb

# choose which ones you want to remove
gem uninstall rjb

# remove version 1.1.9 only
gem uninstall rjb --version 1.1.9

# remove all versions less than 1.3.4
gem uninstall rjb --version '<1.3.4'

Solution 2 - Ruby

For removing older versions of all installed gems, following 2 commands are useful:

 gem cleanup --dryrun

Above command will preview what gems are going to be removed.

 gem cleanup

Above command will actually remove them.

Solution 3 - Ruby

Way to clean out any old versions of gems.

sudo gem cleanup

If you just want to see a list of what would be removed you can use:

sudo gem cleanup -d

You can also cleanup just a specific gem by specifying its name:

sudo gem cleanup gemname

for remove specific version like 1.1.9 only

gem uninstall gemname --version 1.1.9

If you still facing some exception to install gem, like:

invalid gem: package is corrupt, exception while verifying: undefined method `size' for nil:NilClass (NoMethodError) in /home/rails/.rvm/gems/ruby-2.1.1@project/cache/nokogiri-1.6.6.2.gem

the, you can remove it from cache:

rm /home/rails/.rvm/gems/ruby-2.1.1@project/cache/nokogiri-1.6.6.2.gem

For more detail:

http://blog.grepruby.com/2015/04/way-to-clean-up-gem-or-remove-old.html

Solution 4 - Ruby

Try something like gem uninstall rjb --version 1.3.4.

Solution 5 - Ruby

gem cleanup uses system commands. Installed gems are just directories in the filesystem. If you want to batch delete, use rm -R.

  1. gem environment and note the value of GEM PATHS
  2. cd <your-gem-paths>/gems
  3. ls -1 |grep rjb- |xargs rm -R

Solution 6 - Ruby

You might need to set GEM_HOME for the cleanup to work. You can check what paths exist for gemfiles by running:

gem env

Take note of the GEM PATHS section.

In my case, for example, with gems installed in my user home:

export GEM_HOME="~/.gem/ruby/2.4.0"
gem cleanup

Solution 7 - Ruby

bundler clean

Stopped the message showing for me, as a last step after I tried all of the above.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPhilippe BlayoView Question on Stackoverflow
Solution 1 - RubyDylan MarkowView Answer on Stackoverflow
Solution 2 - RubyohhoView Answer on Stackoverflow
Solution 3 - Rubyuser3118220View Answer on Stackoverflow
Solution 4 - RubyDaniel O'HaraView Answer on Stackoverflow
Solution 5 - RubyAnatolyView Answer on Stackoverflow
Solution 6 - RubylkraiderView Answer on Stackoverflow
Solution 7 - RubyMikeView Answer on Stackoverflow