如何卸载使用`bundle install`安装的所有gem

时间:2014-01-27 15:23:14

标签: ruby-on-rails bundler

如何在特定RoR项目中删除使用bundle install安装的所有gem。我不想卸载其他项目使用的宝石。

11 个答案:

答案 0 :(得分:26)

因为我们正在使用ruby,你可以做这样的事情我猜:

bundle list | ruby -e 'ARGF.readlines[1..-1].each {|l| g = l.split(" ");  puts "Removing #{g[1]}"; `gem uninstall --force #{g[1]} -v #{g[2].gsub(/\(|\)/, "")}`; }'

注意:只进行轻微测试。

答案 1 :(得分:12)

没有一种简单的方法可以删除所有宝石 - 更不用说删除特定捆绑中的宝石了。您可以尝试其中一些建议: Uninstall all installed gems, in OSX?

适应bundle show命令而不是gem list


对于未来,请尝试这种方法:

如果您在本地安装捆绑包(如下例所示),则宝石将不会安装在您的全局gem目录中。然后,您可以轻松删除安装文件夹以删除捆绑包中的所有宝石。

# install gems to project_root/vendor/bundle
bundle install --path vendor/bundle --without test

路径选项与其他所有人一样保存到.bundle / config,任何后续的bundle install调用都会使用它,除非您将其设置为其他内容或从配置中删除它!

答案 2 :(得分:9)

您可以使用(如Tobias所说,如果您使用的是RVM)

rvm gemset empty [gemset]

直接在gemset上,例如

rvm gemset empty 2.0.0@server

答案 3 :(得分:3)

如果您正在使用rvm,您当然可以卸载并重新安装已安装宝石的ruby版本,即

% rvm use
Using /home/ubuntu/.rvm/gems/ruby-2.2.1
% rvm uninstall 2.2.1
ruby-2.2.1 - #removing rubies/ruby-2.2.1..
ruby-2.2.1 - #removing default ruby interpreter.............
% rvm install 2.2.1
Searching for binary rubies, this might take some time.
Found remote file https://rvm_io.global.ssl.fastly.net/binaries/ubuntu/14.0/x86_64/ruby-2.2.1.tar.bz2
Checking requirements for ubuntu.
Requirements installation successful.
ruby-2.2.1 - #configure
ruby-2.2.1 - #download
ruby-2.2.1 - #validate archive
ruby-2.2.1 - #setup 
ruby-2.2.1 - #gemset created /home/ubuntu/.rvm/gems/ruby-2.2.1@global
ruby-2.2.1 - #importing gemset /home/ubuntu/.rvm/gemsets/global.gems..............................
ruby-2.2.1 - #generating global wrappers........
ruby-2.2.1 - #gemset created /home/ubuntu/.rvm/gems/ruby-2.2.1
ruby-2.2.1 - #importing gemsetfile /home/ubuntu/.rvm/gemsets/default.gems evaluated to empty gem list
ruby-2.2.1 - #generating default wrappers........

现在你有一个红宝石环境,清除任何已安装的宝石。

答案 4 :(得分:3)

实际上就像

一样简单
gem list --no-versions | xargs gem uninstall -a

如果您没有使用RVM / RBENV,那么当gem尝试卸载可能失败的系统库时,您可能会遇到问题。在这种情况下,请逐个调用uninstall命令跳过这些。

gem list --no-versions | xargs -n1 gem uninstall -a

答案 5 :(得分:3)

注释掉Gemfile中的所有gem,然后运行

bundle clean --force

答案 6 :(得分:2)

https://makandracards.com/jan0sch/9537-uninstall-all-ruby-gems-from-your-system开始的另一种方式(类似于rainkinz's answerRalph's comment)。以下是一些变体:

# if you're the root user:
gem list | cut -d" " -f1 | xargs -I % gem uninstall -aIx %

# if you're a non-root user:
gem list | cut -d" " -f1 | xargs -I % sudo gem uninstall -aIx %

# docker compose (if your service is named "web" running the root user):
docker-compose run web bash -c 'gem list | cut -d" " -f1 | xargs -I % gem uninstall -aIx %'

####

gem install bundler
# or if using docker compose:
docker-compose run web gem install bundler

# optionally reinstall gems:
bundle install
# or if using docker compose:
docker-compose run web bundle install

打破这一点:

  • gem list列出所有宝石
  • cut -d" " -f1占据第一列
  • xargs -I % gem uninstall -aIx %以每行输出作为参数调用gem uninstall -aIx

请注意,我将-I指定为%的参数,并直接将其传递以确保安全性:

xargs -I % gem uninstall -aIx %

代替:

xargs gem uninstall -aIx

这是因为xargs存在一个安全问题,其中-n之类的选项可以传递到其命令并导致意外的结果。这可以通过以下示例进行演示:

# correctly prints "-n hello" (with trailing newline):
echo '-n Hello' | xargs -I % echo % | xargs -I % echo %

# incorrectly prints "hello" (without trailing newline):
echo '-n Hello' | xargs echo

答案 7 :(得分:0)

找到卸载除默认值以外的所有gem的解决方案:

Crete delete_gems.rb with

#!/usr/bin/env ruby
# Remove all gems EXCEPT defaults :)

`gem list -d`.split(/\n\n^(?=\w)/).each do |data|
  match = data.match(/(?<name>([^\s]+)) \((?<versions>.*)\)/)
  name = match[:name]
  versions = match[:versions].split(', ')

  if match = data.match(/^.*\(([\d\.]*),? ?default\): .*$/)
    next if match[1].empty? # it's the only version if this match is empty
    versions.delete(match[1] || versions[0])
  end

  versions.each { |v| system "gem uninstall -Ix #{name} -v #{v}" }
end

运行此脚本:

sudo chmod 1777 delete_gems.rb
./delete_gems.rb

除默认设置外,所有宝石都将被删除。 这里链接原始解决方案 http://zanshin.net/2013/06/10/how-to-delete-all-ruby-gems/

答案 8 :(得分:0)

如果您的问题类似于我的问题,那么您想卸载在测试GemFile更改时安装的所有gem,在这种情况下,我只是使用了

bundle clean

这卸载了GemFileGemFile.lock中未指定的所有gem。

我尚未对其进行测试,但是我想您可以从Gemfile中删除所有行,然后运行上面的命令以摆脱ROR项目在当前目录中安装的所有gem。

答案 9 :(得分:0)

就像其他人提到的那样,您可以使用rvm。列出所有可用的宝石集

rvm gemset list

然后

rvm gemset empty <gemset-name>

答案 10 :(得分:0)

我使用的是 docker 设置,以下是我如何在不重建 bundler 的情况下恢复由 docker 安装的 gem。


准备一份 GemfileGemfile.lock 的备份副本。

暂时删除您的 Gemfile 的内容(不要删除文件,只需将其留空)然后(可选)删除 {{1}并运行:

Gemfile.lock

这应该会删除 bundle clean --force 安装的所有 gem。

然后您可以使用备份副本或 bundler 还原 GemfileGemfile.lock

相关问题