Last Updated:

Disk full error when installing kernel

masonbee Kubuntu

I recently had an error on Kubuntu 20.04 where the latest kernel was refusing to install with the error,

Unpacking linux-image-5.4.0-42-generic (5.4.0-42.46) ...dpkg: error processing archive /var/cache/apt/archives/linux-image-5.4.0-42-generic_5.4.0-42.46_amd64.deb (--unpack): cannot copy extracted data for './boot/vmlinuz-5.4.0-42-generic' to '/boot/vmlinuz-5.4.0-42-generic.dpkg-new': failed to write (No space left on device)No apport report written because the error message indicates a disk full error                                                                              dpkg-deb: error: paste subprocess was killed by signal (Broken pipe)Errors were encountered while processing: /var/cache/apt/archives/linux-image-5.4.0-42-generic_5.4.0-42.46_amd64.deb

....which was unexpected. What it meant was that my boot partition was full. If you have this error you can check your partitions easily with the following command.

df -h

Then look for the usage of the /boot partition. If it is 100% (or near) then this might well be your problem.

Once I saw my boot was full I went looking for a way to remove the old ones which is actually why I am writing this. It turned out to be trickier than I thought as --purge auto remove didn't work with either apt-get or apt.

sudo apt-get ––purge autoremove

Instead I used this method to remove old kernels that I found at PheonixNAP.

Step 1

Find your current kernel

This can be done using the command,

uname -r

On my computer at the moment I am using kernel 5.4.0-40-generic so I definitely will not remove that one.

Step 2

List your kernels

This can be done using the command,

dpkg -l | tail -n +6 | grep -E 'linux-image-[0-9]+'

This will give you something like the list below. Basically it is a list of your installed kernels.

Looking at the list you can see the oldest kernel I have is called linux-image-5.4.0-39-generic so that is the one you want to remove.

Step 3

Remove the oldest kernel

To remove the oldest kernel you need to use a command like,

sudo dpkg --purge linux-image-5.4.0-39-generic

You can copy and paste the name from the terminal (Konsole in this case). It will give you an error though but it is a very useful error as it gives us the name of the second package we need to remove. In the above case the relevant line in the error was,

linux-modules-extra-5.4.0-39-generic depends on linux-image-5.4.0-39-generic

It's just the package manager telling you you will need to remove both packages. So repeat Step 3 again but this time with the dependant package as well.

sudo dpkg --purge linux-image-5.4.0-39-generic linux-modules-extra-5.4.0-39-generic

(Warning, if this starts spiralling into removing many packages then you probably have either a different problem, have not followed the instructions, or there is something very different about your set-up. Think carefully before deleting.)

And that is basically it. Repeat and delete each oldest unneeded kernel until you have enough space free in your boot. Leave a couple around though in case you need them.