macos 卸载/删除 Homebrew 包,包括其所有依赖项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7323261/
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-10-21 08:23:47  来源:igfitidea点击:

Uninstall / remove a Homebrew package including all its dependencies

macoshomebrew

提问by Ory Band

I have a Homebrewformula that I wish to uninstall/remove along with all its dependencies, skipping packages whom other packages depend upon (a.k.a. Cascading package removalin Package manager parlance).

我有一个Homebrew公式,我希望卸载/删除它的所有依赖项,跳过其他包所依赖的包(也就是包管理器术语中的级联包删除)。

e.g.Uninstall package awhich depends on packages b& c, where package dalso depends on package c. The result should uninstall both a& b, skipping c.

例如卸载a依赖于包b& 的包c,其中包d也依赖于包c。结果应该同时卸载a& b,跳过c

How can I do that?

我怎样才能做到这一点?

There must be a way to uninstall a package without leaving unnecessary junkbehind.

必须有一种方法可以卸载软件包而不会留下不必要的垃圾

回答by Ory Band

EDIT:

编辑:

It looks like the issue is now solved using an external command called brew rmdepsor brew rmtree.

看起来这个问题现在已经使用名为brew rmdepsor的外部命令解决了brew rmtree

To install and use, issue the following commands:

要安装和使用,请发出以下命令:

$ brew tap beeftornado/rmtree
$ brew rmtree <package>

See the above link for more information and discussion.

有关更多信息和讨论,请参阅上面的链接。



Original answer:

原答案:

It appears that currently, there's no easy way to accomplish this.

目前看来,没有简单的方法可以实现这一点。

However, I filed an issue on Homebrew's GitHub page, and somebody suggested a temporary solution until they add an exclusive command to solve this.

但是,我在 Homebrew 的 GitHub 页面上提出了一个问题,有人提出了一个临时解决方案,直到他们添加了一个专有命令来解决这个问题。

There's an external commandcalled brew leaveswhich prints all packages that are not dependencies of other packages.

有一个外部命令调用brew leaves它打印所有不依赖于其他包的包。

If you do a logical andon the output of brew leavesand brew deps <package>, you might just get a list of the orphaned dependency packages, which you can uninstall manually afterwards. Combine this with xargsand you'll get what you need, I guess (untested, don't count on this).

如果你的逻辑,并在输出brew leavesbrew deps <package>,你可能只是得到孤立依赖的软件包,您可以手动卸载后的名单。结合它,xargs你会得到你需要的东西,我猜(未经测试,不要指望这个)。



EDIT:Somebody just suggested a very similar solution, using joininstead of xargs:

编辑:有人刚刚提出了一个非常相似的解决方案,使用join代替xargs

brew rm FORMULA
brew rm $(join <(brew leaves) <(brew deps FORMULA))


See the comment on the issue mentioned abovefor more info.

有关更多信息,请参阅对上述问题评论

回答by jfmercer

The goal here is to remove the given package and its dependencies withoutbreaking another package's dependencies. I use this command:

这里的目标是在破坏另一个包的依赖关系的情况下删除给定的包及其依赖项。我使用这个命令:

brew deps [FORMULA] | xargs brew remove --ignore-dependencies && brew missing | xargs brew install

Note: Edited to reflect @alphadogg's helpful comment.

注意:编辑以反映@alphadogg 的有用评论。

回答by Timmmm

brew rmtreedoesn't work at all. From the links on that issue I found rmrecwhich actually does work. God knows why brewdoesn't have this as a native command.

brew rmtree根本不起作用。从有关该问题的链接中,我发现rmrec确实有效。天知道为什么brew没有将此作为本地命令。

brew tap ggpeti/rmrec
brew rmrec pkgname

回答by vault

Based on @jfmercer answer (corrections needed more than a comment).

基于@jfmercer 的回答(更正需要的不仅仅是评论)。

Remove package's dependencies (does not remove package):

删除包的依赖项(不删除包):

brew deps [FORMULA] | xargs brew remove --ignore-dependencies

Remove package:

删除包:

brew remove [FORMULA]

Reinstall missing libraries:

重新安装缺少的库:

brew missing | cut -d: -f2 | sort | uniq | xargs brew install

Tested uninstalling meldafter discovering MeldMerge releases.

meld在发现MeldMerge 版本后测试卸载。

回答by Chad Skeeters

Using this answer requires that you create and maintain a file that contains the package names you want installed on your system. If you don't have one already, use the following command and delete the package names what you don't want to keep installed.

使用此答案要求您创建并维护一个文件,其中包含要安装在系统上的软件包名称。如果您还没有,请使用以下命令并删除您不想保留安装的软件包名称。

brew leaves > brew_packages

Then you can remove all installed, but unwanted packages and any unnecessary dependencies by running the following command

然后您可以通过运行以下命令删除所有已安装但不需要的包和任何不必要的依赖项

brew_clean brew_packages

brew_cleanis available here: https://gist.github.com/cskeeters/10ff1295bca93808213d

brew_clean可在此处获得:https: //gist.github.com/cskeeters/10ff1295bca93808213d

This script gets all of the packages you specified in brew_packages and all of their dependancies and compares them against the output of brew listand finally removes the unwanted packages after verifying this list with the user.

此脚本获取您在 brew_packages 中指定的所有包及其所有依赖项,并将它们与输出进行比较,brew list并在与用户验证此列表后最终删除不需要的包。

