Boot partition full, failing to delete old kernels. Broken packages

Solution 1:

By using linux-purge

I have created a helper script for this purpose. It is called linux-purge and its project page is here in Launchpad.net. In the announcements section there are installation instructions here.

In this specific case you would run

sudo linux-purge --fix

Manual method

As an alternative, there are instructions on how to do the task manually at Communiy Wiki Help which I have contributed to (as jarnos).

Anyway, here are my manual instructions as an archive:

First remove any leftover temporary files from previous kernel updates.

sudo rm -rv ${TMPDIR:-/var/tmp}/mkinitramfs-*

(There may be some due to bug in Ubuntu 16.04 and older.)

List the successfully installed kernels excluding booted kernel by command:

dpkg-query -W -f='${Status} ${Package}\n' | awk '/^[^ ]+ ok installed linux-image-[0-9]+/{print $4}' | grep -Fv $(uname -r)

Delete the initrd.img file of an unneeded kernel (due to bug); here the chosen kernel release is 4.2.0-23-generic:

sudo update-initramfs -d -k 4.2.0-23-generic

Purge the kernel by

sudo dpkg --purge linux-image-extra-4.2.0-23-generic linux-image-4.2.0-23-generic

to free some space in /boot. If the command fails, some installed package depends on the kernel. The output of dpkg tells the name of the package. Purge it first.

You may also purge respective header package

sudo dpkg --purge linux-headers-4.2.0-23-generic

and even the common header package

sudo dpkg --purge linux-headers-4.2.0-23

if no other package depends on it; otherwise the command will fail, but it is safe to run.

Then run

sudo apt-get install -f

to fix the broken dependency. If that fails due to insufficient disk space, you have to remove another kernel the way told above.

Note that there still may be too many kernels installed in your system, but now you should be able to purge the extra ones by e.g. sudo apt-get purge followed by kernel package name(s).