从 Ubuntu 中移除 Snap

参考How to Remove and Disable Snap | Baeldung on LinuxCompletely remove Snap from Ubuntu 24.04? : r/Ubuntu

在 Ubuntu 24.04 (Server & WSL) 上测试通过,社区报告 18.04, 20.04, 22.04 也可行。

步骤

检查是否有 Snap

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
$ snap --version
snap    2.67
snapd   2.67
series  16
ubuntu  24.04
kernel  6.8.0-52-generic

$ snap list
Name                 Version        Rev    Tracking       Publisher   Notes
canonical-livepatch  10.9.0         286    latest/stable  canonical✓  -
core20               20240911       2434   latest/stable  canonical✓  base
core22               20250110       1748   latest/stable  canonical✓  base
lxd                  5.0.4-497fe1e  31333  5.0/stable/…   canonical✓  -
snapd                2.67           23545  latest/stable  canonical✓  snapd

移除所有 Snap 包

根据snap list的输出,逐个移除所有 Snap 包。

1
2
3
4
5
sudo snap remove --purge canonical-livepatch
sudo snap remove --purge lxd
sudo snap remove --purge core20
sudo snap remove --purge core22
sudo snap remove --purge snapd

确保已移除所有包。

1
2
$ snap list
No snaps are installed yet. Try 'snap install hello-world'.

移除 snapd

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
$ sudo systemctl stop snapd
Stopping 'snapd.service', but its triggering units are still active:
snapd.socket

$ sudo systemctl disable snapd
Removed "/etc/systemd/system/multi-user.target.wants/snapd.service".
Disabling 'snapd.service', but its triggering units are still active:
snapd.socket

$ sudo systemctl mask snapd
Created symlink /etc/systemd/system/snapd.service → /dev/null.
Masking 'snapd.service', but its triggering units are still active:
snapd.socket

$ sudo apt purge snapd -y
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages will be REMOVED:
  snapd* ubuntu-server-minimal*
...
Discarding preserved snap namespaces
Final directory cleanup
Removing extra snap-confine apparmor rules
Removing snapd cache
Removing snapd state

$ sudo apt-mark hold snapd
snapd set on hold.

注意:Server 版本中,ubuntu-server-minimal可能会被自动移除。在 24.04 版本中,这没有影响。

移除 Snap 包目录

1
2
3
4
rm -rf ~/snap
sudo rm -rf /snap
sudo rm -rf /var/snap
sudo rm -rf /var/lib/snapd

阻止 snapd 通过依赖(Canonical 的 Snap 包装器)安装

1
2
3
4
5
sudo cat <<EOF | sudo tee /etc/apt/preferences.d/nosnap.pref
Package: snapd
Pin: release a=*
Pin-Priority: -10
EOF

效果如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
$ sudo apt install chromium-browser
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:
 chromium-browser : PreDepends: snapd but it is not installable
E: Unable to correct problems, you have held broken packages.

可选:使用假 snapd 包修复依赖关系

Internet Archive 上有一个社区贡献的假 snapd 包文件备份),可以用来修复依赖关系。

除非另有说明,此内容使用 CC BY-SA 4.0 许可。
提交: 6ddb243b   环境: production   Hugo: 0.142.0   主题: 3.30.0-modified   时间: 1740145765872504   语言: zh-cn
作者对于因使用或解读所提供内容而导致的任何直接或间接后果不承担任何责任。
使用 Hugo 构建
主题 StackJimmy 设计