At this point if you want to remove package a, you simply remove it from the brew_packages file then re-run brew_clean brew_packages. It will remove b, but not c.

此时,如果您想删除 package a,只需将其从 brew_packages 文件中删除,然后重新运行brew_clean brew_packages。它会删除b,但不会c

回答by shapeshed

You can just use a UNIX pipe for this

您可以为此使用 UNIX 管道

brew deps [FORMULA] | xargs brew rm

回答by Kevin Davies

Save the following script as brew-purge

将以下脚本另存为brew-purge

#!/bin/bash
#:Usage: brew purge formula
#: 
#:Removes the package and all dependancies.
#: 
#: 
PKG=""
if [ -z "$PKG" ];then
   brew purge --help
   exit 1
fi
brew rm $PKG
[ $? -ne 0 ] && exit 1
while brew rm $(join <(brew leaves) <(brew deps $PKG)) 2>/dev/null
  do :
done
echo Package $PKG and its dependancies have been removed.
exit 0

Now install it with the following command

现在使用以下命令安装它

sudo install brew-purge /usr/local/bin

Now run it

现在运行它

brew purge package

Example using gpg

使用 gpg 的示例

$ brew purge gpg
Uninstalling /usr/local/Cellar/gnupg/2.2.13... (134 files, 11.0MB)
Uninstalling /usr/local/Cellar/adns/1.5.1... (14 files, 597.5KB)
Uninstalling /usr/local/Cellar/gnutls/3.6.6... (1,200 files, 8.9MB)
Uninstalling /usr/local/Cellar/libgcrypt/1.8.4... (21 files, 2.6MB)
Uninstalling /usr/local/Cellar/libksba/1.3.5... (14 files, 344.2KB)
Uninstalling /usr/local/Cellar/libusb/1.0.22... (29 files, 508KB)
Uninstalling /usr/local/Cellar/npth/1.6... (11 files, 71.7KB)
Uninstalling /usr/local/Cellar/pinentry/1.1.0_1... (12 files, 263.9KB)
Uninstalling /usr/local/Cellar/libassuan/2.5.3... (16 files, 444.2KB)
Uninstalling /usr/local/Cellar/libtasn1/4.13... (59 files, 436KB)
Uninstalling /usr/local/Cellar/libunistring/0.9.10... (54 files, 4.4MB)
Uninstalling /usr/local/Cellar/nettle/3.4.1... (85 files, 2MB)
Uninstalling /usr/local/Cellar/p11-kit/0.23.15... (63 files, 2.9MB)
Uninstalling /usr/local/Cellar/gmp/6.1.2_2... (18 files, 3.1MB)
Uninstalling /usr/local/Cellar/libffi/3.2.1... (16 files, 296.8KB)
Uninstalling /usr/local/Cellar/libgpg-error/1.35... (27 files, 854.8KB)
Package gpg and its dependancies have been removed.
$ 

回答by Todd A. Jacobs

A More-Complete Bourne Shell Function

更完整的 Bourne Shell 函数

There are a number of good answers already, but some are out of date and none of them are entirely complete. In particular, most of them will remove dependencies but still leave it up to you to remove the originally-targeted formula afterwards. The posted one-liners can also be tedious to work with if you want to uninstall more than one formula at a time.

已经有许多好的答案,但有些已经过时,而且没有一个是完整的。特别是,它们中的大多数会删除依赖项,但仍然由您决定之后删除最初针对的公式。如果您想一次卸载多个公式,发布的单行代码也可能很乏味。

Here is a Bourne-compatible shell function (without any known Bashisms) that takes a list of formulae, removes each one's dependencies, removes all copies of the formula itself, and then reinstalls any missing dependencies.

这是一个与 Bourne 兼容的 shell 函数(没有任何已知的 Bashisms),它接受一个公式列表,删除每个人的依赖项,删除公式本身的所有副本,然后重新安装任何丢失的依赖项。

unbrew () {
    local formula
    for formula in "$@"; do
        brew deps "$formula" |
        xargs brew uninstall --ignore-dependencies --force
        brew uninstall --force "$formula"
    done
    brew missing | cut -f2 -d: | sort -u | xargs brew install
}

It was tested on Homebrew 1.7.4.

它在 Homebrew 1.7.4 上进行了测试。

Caveats

注意事项

This works on all standard formulae that I tested. It does not presently handle casks, but neither will it complain loudly if you attempt to unbrew a cask with the same name as a standard formula (e.g. MacVim).

这适用于我测试过的所有标准公式。它目前不处理木桶,但如果您尝试使用与标准公式(例如 MacVim)相同的名称来解泡木桶,它也不会大声抱怨。

回答by Luke Miles

Other answers didn't work for me, but this did (in fishshell):

其他答案对我不起作用,但这确实(在fishshell 中):

brew remove <package>
for p in (brew deps <package>)
    brew remove $p
end

Because brew remove $pfails when some other package depends on p.

因为brew remove $p当其他一些包依赖于p.

回答by freytag

The answer of @jfmercer must be modified slightly to work with current brew, because the output of brew missinghas changed:

@jfmercer 的答案必须稍作修改才能与当前的 brew 一起使用,因为输出brew missing已更改:

brew deps [FORMULA] | xargs brew remove --ignore-dependencies && brew missing | cut -f1 -d: | xargs brew install