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

发布于 2024-12-03 05:39:51 字数 437 浏览 2 评论 0 原文

我有一个 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

一抹淡然 2024-12-10 05:39:52

编辑:

看起来 现在使用名为 brew 的外部命令解决了问题rmdeps 或 brew rmtree

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

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

请参阅上面的链接以获取更多信息和讨论。


[编辑] 请参阅 https://stackoverflow.com/a/66719581/160968 中的新命令 brew autoremove


原始答案:

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

不过,我在 Homebrew 的 GitHub 页面上提交了一个问题,有人提出了一个临时解决方案,直到他们同意添加一个独占命令来解决这个问题。

有一个名为 brew leaves外部命令,它可以打印所有内容不依赖于其他包的包。

如果您对 brew leavesbrew deps 的输出执行逻辑,您可能会只需获取孤立依赖包的列表,您可以随后手动卸载它们。我想,将它与 xargs 结合起来,你就会得到你需要的东西(未经测试,不要指望这一点)。


编辑:有人刚刚建议了一个非常相似的解决方案,使用join而不是xargs

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

请参阅对上述问题发表评论以获取更多信息。

EDIT:

It looks like the issue is now solved using an external command called brew rmdeps or 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.


[EDIT] see the new command brew autoremove in https://stackoverflow.com/a/66719581/160968


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.

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 and brew deps <package>, you might just get a list of the orphaned dependency packages, which you can uninstall manually afterwards. Combine this with xargs 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 of xargs:

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

See the comment on the issue mentioned above for more info.

一曲琵琶半遮面シ 2024-12-10 05:39:52

到 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

巴黎夜雨 2024-12-10 05:39:52

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

brew tap ggpeti/rmrec
brew rmrec pkgname

brew rmtree doesn't work at all. From the links on that issue I found rmrec which actually does work. God knows why brew doesn't have this as a native command.

brew tap ggpeti/rmrec
brew rmrec pkgname
伤感在游骋 2024-12-10 05:39:52

这里的目标是删除给定的包及其依赖项而不破坏另一个包的依赖项。我使用这个命令:

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

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

The goal here is to remove the given package and its dependencies without breaking 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.

酒废 2024-12-10 05:39:52

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

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

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

删除包:

brew remove [FORMULA]

重新安装缺少的库:

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

在发现 meld “noreferrer">MeldMerge 版本

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

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 meld after discovering MeldMerge releases.

情绪少女 2024-12-10 05:39:52

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

brew leaves > brew_packages

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

brew_clean brew_packages

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.

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_clean is available here: 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 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-run brew_clean brew_packages. It will remove b, but not c.

心舞飞扬 2024-12-10 05:39:52

将以下脚本保存为 brew-purge

#!/bin/bash
#:Usage: brew purge formula
#: 
#:Removes the package and all dependancies.
#: 
#: 
PKG="$1"
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

现在使用以下命令安装它

sudo install brew-purge /usr/local/bin

现在运行它

brew purge package

使用 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.
$ 

Save the following script as brew-purge

#!/bin/bash
#:Usage: brew purge formula
#: 
#:Removes the package and all dependancies.
#: 
#: 
PKG="$1"
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

$ 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.
$ 
绿阴红影里的.如风往事 2024-12-10 05:39:52

更完整的 Bourne Shell 函数

已经有很多好的答案,但有些已经过时,而且没有一个是完全完整的。特别是,它们中的大多数将删除依赖项,但之后仍由您删除最初目标的公式。如果您想一次卸载多个公式,那么使用发布的单行文字也可能很乏味。

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

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
}

它在 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.

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.

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).

鹿童谣 2024-12-10 05:39:52

您可以使用 UNIX 管道来实现此目的

brew deps [FORMULA] | xargs brew rm

You can just use a UNIX pipe for this

brew deps [FORMULA] | xargs brew rm
-柠檬树下少年和吉他 2024-12-10 05:39:52

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

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

因为当其他软件包依赖于 p时, brew remove $p 失败代码>.

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

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

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

太阳男子 2024-12-10 05:39:52

自 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/

木落 2024-12-10 05:39:52

稍微精致一点;可以提供多种封装;未提供时可使用。

#!/bin/bash
# Removes the package and all dependancies.

if [ $# -eq 0 ]; then
   echo "$(basename $0) <pkg> [<pkg> [...]]"
   exit 1
fi

function tree() {
    pkg="$1"
    join <(brew leaves) <(sort <(brew deps ${pkg}; echo ${pkg}))
} 

let e=0
for pkg in "$@"; do
    printf "Purging %s and its dependencies...\n" "${pkg}"
    deps=( $(tree ${pkg}) )
    while (( ${#deps[@]} > 0 )); do
        brew rm "${deps[@]}"
        deps=( $(tree ${pkg}) )
    done
done

Slightly refined; can supply multiple packages; has usage when none supplied.

#!/bin/bash
# Removes the package and all dependancies.

if [ $# -eq 0 ]; then
   echo "$(basename $0) <pkg> [<pkg> [...]]"
   exit 1
fi

function tree() {
    pkg="$1"
    join <(brew leaves) <(sort <(brew deps ${pkg}; echo ${pkg}))
} 

let e=0
for pkg in "$@"; do
    printf "Purging %s and its dependencies...\n" "${pkg}"
    deps=( $(tree ${pkg}) )
    while (( ${#deps[@]} > 0 )); do
        brew rm "${deps[@]}"
        deps=( $(tree ${pkg}) )
    done
done
清风无影 2024-12-10 05:39:52

@jfmercer 的答案必须稍作修改才能适用于当前的brew,因为 brewmissing 的输出已更改:

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

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

brew deps [FORMULA] | xargs brew remove --ignore-dependencies && brew missing | cut -f1 -d: | xargs brew install
ι不睡觉的鱼゛ 2024-12-10 05:39:52

我英语不好,所以阅读时请考虑到这一点。
由于自制程序的深层依赖关系无法通过简单的脚本检索,因此我创建了一个分析工具。
(分析formula.jws.json、cask.jws.json、INSTALL_RECEIPT.json)
该工具还读取运行时依赖项,允许完全卸载,
它还检查 build_dependency 与 .检查 build_dependency 的选项

检查所有公式所依赖的公式。

           |-formula(B)
           |
formula(A)-|-formela(C)
           |
           |-formula(D)

公式(A).deps = 公式(B),公式(C),公式(D)

检查依赖于该公式的所有公式。

formula(X)-|
           |
formula(Y)-|-formula(A)
           |
formula(z)-|

公式(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(B)
           |
formula(A)-|-formela(C)
           |
           |-formula(D)

formula(A).deps = formula(B),formula(C),formula(D)

Examine all formulas that depend on that formula.

formula(X)-|
           |
formula(Y)-|-formula(A)
           |
formula(z)-|

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文