Minimizing Debian

Reducing the install size of Debian is a tough job, yet its minimal size is still far bigger than basic Alpine installation. The thing is that its kernel package (including modules) is already few 100s MB big, so minimization is almost pointless on VMs and physical machines. On the other hand, it an be reduced to around 250~300 MB on LXC with a little effort.

Use Aptitude

The first trick is to use Aptitude, and its "Mark as auto" (Shift+M) shortcut. On tree package view, simply mark the entire package group as auto, then Aptitude will happily weed out most unimportant packages.

Some useful tips:

  • - key to uninstall package. (Works on category, too)

  • m key to mark package as manual (Works on category, too)

  • Listing only manually installed packages:

    1. Views menu -> New Flat Package List.

    2. l (Limit view) -> ~i!~M!~v (installed, not-auto, not-virtual)

    search terms are so useful.

At the end of the day, there will be only dozens of manually installed packages, that are really necessary for spinning up an SSH daemon.

WARNING: Not every critical packages are protected. Some important packages can get deleted through this method, but avoiding this might take lots of experiments. Make sure to read package description.

No more feature hoarding

Another trick is to weeding out recommended/suggested packages. Debian, by default, installs almost all features you might ever need in your lifetime. This makes Debian way much more convinient than competitors, but, yeah, those packages are just dead weights on containers.

Through configuration, you can not only disable auto-installation, but also can stop treating Suggests/Recommends as a kind of dependency. That is, you can automatically uninstall any "extra" packages installed for "extra" features.

To do this, add the following to /etc/apt/apt.conf.d/99norecommend:

APT::Install-Recommends "false";
APT::AutoRemove::RecommendsImportant "false";
APT::AutoRemove::SuggestsImportant "false";

This reduces the number of packages installed, and apt autoremove might remove some more packages than usual. But this becomes a strong weapon when combined with the Aptitude trick mentioned above. You'll be removing dozens of packages with single key press.

Localepurge

This is a pure hack. Even the package description claims it's a hack. Localpurge is a script that removes unused locales and translation. What makes this great is that it utilizes dpkg features to keep things tidy. It can either:

  1. Delete files after every package changes through hooks.

  2. Prevent installing locales by configuring dpkg.

Personally, I prefer the first option, because I like the feeling of "deleting" something.

Delete documentation

/usr/share/doc is officially safe to delete, since no operation should rely on the existence of such files. They are often useful, but, it's full of CHANGELOG, LICENSE, and examples with gigantic comment blocks, few of which you'll ever read.

rm -rf /usr/share/doc/*

Limitations

  • Kernel is large. Abandon hope, all ye who enter here.

  • Libc is large. That's why people keep using musl and uclibc.

  • Package registry (/var/lib/dpkg) is large, but you MUST NOT delete it.

  • Deleting /var/lib/apt will also erase "auto" markings on packages, making every package "non-automatic". If you're going to delete it, do so only at the very last moment. (i.e. before snapshoting)