Uninstall all installed gems, in OSX?
Asked Answered
B

13

204

There are instances where I would like to revert and uninstall all previous gem installations.

For instance, I needed to assist a friend migrate their rails development machine to use RVM. As they had been previously using the system-wide gem, he was experiencing many headaches when working with multiple projects. Essentially, he was the poster-child for an RVM convert.

How can I elegantly uninstall all of the gems on his OSX system?

Beatitude answered 11/11, 2011 at 14:16 Comment(5)
Nowadays this is unnecessary. RVM knows where to look for the gems after you switch to the right Ruby version.Eberle
yes Y | gem uninstall -a -IHest
Just a quick note to @FranklinYu - this is not always the case. I've had instances where a globally installed copy of Nokogiri seemed to always be taking precedent over the RVM gemset, and the global one had to be uninstalled.Polar
@NathanCrause Then it’s a bug in RVM and should be reported. I’m pretty sure this isn’t the intended behavior of RVM.Eberle
@FranklinYu you're right, but until such an issue is resolved, there would still be the need for a way to purge all gems. I was only referring to the point of it being "unnecessary", which would be true in an ideal situation, but hey, things go sideways all the time.Polar
M
58

You could also build out a new Gemfile and run bundle clean --force. This will remove all other gems that aren't included in the new Gemfile.

Mettlesome answered 19/12, 2012 at 15:4 Comment(4)
best answer because test-unit cannot be uninstalled and kills the process.Glossematics
Yes, this is the best answer. I feel OP should choose this post 2.0.0.Futility
This is certainly working for me and much more elegant than other versionsGilchrist
This is a great technique. Just what I needed.Irrigation
B
471

Rubygems >= 2.1.0

gem uninstall -aIx

a removes all versions
I ignores dependencies
x includes executables

Rubgems < 2.1.0

for i in `gem list --no-versions`; do gem uninstall -aIx $i; done
Beatitude answered 11/11, 2011 at 14:18 Comment(8)
Duh, you beat me to the speed :)Miscreant
Then of course, you'll want to re-install a sane list of default gems which for me is: "gem install bundler json minitest rake rdoc"Sell
gem list | cut -d" " -f1 | sudo xargs gem uninstall -Iax -worked for meAccent
Does not work: ERROR: While executing gem ... (Gem::InstallError) gem "test-unit" cannot be uninstalled because it is a default gem (gem 2.0.3)Frankish
@Anthony Though this is a bit dangerous, you can suppress errors with for i in `gem list --no-versions`; do gem uninstall -aIx $i 2>/dev/null; doneTautomer
Just added sudo for i in sudo gem list --no-versions; do sudo gem uninstall -aIx $i; doneNnw
Please mention sudo since OP mentioned system-wide gem in OS X.Eberle
Hey, I'm writing a guide on this topic. Has anyone on gem < 2.1.0 tried this bash script to work around the errors?Sycosis
M
58

You could also build out a new Gemfile and run bundle clean --force. This will remove all other gems that aren't included in the new Gemfile.

Mettlesome answered 19/12, 2012 at 15:4 Comment(4)
best answer because test-unit cannot be uninstalled and kills the process.Glossematics
Yes, this is the best answer. I feel OP should choose this post 2.0.0.Futility
This is certainly working for me and much more elegant than other versionsGilchrist
This is a great technique. Just what I needed.Irrigation
D
31

A slighest different version, skipping the cut step, taking advantage of the '--no-version' option:

gem list --no-version |xargs gem uninstall -ax

Since you are removing everything, I don't see the need for the 'I' option. Whenever the gem is removed, it's fine.

