Ruby-on-rails 如何卸载使用`bundle install`安装的所有gem

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21384664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:17:34  来源:igfitidea点击:

How to uninstall all gems installed using `bundle install`

ruby-on-railsbundler

提问by nish

How can I remove all the gems installed using bundle installin a particular RoR project. I don't want to uninstall gems that are used by other projects.

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

回答by rainkinz

Since we're using ruby you could do something like this I guess:

由于我们使用的是 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(/\(|\)/, "")}`; }'

NOTE: Only lightly tested.

注意:仅经过轻微测试。

回答by KappaNossi

There's no one simple way to remove all gems - let alone removing those within a specific bundle. You could try some of these suggestions: Uninstall all installed gems, in OSX?

没有一种简单的方法可以删除所有宝石 - 更不用说删除特定捆绑包中的宝石了。您可以尝试以下一些建议: 在 OSX 中卸载所有已安装的 gems?

Adapt to the bundle showcommand instead of gem list

适应bundle show命令而不是gem list



For the future, try this approach:

对于未来,请尝试以下方法:

If you install your bundle locally like the example below, the gems won't be installed in your global gem directory. Then you can easily delete the installation folder to delete all gems of the bundle.

如果你像下面的例子一样在本地安装你的包,gem 将不会安装在你的全局 gem 目录中。然后您可以轻松删除安装文件夹以删除捆绑包的所有 gem。

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

The path option is saved to .bundle/config just like all others and any subsequent bundle installcalls will use it unless you set it to something else or remove it from the config!

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

回答by martincito

You can use (like Tobias says, if you are using RVM)

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

rvm gemset empty [gemset]

rvm gemset empty [gemset]

Directly on the gemset, for example

直接在宝石上,例如

rvm gemset empty 2.0.0@server

回答by lzap

It is actually as simple as

其实很简单

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

If you are not using RVM/RBENV, you might hit into issue when gem attempts to uninstall system library which can fail. In that case, call uninstall command one by one to skip these.

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

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

回答by Dan Kerchner

If you're using rvm, you could of course just uninstall and reinstall the version of ruby under which you've installed the gems, i.e.

如果您使用的是 rvm,您当然可以卸载并重新安装安装了 gems 的 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........

and now you have a ruby environment clean of any installed gems.

现在你有一个 ruby​​ 环境,没有任何已安装的 gem。

回答by Zack Morris

Another way from https://makandracards.com/jan0sch/9537-uninstall-all-ruby-gems-from-your-system(similar to rainkinz's answerand Ralph's comment). Here are some variations:

来自https://makandracards.com/jan0sch/9537-uninstall-all-ruby-gems-from-your-system 的另一种方式(类似于rainkinz 的回答Ralph 的评论)。以下是一些变化:

# 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

Breaking this down:

打破这个:

  • gem listlists all gems
  • cut -d" " -f1takes the first column
  • xargs -I % gem uninstall -aIx %calls gem uninstall -aIxwith each output line as an argument
  • gem list列出所有宝石
  • cut -d" " -f1取第一列
  • xargs -I % gem uninstall -aIx %调用gem uninstall -aIx与各输出线作为参数

Note that I specified the argument with -Ias %and passed it directly for security:

请注意,我使用-Ias指定了参数%并直接传递了它以确保安全:

xargs -I % gem uninstall -aIx %

instead of:

代替:

xargs gem uninstall -aIx

That's because xargshas a security issue where options like -ncan be passed to its command and cause unexpected results. This can be demonstrated with the following example:

那是因为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

回答by Perennialista

If your problem is similar to mine, then you want to uninstall all gems that were installed while testing GemFilechanges, in that case I simply used:

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

bundle clean

That uninstalled all the gems that are not specified in the GemFileand GemFile.lock.

这卸载了GemFile和中未指定的所有 gem GemFile.lock

I have not tested it but I suppose you could delete all lines from your Gemfile and run the above command to get rid of all the gems installed by the ROR project in the current directory.

我还没有测试过,但我想你可以从 Gemfile 中删除所有行并运行上面的命令来删除当前目录中 ROR 项目安装的所有 gem。

回答by Kirikami

Found solution for uninstalling all gems except of default:

找到卸载所有 gem 的解决方案,默认情况除外:

Crete delete_gems.rb with

克里特岛 delete_gems.rb 与

#!/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

Run this script:

运行这个脚本:

sudo chmod 1777 delete_gems.rb
./delete_gems.rb

All gems will be removed except of default. Here link on original solution http://zanshin.net/2013/06/10/how-to-delete-all-ruby-gems/

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