How to remove old kernels on Ubuntu

The Linux kernel is the core Ubuntu Linux, as well as all other Linux distributions. In this tutorial you will learn how to remove old kernels from Ubuntu Linux system.

Like the rest of the system components, the Linux kernel also gets updated every once in a while. When the kernel is updated on Ubuntu, the operating system will keep some old kernels lying around, in case there is a problem with a new kernel and you need to revert to an older one.

This is a nice feature, although the old kernels can take up storage space. In this tutorial, we will show you how to view the old kernels installed on your Ubuntu system, and also how to remove them.

In this tutorial you will learn:

  • How to remove all old kernels with apt command
  • How to remove a specific kernel with apt command
  • How to view a list of all installed kernels
  • How to view kernel version currently in use
  • How to upgrade to the lastest Ubuntu kernel
How to remove old kernels on Ubuntu
How to remove old kernels on Ubuntu
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software N/A
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

How to remove old kernels from Ubuntu




Ubuntu installs new kernels whenever you do a full system upgrade. The new kernel will not be immediately usable, but after a reboot, your system will boot into the new kernel that was downloaded. As mentioned before, the old kernel will stick around in case there is a problem with the new one.

To upgrade your kernel on Ubuntu, you would use the following two apt commands and then reboot:

$ sudo apt update
$ sudo apt full-upgrade
$ reboot

To see a list of your Linux kernels on Ubuntu, execute the following dpkg command:

$ sudo dpkg --list | egrep 'linux-image|linux-headers'
Viewing the Linux kernels installed on Ubuntu Linux
Viewing the Linux kernels installed on Ubuntu Linux

At the start of each line, you will see a few letters. This is a two letter abbreviation, with the first letter indicating the desired package state, and the second letter indicating the current package state. We will keep things simple and just tell you what the abbreviations mean:

  • ii – indicates packages that are currently installed
  • iU – package has been unpacked and will be used next reboot
  • rc – package already removed, but the configuration files are still present

To see which Linux kernel your Ubuntu system is currently running, you can run the uname command.

$ uname -r
5.13.0-35-generic

Next, let’s look at how to remove these older kernels and their configuration files:

  1. The recommended method to remove old kernels on Ubuntu is with the apt package manager. The following command will remove old kernels, as well as lingering software packages that are no longer required on your system. It is a good idea to occasionally run this command just to free up disk space.
    $ sudo apt autoremove --purge
    

    Note that this command will still keep one old kernel, along with the newest one that Ubuntu is currently running.



  2. You can manually remove one or more of the kernels with an apt command. Use the dpkg command shown earlier in order to get a list of kernels that you can remove. The following Linux kernel is just an example.
    $ sudo apt purge linux-image-5.8.0-50-generic
    

    Removing a specific Linux kernel from Ubuntu
    Removing a specific Linux kernel from Ubuntu

Closing Thoughts




In this tutorial, we saw how to remove old kernels from Ubuntu. We also learned how to view which kernels are currently installed, and why this feature is implemented into Ubuntu in the first place. Deleting old kernels can free up a little disk space, and running the apt autoremove command is a good habit just to keep a clean system and remove unused packages.