Drench answered 14/2, 2012 at 7:7 Comment(4)
sudo shouldn't be necessary if the gems were installed by an ordinary user.Dissected
See my answer for a reason why I downvoted. I wanted to comment here but comments do not allow lines...Palmirapalmistry
good, can remove a lot of gems, but something dosen`t it like bundle, rake, rvmGehlbach
Very elegant but fails due to the inability to remove default gems. Suggest crftr's solution insteadDiscriminant
C
14

First make sure you have at least gem version 2.1.0

gem update --system
gem --version
# 2.6.4

To uninstall simply run:

gem uninstall --all

You may need to use the sudo command:

sudo gem uninstall --all
Coextensive answered 18/6, 2016 at 22:18 Comment(0)
P
11

Use either

$ gem list --no-version | xargs gem uninstall -ax

or

$ sudo gem list --no-version | xargs sudo gem uninstall -ax

Depending on what you want, you may need to execute both, because "gem list" and "sudo gem list" provide independent lists.

Do not mix a normal "gem list" with a sudo-ed "gem uninstall" nor the other way around otherwise you may end up uninstalling sudo-installed gems (former) or getting a lot of errors (latter).

Palmirapalmistry answered 28/9, 2012 at 16:49 Comment(1)
You are right, I edited my answer to exclude sudo. For both versions users can rely on your answer.Drench
B
10

If you are using Rubygems version 2.1.0 or later, you can try: gem uninstall --all.

Bearable answered 3/3, 2015 at 21:57 Comment(1)
It should be 'gem' not 'gems'.Coextensive
A
9

Rubygems >= 2.1.0

gem uninstall -aIx

If Terminal returns below error

ERROR:  While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory.

Then write above command as below

sudo gem uninstall -aIx

And enter your mac os account password Done!!

Aliphatic answered 29/1, 2016 at 4:4 Comment(1)
again same error after executing above command.Residuum
L
9

And for those of you who are here because you want to remove all gems with a certain prefix (ahem I'm looking at you aws-sdk!) you can run something like this:

gem list --no-version | grep "aws-sdk-" | xargs gem uninstall -aIx

Obviously put in your query instead of aws-sdk-. You need the -I in there to ignore dependencies.

Adopted form Ando's earlier answer

Lunatic answered 25/9, 2017 at 22:6 Comment(0)
S
7

If you like doing it using ruby:

ruby -e "`gem list`.split(/$/).each { |line| puts `gem uninstall -Iax #{line.split(' ')[0]}` unless line.strip.empty? }"

Cheers

Scribbler answered 21/11, 2012 at 10:46 Comment(3)
I am getting an error with this: ERROR: While executing gem ... (Gem::CommandLineError) Please specify at least one gem name (e.g. gem build GEMNAME)Urinary
@Urinary Fixed, hehe providing support to my answers on Stack Overflow :)Scribbler
This is handy in that it works in Windows where the command shell is... not good.Macleod
S
5

The only command helped me to cleanup all gems and ignores default gems, which can't be uninstalled

for x in `gem list --no-versions`; do gem uninstall $x -a -x -I; done
Scurf answered 22/4, 2013 at 15:34 Comment(0)
M
3

I did that not too long ago (same poster-child RVM switcher situation):

gem list | cut -d" " -f1 | sudo xargs gem uninstall -Iax

Takes the list of all gems (incl. version stuff), cuts it to keep only the gem name, then uninstalls all versions of such gems.

The sudo is only useful if you had gems installed system-wide, and should not be included unless necessary.

Miscreant answered 11/11, 2011 at 14:19 Comment(0)
D
2

When trying to remove gems installed as root, xargs seems to halt when it encounters an error trying to uninstall a default gem:

sudo gem list | cut -d" " -f1 | xargs gem uninstall -aIx
# ERROR:  While executing gem ... (Gem::InstallError)
#    gem "test-unit" cannot be uninstalled because it is a default gem


This won't work for everyone, but here's what I used instead:

sudo for gem (`gem list | cut -d" " -f1`); do gem uninstall $gem -aIx; done
Dinitrobenzene answered 29/3, 2013 at 18:51 Comment(0)
B
2
gem list --no-version | grep -v -e 'psych' -e 'rdoc' -e 'openssl' -e 'json' -e 'io-console' -e 'bigdecimal' | xargs sudo gem uninstall -ax

grep here is excluding default gems. All other gems will be uninstalled. You can also precede it with sudo in case you get permission issues.

Braddock answered 25/7, 2017 at 6:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.