Ubuntu – How to remove old kernel versions to clean up the boot menu

cleanupgrub2kernel

Every time I install a new Linux kernel, it gets left in the grub_config, making the boot menu longer each time.

I know I can manually search through the installed packages and remove them.

Does Ubuntu provide any easier way to clean them up or keep them from showing in the boot list?

Best Answer

  • 16.04 and newer versions of Ubuntu

    sudo apt autoremove
    

    This command removes packages that were automatically installed to resolve a dependency, but are now no longer depended on. This includes old versions of linux-headers-* and linux-image-*. (It’s also smart about this process, leaving one spare version of the kernel around as a fallback!)

    11.10 and newer versions of Ubuntu

    GRUB2 and its display of all kernels

    The latest versions of Grub2 installed in Ubuntu automatically display the latest kernel and hides the older kernels that you may have installed.

    GNU GRUB

    If you do not see your grub - then remember to press Shift whilst booting.

    As you can see, only the latest kernel is displayed.

    If you select the option shown (press Enter) then all the old kernels become visible and available to boot from.

    GNU GRUB Previous versions

    How to permanently delete older kernels

    First boot with the latest available kernel.

    There are a number of ways to delete old kernels. Personally, I wouldn't touch Computer Janitor since this is acknowledged to break your computer with its suggestions.

    synaptic

    An alternative is Synaptic (sudo apt install synaptic)

    search for linux-image, right-click a kernel and choose complete removal and finally click the Apply button to delete the kernel.

    Synaptic Package Manager

    Repeat the search but this time for linux-header - you can delete the associated headers for the kernel image chosen previously.

    Synaptic though will not attempt to verify what you are trying to remove... you could inadvertently delete your newest kernel - or even delete all of your kernels via this tool leaving you with an unbootable Ubuntu!.

    Remember to check which kernel you are using type:

    uname -r
    

    The result would be similar to:

    Terminal <uname -r>

    Remember the result and the number - make sure you don't delete the corresponding image or header.

    Recommendation

    My recommendation is to keep at least two or preferably three kernels including the latest. The reason for the recommendation is that you will have at least one/two other kernels to boot with, if for what-ever reason the latest kernel you are unable to boot with or introducing a regressed capability such as broken wireless.