Ubuntu 22.04 禁用(彻底移除)Snap

Posted by sysin on 2022-09-16
Estimated Reading Time 5 Minutes
Words 1.1k In Total
更新日期:Fri Sep 16 2022 09:08:13 GMT+0800,阅读量:

请访问原文链接:Ubuntu 22.04 禁用(彻底移除)Snap,查看最新版。原创作品,转载请保留出处。

作者主页:sysin.org


无耻抄袭者 Yu Tao,请立遁!!!

Snaps 是 Ubuntu 的母公司 Canonical 于 2016 年 4 月发布 Ubuntu 16.04 LTS(Long Term Support,长期支持版)时引入的一种容器化的软件包格式。自 Ubuntu 16.04 LTS 起,Ubuntu 操作系统可以同时支持 Snap 及 Debian 这两种格式的安装包。

Snap 虽然有一定的优点(请自行搜索),但是不足之处更多。Snap 软件包体积庞大,snapd 进程会导致系统重启等待,并且可能导致卡顿,禁用为佳。

特别是服务器版用不上这种软件包,默认是安装的,必须彻底删除。

Ubuntu 22.04 桌面版中两个关键的程序:软件商店和 Firefox 使用了 Snap 软件包,卡顿明显。

禁用服务无效示例:

1
2
3
4
5
6
#sudo systemctl disable --now snapd
#sudo systemctl disable --now snapd.socket
#sudo systemctl disable snapd
但是重启后,snapd 仍然运行
# 验证状态
systemctl status snapd

ubuntu-2204-desktop
图:Ubuntu 22.04 中的 Firefox(deb)

现在 Ubuntu OVF 中已经更新 apt_preferences 彻底排除了 Snap。

删除 Snap 的方法

已经确认 snapd 是无法禁用的,只能强制删除。以下操作无需停止 snapd 服务。

(1)删掉所有的已经安装的 Snap 软件。

snap list 用于查看已经安装的 Snap 软件,通过脚本全部删除:

1
2
3
for p in $(snap list | awk '{print $1}'); do
sudo snap remove $p
done

一般需要执行两次(桌面版三次),提示如下则正确执行:

1
2
3
snap "Name" is not installed
core20 removed
snapd removed

再次执行,提示如下,表明已经删除干净:

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

(2)删除 Snap 的 Core 文件。

1
2
3
4
5
6
sudo systemctl stop snapd
sudo systemctl disable --now snapd.socket

for m in /snap/core/*; do
sudo umount $m
done

(3)删除 Snap 的管理工具。

1
sudo apt autoremove --purge snapd

(4)删除 Snap 的目录。

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

(5)配置 APT 参数:禁止 apt 安装 snapd。

正确配置(参看 apt_preferences):

1
2
3
4
5
sudo sh -c "cat > /etc/apt/preferences.d/no-snapd.pref" << EOL
Package: snapd
Pin: release a=*
Pin-Priority: -10
EOL
1
2
3
4
5
a -> Archive
c -> Component
o -> Origin
l -> Label
n -> Architecture

验证效果:可以查询到该包,但是无法安装。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# sysin @ u22 in ~ [13:59:22]
$ apt list snapd
Listing... Done
snapd/jammy-updates 2.56.2+22.04ubuntu1 amd64
N: There is 1 additional version. Please use the '-a' switch to see it

# sysin @ u22 in ~ [13:59:37]
$ sudo apt install snapd
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Package snapd is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

E: Package 'snapd' has no installation candidate

同时禁用 snap Firefox 的更新(Server 版也可以配置):

1
2
3
4
5
sudo sh -c "cat > /etc/apt/preferences.d/no-firefox.pref" << EOL
Package: firefox
Pin: release a=*
Pin-Priority: -10
EOL

现在服务器版安装桌面环境也没有 Snap!

1
2
3
4
sudo apt update
#sudo apt install ubuntu-desktop #完整 GNOME 桌面环境包括 LibreOffice、视频播放器、游戏等等
sudo apt install ubuntu-desktop-minimal #仅浏览器和基本工具
sudo reboot

该方法也适用于 Ubuntu 20.04,只是 20.04 中 Firefox 不使用 Snap 软件包,不用额外屏蔽,也不用额外安装 deb 格式替代。

安装软件商店和 Firefox

上述操作后,可以安装 deb 格式的软件商店和 Firefox。

安装 Gnome 软件商店

1
2
3
4
sudo apt install gnome-software
# 确认没有 snapd 在按 y
#sudo apt install --install-suggests gnome-software
# 网传使用 `--install-suggests` 参数防止安装上 Snap 版本的软件包管理器,本例无效。

安装 DEB 格式的 Firefox

添加 Firefox 官方 PPA(Personal Package Archives)仓库:

1
sudo add-apt-repository ppa:mozillateam/ppa

将 Firefox 官方 PPA 仓库中的 firefox 设为高优先级:

1
2
3
4
5
sudo sh -c "cat > /etc/apt/preferences.d/mozillateam-firefox.pref" << EOL
Package: firefox*
Pin: release o=LP-PPA-mozillateam
Pin-Priority: 501
EOL

现在可以安装最新版的 deb 版 Firefox:

1
2
3
4
5
sudo apt update
apt list firefox #现在已经不是 Snap firefox
sudo apt install firefox
# 或者 Firefox ESR
sudo apt install firefox-esr

附录:恢复 Snap 的方法

只需要删除配置文件,即可重新使用 Snap 软件包。

1
2
3
4
5
6
sudo rm /etc/apt/preferences.d/no-snap.pref
sudo rm /etc/apt/preferences.d/no-firefox.pref
sudo rm /etc/apt/preferences.d/mozillateam-firefox.pref
sudo apt update
sudo snap install snap-store
sudo apt install firefox

捐助本站 ❤️ Donate

点击访问官方网站


文章用于推荐和分享优秀的软件产品及其相关技术,所有软件默认提供官方原版(免费版或试用版),免费分享。对于部分产品笔者加入了自己的理解和分析,方便学习和测试使用。任何内容若侵犯了您的版权,请联系作者删除。如果您喜欢这篇文章或者觉得它对您有所帮助,或者发现有不当之处,欢迎您发表评论,也欢迎您分享这个网站,或者赞赏一下作者,谢谢!

支付宝赞赏 微信赞赏

赞赏一下


☑️ 评论恢复,欢迎留言❗️
敬请注册!点击 “登录” - “用户注册”(已知不支持 21.cn/189.cn 邮箱)。请勿使用联合登录(已关闭)