Do I have to manually uninstall all dependent gems

2020-02-02 11:50发布

问题:

I tried to uninstall datamapper using the command gem uninstall dm-core.

But it seems that a whole bunch of dependent gems also need to be uninstalled.

C:\>gem uninstall dm-core

You have requested to uninstall the gem:
        dm-core-0.9.11
dm-migrations-0.9.11 depends on [dm-core (= 0.9.11)]
dm-cli-0.9.11 depends on [dm-core (= 0.9.11)]
dm-serializer-0.9.11 depends on [dm-core (= 0.9.11)]
dm-timestamps-0.9.11 depends on [dm-core (= 0.9.11)]
dm-aggregates-0.9.11 depends on [dm-core (= 0.9.11)]
dm-types-0.9.11 depends on [dm-core (= 0.9.11)]
dm-is-tree-0.9.11 depends on [dm-core (= 0.9.11)]
dm-observer-0.9.11 depends on [dm-core (= 0.9.11)]
dm-validations-0.9.11 depends on [dm-core (= 0.9.11)]
If you remove this gems, one or more dependencies will not be met.
Continue with Uninstall? [Yn]  n
ERROR:  While executing gem ... (Gem::DependencyRemovalException)
    Uninstallation aborted due to dependent gem(s)

I tried finding documentation on "gem uninstall" but there doesn't seem to be a way to uninstall the dependencies automatically:

C:\>gem help uninstall
Usage: gem uninstall GEMNAME [GEMNAME ...] [options]

  Options:
    -a, --[no-]all                   Uninstall all matching versions
    -I, --[no-]ignore-dependencies   Ignore dependency requirements while
                                     uninstalling
    -x, --[no-]executables           Uninstall applicable executables with
out
                                     confirmation
    -i, --install-dir DIR            Directory to uninstall gem from
    -n, --bindir DIR                 Directory to remove binaries from
        --[no-]user-install          Uninstall from user's home directory
                                     in addition to GEM_HOME.
    -v, --version VERSION            Specify version of gem to uninstall
        --platform PLATFORM          Specify the platform of gem to uninst
all

  Common Options:
    -h, --help                       Get help on this command
    -V, --[no-]verbose               Set the verbose level of output
    -q, --quiet                      Silence commands
        --config-file FILE           Use this config file instead of defau
lt
        --backtrace                  Show stack backtrace on errors
        --debug                      Turn on Ruby debugging


  Arguments:
    GEMNAME       name of gem to uninstall

  Summary:
    Uninstall gems from the local repository

  Defaults:
    --version '>= 0' --no-force --install-dir C:/Ruby18/lib/ruby/gems/1.8
    --user-install

C:\>

Am I missing something?

回答1:

As far as I know you're correct, there is not an easy way built-in to the gem command to do this.

However, you can check out gem-prune which can help clean up your gem repository after you've removed dm-core.

http://github.com/ddollar/gem-prune/tree/master



回答2:

gem list | cut -d" " -f1 | xargs gem uninstall -aIx deletes all installed ruby gems!



回答3:

I ended up making a simple command line tool to gem uninstall dependencies recursively.

I also filed a rubygems issue to gem uninstall dependencies recursively.


That rubygems issue was closed and will not be considered until somebody provides a patch including tests.



回答4:

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

Works the best for me, not sure why but

gem list | cut -d" " -f1 | xargs gem uninstall -aIx

doesn't work on my system as it still complains...

ERROR:  While executing gem ... (Gem::InstallError)
    cannot uninstall, check `gem list -d some-gem-here`


回答5:

The problem when running these sort of uninstalls is that they go down the list of gems in order so if a gum is uninstallable then you end up getting stuck. Run the below a few times and it should remove all the gems it is allowed to.

gem list | cut -d" " -f1 | sort -R | xargs -n1 gem uninstall -aIx


回答6:

gem cleanup should do the trick. See here for details.



回答7:

This snippet of code does it for me:

def gem_deps(name)
  if `gem dependency #{name}` =~ /(Gem #{name}-.*?)(Gem|\z)/m
    $1.split("\n").grep(/runtime\s*\)/).map do |line|
      line[/[\w-]+/]
    end.compact
  else
    []
  end
end

def gem_recursive_uninstall(name)
  deps = gem_deps(name)
  if deps.empty?
    system('sudo','gem','uninstall',name)
  else
    puts("Uninstall #{name} with dependencies: #{deps.join(', ')}? [y/n]")
    if gets.chomp[/y/]
      system(*(%w{sudo gem uninstall} + [name] + deps))
    end
  end
end

Taken from http://github.com/cldwalker/irbfiles/blob/master/.irb/libraries/gem.rb



回答8:

If you would like to use some wild cards to remove some gems (e.g. to remove some gems from a specific vendor) then you can pipe the output from gem list to grep as shown below

gem list --no-version | grep "opener-" | cut -d " " -f1  | xargs gem uninstall -aIx

The above command removes all the gems whose name begins with "opener-"



回答9:

Just list all the gems you want to uninstall e.g. gem uninstall dm-migrations dm-cli dm-observer. And try to manage your gems with Bundler whenever possible.