Identifying large packages installed on Debian

Last week I talked about my quick fix to freeing up disk space on the
root / partition of my Debian system. I also talked about potentially resizing
my partitions at some point to help stave off the issue of root / filling up
so quickly.

Since the system is my daily driver during the week, I don’t necessarily want to
jeopardize an otherwise stable situation.

The next best thing to simply resizing the partitions, was to figure out which
packages I have installed that are taking up a ton of room, and then figuring
out which ones I don’t need.

Coincidentally, I had some something similar a few months back, removing a bunch
of language packs that were installed by default with Firefox ESR.
While useful to provide multiple-language support out of the box, it didn’t
make a ton of long term sense to have them around once you determine which
language you are going to use for your day-to-day activities.

Less packages installed means less packages to potentially update in the future,
and that’s a good thing.

So at this point, I’ve cleaned my apt cache and have removed nearly every
single unnecessary package on my system, in addition to auto-removing the
packages the system has deemed unnecessary.

But have to actually gotten rid of everything I no longer need?

While I wasn’t immediately certain if I had, I thought it would be good to see
what packages I had installed, and how much space they were taking up.

To accomplish this, you have to pipe | a handful of commands together. The
first of which is dpkg-query. If you run it with the --show command you’ll
get a quick list of what you have installed and the version of the package:

% dpkg-query --show

Great if you want the list, not great if you want the size of the packages.
Fortunately, you can pass in the --showformat argument, and tell it to show
you the installed size of the package:

% dpkg-query --show --showformat='${Package;-50}t${Installed-Size}n'

Now we have the package names, and their installed size, in kilobytes (KB). Next
up would be to sort thing, to get a better sense for which packages are the
largest:

% dpkg-query --show --showformat='${Package;-50}t${Installed-Size}n' 
| sort -k 2 -n

Great, at the end of the list, we can see which packages are the largest!

At this point, we actually have enough to go on to identifying any large
packages that we may not need, like old versions of the Linux kernel that may no
longer be in use.

But, the current output contains packages that are flagged for removal (if you
haven’t run apt autoremove before running this). Also, it would be really nice
to put the size in a more human-readable format instead of just a number.

To accomplish both of those things, we can continue to pipe | things along to
grep and awk and then we’ll have a really nice looking output:

% dpkg-query --show --showformat='${Package;-50}t${Installed-Size}n' 
| sort -k 2 -n  # Put it in order
| grep -v deinstall  # Omit the packages scheduled for deletion already
| awk '{printf "%.3f MB t %sn", $2/(1024), $1}' # Make it pretty

As I mentioned, things like old versions of the Linux kernel may still be
hanging out on your system, so you could take this one step further and slap
another | grep 'linux-image' at the end, which will give you output similar to
this:

0.013 MB 	   linux-image-amd64
288.112 MB 	 linux-image-5.10.0-16-amd64
302.977 MB 	 linux-image-5.10.0-20-amd64
455.689 MB 	 linux-image-6.0.0-6-amd64
483.875 MB 	 linux-image-6.1.0-2-amd64

While not a ton of space, there is about a gig of space being taken up by old
images on my system. Given the small size of my root / partition, this is
actually a non-trivial chunk.

That being said, I always like to keep at least one version behind the latest
available on my system, so I’ll only end up removing the 5.10 versions.

Josh Sherman - The Man, The Myth, The Avatar

About Josh

Husband. Father. Pug dad. Musician. Founder of Holiday API, Head of Engineering and Emoji Specialist at Mailshake, and author of the best damn Lorem Ipsum Library for PHP.


If you found this article helpful, please consider buying me a coffee.