Ubuntu/Debian: Not enough free space on disk ‘/boot’ when updating the OS

My /boot partition is only 512MB and I get this error message every now and then when updating:

Not enough free space

The upgrade needs a total of xx.x M free space on disk ‘/boot’. Please free at least an additional xx.x M of disk space on ‘/boot’. You can remove old kernels using ‘sudo apt autoremove’, and you could set COMPRESS=xz in /etc/initramfs-tools/initramfs.conf to reduce the size of your initramfs.

The obvious process is to expand /boot to be at least 1GB and be more careful in the future when partitioning during the OS installation.

Luckily there are a couple of things to try before repartitioning.

Try cleaning old kernels automatically:

# apt autoremove

Compress your initramfs by editing /etc/initramfs-tools/initramfs.conf

# vim /etc/initramfs-tools/initramfs.conf

and change the COMPRESS entry to:

COMPRESS=xz

You might need to rebuild your initramfs for the compression to start applying.

If after doing the above you still don’t have enough free space you can manually delete old kernels.

First check which Linux kernel you are on:

# uname -r

4.15.0-76-generic

In the example above the current kernel is 4.15.0-76. It is really important that the current used kernel is left untouched on the system. Under no circumstances should it be removed.

Check which kernels are on your system:

# dpkg -l | grep linux-image

rc  linux-image-4.15.0-55-generic              4.15.0-55.60                                     amd64        Signed kernel image generic
rc  linux-image-4.15.0-58-generic              4.15.0-58.64                                     amd64        Signed kernel image generic
rc  linux-image-4.15.0-60-generic              4.15.0-60.67                                     amd64        Signed kernel image generic
rc  linux-image-4.15.0-62-generic              4.15.0-62.69                                     amd64        Signed kernel image generic
rc  linux-image-4.15.0-64-generic              4.15.0-64.73                                     amd64        Signed kernel image generic
rc  linux-image-4.15.0-65-generic              4.15.0-65.74                                     amd64        Signed kernel image generic
rc  linux-image-4.15.0-66-generic              4.15.0-66.75                                     amd64        Signed kernel image generic
rc  linux-image-4.15.0-69-generic              4.15.0-69.78                                     amd64        Signed kernel image generic
rc  linux-image-4.15.0-70-generic              4.15.0-70.79                                     amd64        Signed kernel image generic
ii  linux-image-4.15.0-72-generic              4.15.0-72.81                                     amd64        Signed kernel image generic
ii  linux-image-4.15.0-74-generic              4.15.0-74.84                                     amd64        Signed kernel image generic
ii  linux-image-4.15.0-76-generic              4.15.0-76.86                                     amd64        Signed kernel image generic
ii  linux-image-generic                        4.15.0.76.78                                     amd64        Generic Linux kernel image

The first column of the output provides a 2-3 letter code with useful information on the status of each package.

For reference this is their meaning:

First letter. Desired package state:

u ... unknown
i ... install
r ... remove/deinstall
p ... purge (remove including config files)
h ... hold

Second letter. Current package state:

n ... not-installed
i ... installed
c ... config-files (only config files are installed)
U ... unpacked
F ... half-configured (configuration failed for some reason)
h ... half-installed (installation failed for some reason)
W ... triggers-awaited (package is waiting for a trigger from another package)
t ... triggers-pending (package has been triggered)

Third letter. Error state:

R ... reinstallation-required (package broken, reinstallation required)

From the previous output we know that there are some config files left around (rc header), and that several kernel images are still installed (ii header).

The ii ones are the ones consuming the space we need to free up. We need to remove some of those.

We have to keep the current kernel version and at least one or two previous versions as good practice.

So based on all of the above and in this example:

To remove
linux-image-4.15.0-55-generic
linux-image-4.15.0-58-generic
linux-image-4.15.0-60-generic
linux-image-4.15.0-62-generic
linux-image-4.15.0-64-generic
linux-image-4.15.0-65-generic
linux-image-4.15.0-66-generic
linux-image-4.15.0-69-generic
linux-image-4.15.0-70-generic
linux-image-4.15.0-72-generic

To keep
linux-image-4.15.0-74-generic (previous)
linux-image-4.15.0-76-generic (current)

You can remove them one by one:

# apt purge linux-image-4.15.0-55-generic
# apt purge linux-image-4.15.0-58-generic
# apt purge linux-image-4.15.0-60-generic
.
.
.

Or all of them in one go with:

# apt purge linux-image-4.15.0-{55,58,60,62,64,65,66,69,60,72}-generic

This will free up enough space of /boot until you repartition.