Uninstalling all gems Ruby 2.0.0
Asked Answered
C

4

47

It seems that ruby 2.0.0 has added "default" gems to the mix and makes them non removable by gem uninstall.

How can you remove all non default gems?

Countersign answered 26/2, 2013 at 22:19 Comment(6)
Can you provide example of gems & how you installed Ruby 2.0 (manually? rvm?)Announce
I used rbenv to install ruby 2.0.0-p0. Gems that are "default" for me at least are: bigdecimal 1.2.0, io-console 0.4.2, json 1.7.7, minitest 4.3.2, psych 2.0.0, rake 0.9.6, rdoc 4.0.0, test-unit 2.0.0.0. gem uninstall json errors with gem "json" cannot be uninstalled because it is a default gem.Countersign
Sorry, misunderstood. I never delete gems, not sure one would. Why do you need anything else than gem cleanup?Announce
Sometimes I just want to get rid of things. Cleanup is probably what most people need most of the time. There are times however I want to get rid of all gems to test bash scripts that need to install things like gems.Countersign
rvm has gemsets, not sure about rbenvAnnounce
I'm working with a system atm that has ruby without rvm or rbenv. But I agree if I has rvm a simple rvm gemset empty would do :)Countersign
K
87

I used this one line script.

for i in `gem list --no-versions`; do gem uninstall -aIx $i; done

It ignores default gem errors and just proceeds. Simple and self-evident.

Krupp answered 28/2, 2013 at 11:10 Comment(6)
That's a simpler solution for this problem, assuming rubygems continues to work this way :)Countersign
Nice work! You don't see a lot of terse shell commands posted using a loop.Yearround
Does not work. still get 'cannot be uninstalled because it is a default gem' errors.Dorsum
@Dorsum It's normal. You can just ignore it. If you mean the command stops after the first error, type set +e and try again.Krupp
@Krupp would know if this works gem list --no-version | awk 'NR>1{ print $(NF-1) }' | xargs gem 2>/dev/null uninstall -aIx it doesn't error but it also doesn't uninstall the gems. I assume the command still stops executing when error occurs (even if it gets redirected)Ultravirus
If using zsh, you can be even shorter (only in the cli, not as a script), for i in $(gem list --no-versions); gem uninstall -aIx $i.Pharyngology
S
13

First, go to the gems directory Like ../ruby/2.0.0-p195/lib/ruby/gems/2.0.0/specifications
You will find a directory named default, which including all the default gems shipped with ruby 2.0

Move all the *.gemspec stored in default dir to specifications dir and remove the empty default dir.

Then you can do whatever you want like old days.:-)

Schumer answered 30/6, 2013 at 10:12 Comment(2)
What is the specification directory used for now? It's in the load path I assume?Countersign
This worked for me. The "solutions" in other SO questions, github & even the ruby gem docs did not work.Penny
C
6

I wrote a script in ruby to remove all non default gems.

https://gist.github.com/nixpulvis/5042764

This is needed now because unlike before 2.0.0 some gems are labeled "default" with the installation of ruby, and cannot be uninstalled with gem uninstall. This makes the previously popular methods for deleting all gems not work.

For reference here it is.
gem list | cut -d" " -f1 | xargs gem uninstall

Countersign answered 26/2, 2013 at 22:19 Comment(4)
The supplied link no longer exists. "OH NOES, 404". Is there somewhere else this script exists, or is the "for reference" line the entire script?Punic
Thanks. I changed my username. The URL is fixed.Countersign
this crashes for default gemsLegume
this doesn't work for cases where there are multiple versions for any specific gemPentangular
F
0

I have not yet found a better answer than to exclude the "default" gems:

/usr/local/bin/gem list --no-versions | \
grep -v -E "(bigdecimal|io-console|json|minitest|psych|rake|rdoc|test-unit)" | \
xargs --no-run-if-empty /usr/local/bin/gem uninstall --executables --user-install --all --force
Flagstaff answered 11/2, 2014 at 18:13 Comment(1)
This code is assuming you installed ruby 2.0 from source on Linux.Flagstaff

© 2022 - 2024 — McMap. All rights reserved.