如何阻止apt安装snap软件包?

sudo apt-get install chromium-browser出于某种原因安装的不是适当的apt-get软件包,而是安装了chromium snap。

我知道阻止这样的安装并不会奇迹般地建立一个维护良好的软件包,并且会导致安装失败。但我更喜欢在安装时出现错误并手动安装 - 从源代码或者在极少数情况下从snap安装。

我希望永远不要使用snap安装任何东西(起初是因为Snap Store的闭源做法)。尤其是在我使用apt-get安装时不要悄悄地安装。

如何在gnome-software centre中禁用snap?不能回答我的问题,因为我想摆脱apt中的snap混乱,而不是gnome-software-center。


1看这个:https://askubuntu.com/q/1317194/1157519 - Levente
4@Levente 谢谢!不幸的是,这并没有帮助 —— 如果安装需要使用 apt-get 安装 snap,我希望安装失败。 - reducing activity
3Chromium浏览器在许多版本中都不作为deb软件包提供(早在20.04之前就不再提供了;18.04还有一个deb软件包,但在2018年底首次出现了snap软件包-请参见https://discourse.ubuntu.com/t/please-test-chromium-snap-as-a-replacement-for-the-deb/7978 https://discourse.ubuntu.com/t/intent-to-provide-chromium-as-a-snap-only/5987;2018年5月)。它仅以*deb*的形式提供,以便用户不会收到找不到软件包的错误提示(它只是为他们加载*snap*软件包)。我发现`discover`很容易选择*snap*和*deb*软件包。 - guiverc
5@guiverc 我对无法使用apt安装Chromium感到满意。事实上,如果需要用snap安装,我希望无法这样做。 - reducing activity
这个回答解决了你的问题吗?如何在gnome软件中心禁用snaps? - guiverc
我不认为你所要求的是明智的……这样做会使你的系统无法进行更新……而更新通常是通过 deb 进行的,如果从技术上来说比让用户运行命令更容易的话(并且通过 snap 是不可能的)。 - guiverc
1@guiverc 我对此没有意见。如果我的系统无法在没有snap的情况下工作,我可以考虑切换到其他发行版。 - reducing activity
Lubuntu在禁用snaps的情况下运行良好(对于只有1GB内存的设备来说,通常是明智的做法;尽管我倾向于仅禁用,因为有时启用它会很有用.. 尤其是在升级时)。我不建议,但如果你看一下Linux Mint是Ubuntu的下游,并且最近才禁用/修改它以使其符合你的要求,但问题很快出现,部分功能不得不被撤销... 我认为这样做不明智,所以无法提供帮助(但禁用snaps更容易,也是我推荐的做法)。 - guiverc
Debian:没有Chromium Snap。请访问https://packages.debian.org/sid/chromium了解更多信息。例如,Ubuntu 20.04的下载链接为https://drive.google.com/drive/folders/1HsuZQNyKNq9L1XcE1clacFLwiRK9gDS3?usp=sharing。 - Knud Larsen
3这就是为什么我还在使用18.04LTS版本,而且当它到达终止支持时,我将离开Ubuntu的世界。 - RonJohn
2个回答

你必须从系统中移除snapd。
sudo apt-get autopurge snapd

然后为APT创建特殊的配置文件,就像LinuxMint所做的一样:

cat <<EOF | sudo tee /etc/apt/preferences.d/nosnap.pref
# To prevent repository packages from triggering the installation of Snap,
# this file forbids snapd from being installed by APT.
# For more information: https://linuxmint-user-guide.readthedocs.io/en/latest/snap.html

Package: snapd
Pin: release a=*
Pin-Priority: -10
EOF

这将阻止未来的Snaps安装。

1立即执行自动清理是否安全?许多教程都涉及先禁用快照,删除各个快照等操作。也许是的,但这可能会留下未使用的文件和文件夹。 - vanadium
1配置脚本将为我们完成所需的工作。 - N0rbert
1@N0rbert 所以通过这样做,我如何通过apt安装Firefox?这取决于snapd。 - mslot
2如果你还想要使用Firefox,首先卸载Snap,然后添加上面的文件,接着从以下几个不同的地方之一获取Firefox,这些地方在这里列出:https://askubuntu.com/questions/1369159/how-to-remove-snap-completely-without-losing-firefox - jeffmcneill
我找不到关于“autopurge”命令存在的证据。 - odigity

除了在/etc/apt中设置文件外,您还可以在卸载snapd后运行apt-mark来防止安装需要snapd的任何内容:
$ sudo apt-mark hold snapd

例如,在Ubuntu 22.04中安装firefox(这是一个基于snapd的软件包)会出现错误:

$ sudo apt install firefox
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 firefox : PreDepends: snapd but it is not going to be installed
E: Unable to correct problems, you have held broken packages.

唯一打破这个规则的方法是明确地去做。
$ sudo apt install snapd
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
  snapd
The following held packages will be changed:
  snapd
0 upgraded, 1 newly installed, 0 to remove and 11 not upgraded.
Need to get 23.8 MB of archives.
After this operation, 102 MB of additional disk space will be used.
Do you want to continue? [Y/n]

此外,您可以通过以下方式撤销效果:
$ sudo apt-mark unhold snapd