Quickzi: How To Remove Older Kernels from Ubuntu

If you’ve had the same Ubuntu installation for a while and have just been upgrading to newer releases, you may have noticed that a lot of older kernel versions are piling up in your grub menu and on your system.

How to remove older kernels from Ubuntu

This can be done by using the Synaptic Package Manager, however I will show you how it is done on the command line.

First, find out what kernel you are currently running:

# uname -a

Linux foogazi 2.6.24-19-generic #1 SMP Wed Jun 18 14:43:41 UTC 2008 i686 GNU/Linux

From the output you can see that you are currently using the 2.6.24-19-generic kernel.

Next, let’s take a look at all of the kernel versions you have installed:

# dpkg -l | grep linux-headers-*

linux-headers-2.6.24-16                    2.6.24-16.30                                       Header files related to Linux kernel version
linux-headers-2.6.24-16-generic            2.6.24-16.30                                       Linux kernel headers for version 2.6.24 on x
linux-headers-2.6.24-19                    2.6.24-19.34                                       Header files related to Linux kernel version
linux-headers-2.6.24-19-generic            2.6.24-19.34                                       Linux kernel headers for version 2.6.24 on x
linux-headers-generic                                                   Generic Linux kernel headers

Now all you need to do is remove the old versions with apt-get.  Since we’ve noted with uname -a that we are currently running 2.6.24-19-generic we want to make sure we do not remove it.  All of the others can be removed.

# sudo apt-get remove linux-headers-2.6.24-16 linux-headers-2.6.24-16-generic

Now the older kernels are gone.  Repeat the apt-get remove step to remove any others you may have.  Remember to not remove your current kernel.

Important note: It is a good idea to keep at least one old kernel version around in case anything breaks in your current kernel and you are unable to boot into it.  An example would be that you boot into your current kernel but recieve a kernel panic.  With an old kernel still available you can reboot the computer and select the older kernel version from the Grub menu and still access your system to find out what is going on.

10 thoughts on “Quickzi: How To Remove Older Kernels from Ubuntu

  1. Another interesting command is ‘apt-cache policy’ which gives more information than ‘dpkg -l’

  2. There is a much easier and quicker way:

    sudo apt-get autoremove

    This will scan for absolete kernels and delete them (after user confirmation).

  3. What w-sky is sorta wrong. I’ve got several kernel versions that do not get removed by that, and I do not remember if that ever did anything useful for grub menu kernel cleanup. Additionally, the hunt and peck method by the blog author is prone to “oops” due to the attention to detail required

    I use this command:

    # dpkg -l | grep ^ii | grep linux-image | grep -v image-generic | grep -v `uname -r | cut -d’-’ -f1,2` | cut -d’ ‘ -f3 | xargs apt-get remove -y && update-initramfs -u

    It says: list the packages (|), find the ones that are installed (|) and named linux-image (|), but not the image-generic (|). Then take the one that we are currently using (`) out of the list (|). Now remove every image left in that list (|) and update my grub menu(&&).

    You will not be left with a backup kernel, but I’ve never had a problem that could not be solved with the recovery mode of the current kernel.

  4. The shell command nthalk supplied is excellent! I especially appreciate the description of what it’s doing. UNFORTUNATELY this site’s blog software replaced all the single quotes with the “pretty” ones, so you cannot cut and paste the command and expect it to work.

    To play with it, copy everything up to before the last pipe | character, open a terminal, paste it in (using either Edit –> Paste or shift-control-P) then backspace and replace all the curly single quotes with the non-curly single quote (next to the Enter key on US keyboards). When you press Enter, you should see the list of outdated kernels.

    Then once you feel confident you can up-arrow to see the successful command again and type or paste the last bit

    | xargs apt-get remove -y && update-initramfs -u

    I don’t know if this blog allows any tags like code but if folks are pasting in code it would be nice to have “pretty” quotes and whatever else turned off. Thanks to nthalk and Foogazi!

  5. doesn’t work, I get this error:

    “Could not get lock /var/lib/dpkg/lock”

    Wonder why Linux makes it so difficult to remove older versions?

  6. Farky,

    This usually means you are not running as root (sudo command) or you have apt-get running somewhere else.

    To fix it for me, I changed the last portion of nthalk’s to:

    | xargs sudo apt-get remove -y && sudo update-initramfs -u

  7. @Farky If you’re running Synaptic Package Manager at the same time as trying to run a dpkg command, you’ll get that. This is because you may only run one instance of apt-get at a time, and as I understand it, Synaptic grabs the lock the whole time it’s running. All you need to do is quit Synaptic and try it again, it should be no problem.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>