How can I remove a default gem? ! want to uninstall a gem 1.7.7 version of json
Asked Answered
H

4

44

I have the same rails app in OSX and Ubuntu, I want to use Zeus to speed up my rspec. In Ubuntu, Zeus starts Ok, but in OSX it always be crashed. At last I find the issue, https://github.com/burke/zeus/issues/237#issuecomment-18700462 the difference between OSX and Ubuntu is the version of json gem. I use gem list | grep json Ubuntu shows

json (1.8.1, 1.8.0, 1.5.3)
json_pure (1.5.3)
json_spec (1.1.1)
jsonpath (0.5.3)
multi_json (1.8.2, 1.7.8, 1.0.3)

Mac shows

json (1.8.1, 1.7.7)
json_spec (1.1.1)
jsonpath (0.5.5, 0.5.3)
multi_json (1.8.2, 1.7.8)

so I want to uninstall 1.7.7 version of json gem to make zeus start, but

gem uninstall json -v 1.7.7
ERROR:  While executing gem ... (Gem::InstallError)
gem "json" cannot be uninstalled because it is a default gem

What should I do?

Housemother answered 6/1, 2014 at 7:31 Comment(5)
possible duplicate of Uninstalling all gems Ruby 2.0.0Specie
It is dangerous to remove default gem, instead of it create other gem environment with rbenv/rvm and inside of a gemset replace the gem by a newer version.Ypres
@JavierCadiz thanks, at last i find the solution here uninstalling-all-gems-ruby-2-0-0Housemother
honestly, not every environment uses ram or a version manager. We run about 20 services and each in its own docker container and in that container. because it's running just 1 service or app, only needs to have the exact gems in system gems. Which of course makes the OOP question valid.Lauer
It is just still so bad. You can not cleanly remove a default gem no matter what, and you can have 2 default versions of a gem at the same time. Just bad.Hick
K
72

So based off what I can tell there is no easy command that can move the gemspec file from the default folder to the non-default folder. This is a good thing from what I can tell but here are the instructions on how to do this by hand.

  1. Find the location of the default spec. The easiest way is to go into irb and run the following command:

    irb(main):002:0> Gem.default_specifications_dir
    => "/Users/user/.rubies/ruby-2.5.7/lib/ruby/gems/2.5.0/specifications/default"
    

    For older rubygems it's:

    irb(main):001:0> File.join Gem::Specification.default_specifications_dir
    => "/Users/newdark/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/specifications/default"
    

This is the line of code that builds the gemspec path https://github.com/rubygems/rubygems/blob/v2.6.13/lib/rubygems/installer.rb#L420

  1. Once you get the file path you just need to move the gem name and version from the default folder to the parent folder.

    $ cd /Users/newdark/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/specifications/default
    $ mv json-1.7.7.gemspec ../
    

if you do gem list -d you should no longer see the words Installed at (default) next to the gem version json-1.7.7. you can then run gem uninstall json -v 1.7.7 with out it fighting you. If you want to undo all this just run gem install json -v 1.7.7 --default

Karinkarina answered 11/10, 2017 at 14:39 Comment(3)
Further more, sudo rm -rf /Library/Ruby/Gems/2.6.0/json-1.7.7 might be necessary before installing a newer version.Misgiving
Followed many guides, this is the only one that worked. Thanks a bunchEnfield
For those of you cannot enter irb or console due to conflict with a default gem, the path to default director can be found by running gem environment and then going to INSTALLATION DIRECTORY -> specifications -> default and then removing the default gemAmadus
M
19

I have been experiencing a problem with default versions of gems, and the accepted answer did not work for me. What worked for me was to install the same version as the default, but without the default flag, and then uninstall it.

gem install json -v '1.7.7'

Then once that is finished:

gem uninstall json -v '1.7.7'
Maduro answered 14/9, 2017 at 16:46 Comment(2)
This seems unlikely to work most of the time, since <ruby_install_dir>/lib/ruby/gems/<ruby_major_ver>/specifications/default will now have a copy of the .gemspec and so will the parent directory. After attempting this solution, I still had to remove the (now duplicate) json-x.y.z.gemspec file from my specifications/default directory.Emileemilee
It actually didn't work. You got a message telling you the uninstall command succeeded, but the actual uninstall action didn't as the gem was default. Please take a closer look.Dubuffet
C
9

open your terminal and then

Step 1

open interactive ruby

irb

Step 2

inside that use the command

Gem.default_specifications_dir

it will provide you an address like:

"/home/user/.rvm/rubies/ruby-2.7.1/lib/ruby/gems/2.7.0/specifications/default"

Step 3

then break out of irb and move to that address directory by

cd /home/user/.rvm/rubies/ruby-2.7.1/lib/ruby/gems/2.7.0/specifications/default

Step 4

just delete that gem's gemspec file by:

rm gemname-2.1.4.gemspec

Hope this works!

Coffeecolored answered 2/6, 2022 at 15:8 Comment(0)
K
7

may be this will help you....

bundle exec gem uninstall GEM_NAME

if above cmd not work then try this

execute this either in irb or in a script proper:

`gem list --no-versions`.split("\n").each do |gem|
  `gem list -d #{gem}`.gsub(/Installed at(.*):.*/).each do |dir|
    dir = dir.gsub(/Installed at(.*): /,'').gsub("\n", '')
    system "gem uninstall #{gem} -aIx -i #{dir}"
  end  
end

if above both are fails then try this

go to your rvm dir.. where all gems are install then manually remove that gem which you want.. such as in my case my gem dir location is /home/user_name/.rvm/gems/ruby-1.9.3-p194/gems

Kinsfolk answered 6/1, 2014 at 9:53 Comment(2)
Hi HarsHarl, i did as you said , but it shows ERROR: Gem names and --all may not be used together,and zeus still can not run.Housemother
then go to your rvm dir.. where all gems are install then manually remove that gem which you want.. such as in my case my gem dir location is /home/user_name/.rvm/gems/ruby-1.9.3-p194/gemsKinsfolk

© 2022 - 2024 — McMap. All rights reserved.