15

How does a user list the size of each installed snap package in Ubuntu 20.04? The purpose is to get an overview perspective of the disk space utilised by each installed snap package. The command snap list does not do this. Also, the app Disk Usage Analyzer could not show further than /var/lib/snapd/snaps.

disk usage analyzer

Sun Bear
  • 3,269

2 Answers2

11

All the snap packages are stored in /var/lib/snapd/snaps/. This directory contains all the revisions and names of the snap packages. You can run ls in the directory to list all the snap packages. Using -lh argument with ls will give you a more detailed output along with the size of the packages (and each of it's revisions). Thus, you can run the following command to list all the snap packages along with their installed size:

ls -lh /var/lib/snapd/snaps/

Alternatively, you can use the du command with -hcs argument to list the size of all the installed packages:

du -hcs /var/lib/snapd/snaps/* 
Error404
  • 8,333
2

snap list limitations

At a time of writing, July 2022, snap list command indeed doesn't support configuration for columns it would output, it's a hardcoded behavior: https://github.com/snapcore/snapd/blob/293c48dc2b97f141d1bc35d5ea91c2e3484f01fd/cmd/snap/cmd_list.go

Using snap API

Hovewer, according to the official documentation, https://snapcraft.io/docs/using-the-api, snap daemon (snapd) exposes its API via unix socket at /run/snapd.socket.

$ netstat --unix --listening --programs | grep snap
unix  2      [ ACC ]     STREAM     LISTENING     25415    4229/systemd        /run/user/1000/snapd-session-agent.socket
unix  2      [ ACC ]     STREAM     LISTENING     19241    -                   /run/snapd.socket
unix  2      [ ACC ]     STREAM     LISTENING     19242    -                   /run/snapd-snap.socket

The /snaps endpoint (https://snapcraft.io/docs/snapd-api#heading--snaps) return machine-readable (JSON) information about all installed snaps.

And it includes "installed-size" property as well.

As suggested in the previously mentioned help page (https://snapcraft.io/docs/using-the-api#heading--accessing), you can get this information by issuing the following command

curl --silent --show-error --unix-socket /run/snapd.socket http://localhost/v2/snaps?select=all

You can then process and analyze this data using any tool you like, would it be jq, python or something else.

Example of listing the size of each installed snap

Example on data from my system, with ruby 3.0.1.

Command (shell):

ruby -rjson -rtime -rterminal-table -e '
json = JSON.parse(ARGF.read)

columns = [*%w(name revision), %w(publisher display-name), *%w(confinement install-date installed-size mounted-from)]

formatters = { "install-date" => lambda { Time.parse(_1).getlocal.strftime("%x %X") rescue "" }, "installed-size" => lambda { numfmt --to=iec #{_1}.chop }, "mounted-from" => lambda { File.dirname(_1) } }

result = json["result"].map do |app| columns.map do |column| app.dig *column end end

puts Terminal::Table.new( headings: [ "#", *columns.map{ _1.respond_to?(:join) ? _1.join("-") : _1 } ], rows: result.sort_by{ _1[columns.index("name")] }.map.with_index do |row, index| [ index + 1, *row.map.with_index do |cell, index| formatters[columns[index]] ? formatters[columns[index]][cell] : cell end ] end << Array.new(columns.size).tap do |emptyrow| index = columns.index("installed-size")

        emptyrow[index + 1] = formatters[&quot;installed-size&quot;][
            result.reduce(0) do |total, row|
                total + row[index]
            end
        ]
end

)' <(curl --silent --show-error --unix-socket /run/snapd.socket http://localhost/v2/snaps?select=all)

Output:

+----+-----------------------------------+----------+------------------------+-------------+-------------------+----------------+----------------------+
| #  | name                              | revision | publisher-display-name | confinement | install-date      | installed-size | mounted-from         |
+----+-----------------------------------+----------+------------------------+-------------+-------------------+----------------+----------------------+
| 1  | atom                              | 282      | Snapcrafters           | classic     |                   | 286M           | /var/lib/snapd/snaps |
| 2  | atom                              | 286      | Snapcrafters           | classic     | 07/14/22 21:41:12 | 285M           | /var/lib/snapd/snaps |
| 3  | bare                              | 5        | Canonical              | strict      | 09/27/21 03:08:28 | 4.0K           | /var/lib/snapd/snaps |
| 4  | chromium                          | 2033     | Canonical              | strict      |                   | 134M           | /var/lib/snapd/snaps |
| 5  | chromium                          | 2036     | Canonical              | strict      | 07/25/22 23:17:33 | 134M           | /var/lib/snapd/snaps |
| 6  | code                              | 101      | Visual Studio Code     | classic     |                   | 222M           | /var/lib/snapd/snaps |
| 7  | code                              | 102      | Visual Studio Code     | classic     | 07/19/22 00:06:55 | 222M           | /var/lib/snapd/snaps |
| 8  | core                              | 13250    | Canonical              | strict      |                   | 112M           | /var/lib/snapd/snaps |
| 9  | core                              | 13425    | Canonical              | strict      | 07/14/22 21:38:16 | 114M           | /var/lib/snapd/snaps |
| 10 | core18                            | 2409     | Canonical              | strict      |                   | 56M            | /var/lib/snapd/snaps |
| 11 | core18                            | 2538     | Canonical              | strict      | 07/19/22 00:06:55 | 56M            | /var/lib/snapd/snaps |
| 12 | core20                            | 1581     | Canonical              | strict      |                   | 62M            | /var/lib/snapd/snaps |
| 13 | core20                            | 1587     | Canonical              | strict      | 07/27/22 00:03:07 | 62M            | /var/lib/snapd/snaps |
| 14 | digikam                           | 52       | KDE                    | strict      |                   | 184M           | /var/lib/snapd/snaps |
| 15 | digikam                           | 53       | KDE                    | strict      | 03/03/22 23:04:01 | 184M           | /var/lib/snapd/snaps |
| 16 | gnome-3-28-1804                   | 145      | Canonical              | strict      |                   | 163M           | /var/lib/snapd/snaps |
| 17 | gnome-3-28-1804                   | 161      | Canonical              | strict      | 07/27/21 00:54:00 | 165M           | /var/lib/snapd/snaps |
| 18 | gnome-3-34-1804                   | 72       | Canonical              | strict      |                   | 219M           | /var/lib/snapd/snaps |
| 19 | gnome-3-34-1804                   | 77       | Canonical              | strict      | 11/24/21 14:53:44 | 219M           | /var/lib/snapd/snaps |
| 20 | gnome-3-38-2004                   | 99       | Canonical              | strict      |                   | 249M           | /var/lib/snapd/snaps |
| 21 | gnome-3-38-2004                   | 112      | Canonical              | strict      | 07/14/22 21:42:19 | 401M           | /var/lib/snapd/snaps |
| 22 | gtk-common-themes                 | 1534     | Canonical              | strict      |                   | 82M            | /var/lib/snapd/snaps |
| 23 | gtk-common-themes                 | 1535     | Canonical              | strict      | 07/14/22 21:39:41 | 92M            | /var/lib/snapd/snaps |
| 24 | kde-frameworks-5-core18           | 32       | KDE                    | strict      | 03/15/20 10:20:17 | 261M           | /var/lib/snapd/snaps |
| 25 | kde-frameworks-5-qt-5-15-3-core20 | 5        | KDE                    | strict      |                   | 319M           | /var/lib/snapd/snaps |
| 26 | kde-frameworks-5-qt-5-15-3-core20 | 8        | KDE                    | strict      | 11/16/21 19:48:35 | 425M           | /var/lib/snapd/snaps |
| 27 | kde-frameworks-5-qt-5-15-core20   | 14       | KDE                    | strict      | 04/21/21 22:07:58 | 324M           | /var/lib/snapd/snaps |
| 28 | musescore                         | 216      | MuseScore              | strict      |                   | 176M           | /var/lib/snapd/snaps |
| 29 | musescore                         | 228      | MuseScore              | strict      | 05/22/22 18:36:22 | 176M           | /var/lib/snapd/snaps |
| 30 | nongnu-gsequencer                 | 243      | Joël Krähemann         | strict      |                   | 256M           | /var/lib/snapd/snaps |
| 31 | nongnu-gsequencer                 | 252      | Joël Krähemann         | strict      | 07/14/22 21:41:52 | 256M           | /var/lib/snapd/snaps |
| 32 | scrcpy                            | 386      | sisco311               | strict      | 07/31/22 00:49:17 | 83M            | /var/lib/snapd/snaps |
| 33 | shotcut                           | 692      | Meltytech, LLC         | classic     |                   | 82M            | /var/lib/snapd/snaps |
| 34 | shotcut                           | 765      | Meltytech, LLC         | classic     | 07/14/22 21:40:48 | 145M           | /var/lib/snapd/snaps |
| 35 | snap-store                        | 547      | Canonical              | strict      |                   | 51M            | /var/lib/snapd/snaps |
| 36 | snap-store                        | 558      | Canonical              | strict      | 11/24/21 14:53:52 | 55M            | /var/lib/snapd/snaps |
| 37 | whalebird                         | 119      | AkiraFukushima         | strict      |                   | 98M            | /var/lib/snapd/snaps |
| 38 | whalebird                         | 121      | AkiraFukushima         | strict      | 07/14/22 21:41:11 | 99M            | /var/lib/snapd/snaps |
|    |                                   |          |                        |             |                   | 6.4G           |
+----+-----------------------------------+----------+------------------------+-------------+-------------------+---------------------------------------+

Overall output matches that of snap list --all.

We can see that all these snaps are located in one (default) directory.

And running

$ du -h /var/lib/snapd/snaps/{atom*,bare*,chromium*}
286M    /var/lib/snapd/snaps/atom_282.snap
285M    /var/lib/snapd/snaps/atom_286.snap
4,0K    /var/lib/snapd/snaps/bare_5.snap
134M    /var/lib/snapd/snaps/chromium_2033.snap
134M    /var/lib/snapd/snaps/chromium_2036.snap
$ du -hs /var/lib/snapd/snaps
6,5G    /var/lib/snapd/snaps

would return a closely matching values.

Conclusion

We can see then, that approach of measuring space occupied by entries of /var/lib/snapd/snaps directory, suggested in an already accepted answer, wasn't wrong, as evidenced by the API data and official explanation found on https://snapcraft.io/docs/system-snap-directory.

aslian
  • 21