412

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?

Philippe Blayo
  • 10,610
  • 14
  • 48
  • 65

7 Answers7

731
# 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'
Zombo
  • 1
  • 62
  • 391
  • 407
Dylan Markow
  • 123,080
  • 26
  • 284
  • 201
293

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.

Zia Ul Rehman Mughal
  • 2,119
  • 24
  • 44
ohho
  • 50,879
  • 75
  • 256
  • 383
  • 3
    Doesn't work in my ubuntu cloud9 setup ... have to run with sudo – Mirv - Matt Nov 13 '18 at 16:46
  • 3
    As of the current version of gem, 3.2.15, I get the following message. The `--dryrun` option has been deprecated and will be removed in future versions of Rubygems. Use `--dry-run` instead – Ahmed Mohamedeen Apr 20 '21 at 19:11
  • On Rails, I had to run bundle install after these two commands to re-install some gems that went missing such as puma. – Asad Shakil May 23 '22 at 04:16
15

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

user3118220
  • 1,438
  • 12
  • 16
13

Try something like gem uninstall rjb --version 1.3.4.

Daniel O'Hara
  • 13,307
  • 3
  • 46
  • 68
  • 1
    the question was "How can I remove old versions but keep the most recent?" this response would remove the newest version. – austinheiman Oct 06 '17 at 16:40
8

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
eebbesen
  • 5,070
  • 8
  • 48
  • 70
Anatoly
  • 15,298
  • 5
  • 53
  • 77
1

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
lkraider
  • 4,111
  • 3
  • 28
  • 31
-1
bundler clean

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

Mike
  • 87
  • 1
  • 2