Spread the love

About the snap package manager

Snap is a software packaging and deployment system developed by Canonical for operating systems that use the Linux kernel. The packages, called snaps, and the tool for using them, snapd, work across a range of Linux distributions and allow upstream software developers to distribute their applications directly to users. Snaps are self-contained applications running in a sandbox with mediated access to the host system. Snap was originally released for cloud applications but was later ported to work for Internet of Things devices and desktop applications too.

Snap packages are not everyone’s favorite but they are an integral part of the Ubuntu ecosystem.

It has its pros and cons. One of the negatives is that Snap packages are usually bigger in size and take a lot of disk space.

This could be a problem if you are running out of disk space, specially on the root partition.

Let me share a neat trick that you could use to cut down the disk spaced used by Snap packages.

Cleaning up old Snap package versions to free disk space

The system files related to snap are stored in the /var/lib/snapd directory. Based on the number of Snap packages you have installed, this directory size could be in several GBs.

$ sudo du -sh /var/lib/snapd
5.4G	/var/lib/snapd
Code language: JavaScript (javascript)

That’s a lot, right? You could free up some disk space here.

By design, Snap keeps at least one older version of the packages you have installed on your system.

You can see this behavior by using the Snap command:

$ snap list --all
Code language: PHP (php)

You should see the same package listed twice with different version and revision number.

To free up disk space, you can delete the additional package versions. How do you know which one to delete? You can see that these older packages are labeled ‘disabled’.

Don’t worry. You don’t have to manually do it. There is sort of automatic way to do it thanks to a nifty bash script written by Alan Pope while he was working in the Snapcraft team.

I hope you know how to create and run a bash shell script. Basically, create a new file named clean-snap.sh and add the following lines to it.

#!/bin/bash
# Removes old revisions of snaps
# CLOSE ALL SNAPS BEFORE RUNNING THIS
set -eu
snap list --all | awk '/disabled/{print $1, $3}' |
    while read snapname revision; do
        snap remove "$snapname" --revision="$revision"
    done
Code language: PHP (php)

Save it and close the editor.

To run this script, keep it in your home directory and then open the terminal in Ubuntu and run this command:

sudo bash clean-snap.sh
Code language: CSS (css)

You can see that it starts removing the older version of packages. If you check the disk space used by Snap now, you’ll see that the directory size is reduced now.

Conclusion

In this tutorial we found a way to clean up our disk space from the snap packages that we didn’t need by using a simple bash script…

Leave a Reply