我有一个 Homebrew 公式,我希望卸载/删除它及其所有依赖项,跳过其他包依赖的包(又名 级联包删除(用包管理器的说法)。
eg 卸载依赖于包b
& 的包a
c
,其中包 d
也依赖于包 c
。结果应该同时卸载 a
& b
,跳过c
。
我怎样才能做到这一点?
必须有一种方法可以卸载软件包而不留下不必要的垃圾。
I have a Homebrew formula that I wish to uninstall/remove along with all its dependencies, skipping packages whom other packages depend upon (a.k.a. Cascading package removal in Package manager parlance).
e.g. Uninstall package a
which depends on packages b
& c
, where package d
also depends on package c
. The result should uninstall both a
& b
, skipping c
.
How can I do that?
There must be a way to uninstall a package without leaving unnecessary junk behind.
发布评论
评论(14)
编辑:
看起来 现在使用名为
brew 的外部命令解决了问题rmdeps 或
brew rmtree
。
要安装和使用,请发出以下命令:
请参阅上面的链接以获取更多信息和讨论。
[编辑] 请参阅 https://stackoverflow.com/a/66719581/160968 中的新命令
brew autoremove
原始答案:
目前看来,还没有简单的方法来实现这一点。
不过,我在 Homebrew 的 GitHub 页面上提交了一个问题,有人提出了一个临时解决方案,直到他们同意添加一个独占命令来解决这个问题。
有一个名为
brew leaves
的外部命令,它可以打印所有内容不依赖于其他包的包。如果您对
brew leaves
和brew deps
的输出执行逻辑和,您可能会只需获取孤立依赖包的列表,您可以随后手动卸载它们。我想,将它与 xargs 结合起来,你就会得到你需要的东西(未经测试,不要指望这一点)。编辑:有人刚刚建议了一个非常相似的解决方案,使用
join
而不是xargs
:请参阅对上述问题发表评论以获取更多信息。
EDIT:
It looks like the issue is now solved using an external command called
brew rmdeps
orbrew rmtree
.To install and use, issue the following commands:
See the above link for more information and discussion.
[EDIT] see the new command
brew autoremove
in https://stackoverflow.com/a/66719581/160968Original 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.
There's an external command called
brew leaves
which prints all packages that are not dependencies of other packages.If you do a logical and on the output of
brew leaves
andbrew deps <package>
, you might just get a list of the orphaned dependency packages, which you can uninstall manually afterwards. Combine this withxargs
and you'll get what you need, I guess (untested, don't count on this).EDIT: Somebody just suggested a very similar solution, using
join
instead ofxargs
:See the comment on the issue mentioned above for more info.
到 2020 年底,Homebrew 团队添加了一个简单的命令
brew autoremove
来删除所有未使用的依赖项。首先,卸载软件包:
brew uninstall;
然后,删除所有未使用的依赖项:
brew autoremove
By the end of 2020, the Homebrew team added a simple command
brew autoremove
to remove all unused dependencies.First, uninstall the package:
brew uninstall <package>
Then, remove all the unused dependencies:
brew autoremove
brew rmtree
根本不起作用。从该问题的链接中,我发现 rmrec 确实有效。天知道为什么brew
没有将此作为本机命令。brew rmtree
doesn't work at all. From the links on that issue I found rmrec which actually does work. God knows whybrew
doesn't have this as a native command.这里的目标是删除给定的包及其依赖项而不破坏另一个包的依赖项。我使用这个命令:
注意:编辑以反映@alphadogg 的有用评论。
The goal here is to remove the given package and its dependencies without breaking another package's dependencies. I use this command:
Note: Edited to reflect @alphadogg's helpful comment.
基于@jfmercer 的回答(更正需要的不仅仅是评论)。
删除包的依赖项(不删除包):
删除包:
重新安装缺少的库:
在发现 meld “noreferrer">MeldMerge 版本。
Based on @jfmercer answer (corrections needed more than a comment).
Remove package's dependencies (does not remove package):
Remove package:
Reinstall missing libraries:
Tested uninstalling
meld
after discovering MeldMerge releases.使用此答案要求您创建并维护一个文件,其中包含要安装在系统上的软件包名称。如果您还没有,请使用以下命令并删除您不想保留安装的软件包名称。
然后,您可以通过运行以下命令来删除所有已安装但不需要的软件包和任何不必要的依赖项
:https://gist.github.com/cskeeters/10ff1295bca93808213d
该脚本获取您在中指定的所有包brew_packages 及其所有依赖项,并将它们与
brew list
的输出进行比较,最后在与用户验证此列表后删除不需要的包。此时,如果您想删除包
a
,只需将其从brew_packages文件中删除,然后重新运行brew_cleanbrew_packages
即可。它将删除b
,但不会删除c
。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.
Then you can remove all installed, but unwanted packages and any unnecessary dependencies by running the following command
brew_clean
is available here: https://gist.github.com/cskeeters/10ff1295bca93808213dThis script gets all of the packages you specified in brew_packages and all of their dependancies and compares them against the output of
brew list
and finally removes the unwanted packages after verifying this list with the user.At this point if you want to remove package
a
, you simply remove it from the brew_packages file then re-runbrew_clean brew_packages
. It will removeb
, but notc
.将以下脚本保存为 brew-purge
现在使用以下命令安装它
现在运行它
使用 gpg 的示例
Save the following script as brew-purge
Now install it with the following command
Now run it
Example using gpg
更完整的 Bourne Shell 函数
已经有很多好的答案,但有些已经过时,而且没有一个是完全完整的。特别是,它们中的大多数将删除依赖项,但之后仍由您删除最初目标的公式。如果您想一次卸载多个公式,那么使用发布的单行文字也可能很乏味。
这是一个与 Bourne 兼容的 shell 函数(没有任何已知的 Bashism),它获取公式列表,删除每个公式的依赖项,删除公式本身的所有副本,然后重新安装任何丢失的依赖项。
它在 Homebrew 1.7.4 上进行了测试。
注意事项
这适用于我测试过的所有标准公式。它目前不处理木桶,但如果您尝试解开与标准配方(例如 MacVim)同名的木桶,它也不会大声抱怨。
A More-Complete Bourne Shell Function
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.
It was tested on 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).
您可以使用 UNIX 管道来实现此目的
You can just use a UNIX pipe for this
其他答案对我不起作用,但这确实(在
fish
shell 中):因为当其他软件包依赖于
p
时,brew remove $p
失败代码>.Other answers didn't work for me, but this did (in
fish
shell):Because
brew remove $p
fails when some other package depends onp
.自 2024 年 5 月起,Homebrew 4.3 中默认启用之前可选的 HOMEBREW_AUTOREMOVE 标志,因此卸载软件包也会自动删除其依赖项。
请参阅:https://brew.sh/2024/05/14/homebrew -4.3.0/
As of May 2024 with Homebrew 4.3, the previously optional
HOMEBREW_AUTOREMOVE
flag is enabled by default, so uninstalling a package will also automatically remove its dependencies.See: https://brew.sh/2024/05/14/homebrew-4.3.0/
稍微精致一点;可以提供多种封装;未提供时可使用。
Slightly refined; can supply multiple packages; has usage when none supplied.
@jfmercer 的答案必须稍作修改才能适用于当前的brew,因为
brewmissing
的输出已更改:The answer of @jfmercer must be modified slightly to work with current brew, because the output of
brew missing
has changed:我英语不好,所以阅读时请考虑到这一点。
由于自制程序的深层依赖关系无法通过简单的脚本检索,因此我创建了一个分析工具。
(分析formula.jws.json、cask.jws.json、INSTALL_RECEIPT.json)
该工具还读取运行时依赖项,允许完全卸载,
它还检查 build_dependency 与 .检查 build_dependency 的选项
检查所有公式所依赖的公式。
公式(A).deps = 公式(B),公式(C),公式(D)
检查依赖于该公式的所有公式。
公式(A).uses = 公式(X),公式(Y),公式(Z)
保存到简单数据库。
该工具的核心在于创建中间文件的部分。
每次启动该工具时,它每小时都会在后台更新简单数据库。
从简单数据库中检索所有相关公式,
检查该公式是否依赖于它所依赖的公式以外的其他公式,
仅选择依赖于从属公式的公式。
不选择在树结构中多级依赖且较高的公式。
比较所有公式依赖关系并提取不需要的公式。
Linux 有特殊的依赖性并且需要时间。
Linux 的 efl 依赖项是一个超过 700 万行的树形结构。
这是该工具的功能之一,但是依赖项检查的源代码很长,所以我将粘贴一个链接。
命令
bl -dd <公式> # 试运行
bl -ddd <公式> # [y/n]
例如,如果您想删除 efl 依赖项但保留 ffmpeg 依赖项
(包含选项构建)
bl -ddd efl ffmpeg 或 bl -ddd efl 。 ffmpeg
附属制作人
https://github.com/konnano/brew_list
I'm not good at English, so please take this into account when reading.
Since homebrew's deep dependencies cannot be retrieved with a simple script, I created an analysis tool.
(Analyze formula.jws.json, cask.jws.json, INSTALL_RECEIPT.json)
The tool also reads runtime_dependencies, allowing for complete uninstallation,
It also checks for build_dependencies with the . option to check build_dependencies
Examine the formulas that all formulas depend on.
formula(A).deps = formula(B),formula(C),formula(D)
Examine all formulas that depend on that formula.
formula(A).uses = formula(X),formula(Y),formula(Z)
Save to simple database.
The heart of this tool lies in the part that creates intermediate files.
Every time you start the tool, it updates the simple database in the background every hour.
retrieve all dependent formulas from the simple database,
Check whether the formula is dependent on other formulas than the one on which it is dependent,
Select only formulas that are dependent on dependent formulas.
Formulas that are multi-level dependent in the tree structure and are higher up are not selected.
Compare all formula dependencies and extract unnecessary formulas.
Linux has special dependencies and takes time.
Linux's efl dependencies are a tree structure of over 7 million lines.
This is one of the features of the tool, but the source of the dependency check is long, so I will paste a link to it.
command
bl -dd <formula> # dry-run
bl -ddd <formula> # [y/n]
For example, if you want to remove the efl dependency but leave the ffmpeg dependency
( . Option build included )
bl -ddd efl ffmpeg or bl -ddd efl . ffmpeg
Affiliation Producer
https://github.com/konnano/brew_list