Python Mercenaries
in
t
g
p
b
f

Removing Snap from the Ubuntu OS

written by Alan Cugler on 2024-04-09

Down sides to Snapd?

Snapd system provides a way to offer applications as snap packages. "A snap is an application containerised with all its dependencies." this should make installation of applications easier. However, it has several drawback to date.

  1. Snap applications are restricted to their own sandbox filesystem
  2. Configuration Automation has been severely hampered
  3. Application start up time is slower than other types of installed packages such as .deb
  4. Application stability for large applications has shown to be significantly affected
  5. Performance metrics are negatively impacted to notable by end users

Removing snapd

Snapd can be fully removed from linux systems to date, and in critical production infrastructure I personally recommend removing for better performance and stability.

TLDR removal command list

$ snap list
$ cat <<EOF | sudo tee nuke_snapd.sh
#!/bin/bash
# Nuke snap

echo Starting ...

progress_index=0

until
    snap list 2>&1 | grep hello-world
do
    for snap_app in $(snap list | grep -v 'Name' | awk '{print $1}')
    do
        echo Trying to remove ${snap_app}
        snap remove --purge ${snap_app}
        (( progress_index++ ))
        echo Progress index: ${progress_index}
    done
done
EOF

$ chmod +x nuke_snapd.sh
$ ./nuke_snapd.sh
$ systemctl stop snapd
$ systemctl disable snapd
$ systemctl mask snapd
$ apt purge snapd -y
$ rm -rf /root/snap/

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

$ rm -rf /snap
$ rm -rf /var/snap
$ rm -rf /var/lib/snapd

Walkthrough removal of snapd

To remove the snapd application you must have all the snapd applications removed first.

# snap list
Name              Version        Rev    Tracking         Publisher   Notes
amazon-ssm-agent  3.2.2303.0     7983   latest/stable/…  aws✓        classic
core18            20231027       2812   latest/stable    canonical✓  base
core20            20240227       2264   latest/stable    canonical✓  base
lxd               5.0.3-babaaf8  27948  5.0/stable/…     canonical✓  -
snapd             2.61.2         21184  latest/stable    canonical✓  snapd

We will use a brute force script to recursively attempt to remove listed snpad packages until all of them are gone.

# nano nuke_snap.sh
#!/bin/bash
# Nuke snap

echo Starting ...

progress_index=0

until
    snap list 2>&1 | grep hello-world
do
    for snap_app in $(snap list | grep -v 'Name' | awk '{print $1}')
    do
        echo Trying to remove ${snap_app}
        snap remove --purge ${snap_app}
        (( progress_index++ ))
        echo Progress index: ${progress_index}
    done
done
# chmod +x nuke_snap.sh 
# ./nuke_snap.sh 
Starting ...
Trying to remove amazon-ssm-agent
2024-04-09T15:24:02Z INFO Waiting for "snap.amazon-ssm-agent.amazon-ssm-agent.service" to stop.
amazon-ssm-agent removed
Progress index: 1
Trying to remove core18
core18 removed
Progress index: 2
Trying to remove core20
error: cannot remove "core20": snap "core20" is not removable: snap is being used by snap lxd.
Progress index: 3
Trying to remove lxd
lxd removed
Progress index: 4
Trying to remove snapd
error: cannot remove "snapd": snap "snapd" is not removable: remove all other snaps first
Progress index: 5
Trying to remove core20
core20 removed
Progress index: 6
Trying to remove snapd
snapd removed
Progress index: 7
No snaps are installed yet. Try 'snap install hello-world'.

Having fully removed all the applications we can remove the snapd service and purge the application from apt package manager.

# systemctl stop snapd
systemctl disable snapd
systemctl mask snapd
apt purge snapd -y
rm -rf ~/snap/
rm -rf /root/snap/
Warning: Stopping snapd.service, but it can still be activated by:
  snapd.socket
Removed /etc/systemd/system/multi-user.target.wants/snapd.service.
Created symlink /etc/systemd/system/snapd.service  /dev/null.
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following package was automatically installed and is no longer required:
  squashfs-tools
Use 'apt autoremove' to remove it.
The following packages will be REMOVED:
  snapd*
0 upgraded, 0 newly installed, 1 to remove and 59 not upgraded.
After this operation, 102 MB disk space will be freed.
(Reading database ... 132875 files and directories currently installed.)
Removing snapd (2.58+22.04.1) ...
Processing triggers for dbus (1.12.20-2ubuntu4.1) ...
Processing triggers for man-db (2.10.2-1) ...
(Reading database ... 132778 files and directories currently installed.)
Purging configuration files for snapd (2.58+22.04.1) ...
rmdir: failed to remove '/etc/systemd/system/snapd.mounts.target.wants': No such file or directory
Discarding preserved snap namespaces
Final directory cleanup
Removing extra snap-confine apparmor rules
Removing snapd cache
Removing snapd state


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

At this point we will not need to worry about snap again and all thats left is cleaning up the directories left behind.

# snap
Command 'snap' not found, but can be installed with:
apt install snapd
root@ip-172-31-25-48:~# 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

Error: Package 'snapd' has no installation candidate

$ rm -rf ~/snap
$ sudo rm -rf /snap
$ sudo rm -rf /var/snap
$ sudo rm -rf /var/lib/snapd

Note

This demonstration was done on an AWS VM. you may have noticed the amazon-ssm-agent package was listed from snap. This means that snap is actually a requirement for AWS VMs as this is a key application used to enable information and configuration with the aws native services that compliment the ec2 instances.

Closing thoughts

Ultimately Canonical is forcing the market to adopt its snapd applications, but this package management solution has almost entirely hurt the linux market in my opinion. The major upside is its supposed to make it easier to publish new applications and therefore might lower the barrier to entry for developers looking to provide a new applications. While adoption is a market force so is stability, performance, disk footprint, configuration management, automation, end user experience.

While Canonical may have some what consolidated applications to be dependent on their Snapd package framework, I think ultimately it will be a bitter pill swallowed like Jenkins is in CICD, and like Jenkins people will actively seek viable alternatives.


« Previous | Removing Snap from the Ubuntu OS