Updated on March 16, 2023.
Introduction
A Linux kernel is the core of the operating system. As new versions of Linux are released, your system updates the kernel.
By default, modern Linux versions keep the current kernel, plus one older version. In some instances, Linux doesn’t remove old kernel versions. A common problem when keeping old kernels having an extensive list of bootable images on the GRUB (boot) menu.
This guide will help you remove old and unused Linux kernels on your Ubuntu system (20.04 and 22.04).
Prerequisites
- A system running Ubuntu (tested on Ubuntu 20.04 and 22.04).
- A terminal window/command line (Ctrl+Alt+T)
- A user account with sudo privileges
Removing Old Kernels on Ubuntu
Unused old kernels on Ubuntu take up disk space. Check for old kernel packages with the following command:
dpkg --list | egrep -i --color 'linux-image|linux-headers'
The output shows a list of installed kernels with the following flags:
ii
- Marked for installation (i
) and currently installed (i
). Packages with these flags are safe to remove.rc
- Removed (r
) and configuration files present (c
). The package is removed, and configuration files require purging.iU
- Marked for installation (i
) and unpacked (U
) to install at the next reboot. Do not remove packages with this flag.
Do not remove any packages that have the current kernel version. To see the current version, use the uname command with the -r
flag:
uname -r
There are several ways to remove the old kernel packages from the system.
Using apt autoremove Command
Use the apt command with the autoremove
option to automatically remove all old kernels and unrequired packages on the system. Run the following command in the terminal:
sudo apt autoremove --purge
The command keeps only the latest and one prior version on the system.
Note: Alternatively, use the apt-get command for the same results. Read about the difference between apt vs. apt-get.
Using apt remove Command
The apt remove
command removes a specified package without removing the dependencies. Use the following commands:
sudo apt remove --purge linux-headers-[version]
sudo apt remove --purge linux-image-[version]
Press Y to confirm the deletion and wait for the process to complete.
Using GUI
A GUI and graphical tools providee a simple overview and help prevent costly mistakes. This section focuses on how to remove old kernels using Synaptic. The program is a graphical front-end for the apt package manager.
Follow the steps below to install Synaptic and remove old kernels.
Install and Run Synaptic
1. Install Synaptic with the following:
sudo apt install synaptic -y
The installation takes a few moments to complete.
2. Launch the Synaptic interface from the terminal by typing:
sudo synaptic
The Synaptic Package Manager graphical interface opens in a new window.
Remove Old Kernels
1. Click the Sections button in the left menu.
2. Locate the Kernel and modules option on the list.
3. Right-click the kernel to remove in the left menu and select the Mark for Complete Removal option.
Synaptic lists additional dependency packages for the kernel.
4. Click the Mark option to mark the dependencies for removal.
5. Review the marked packages and click Apply to mark the packages for removal.
6. Click Apply in the pop-up Summary window to start the removal.
7. A progress bar and the details window show the removal process. Check the box to close the window automatically after removal.
Alternatively, close the window manually once the removal completes.
Conclusion
You know several methods to remove old kernels from Ubuntu 20.04 or 22.04. Purging old kernels can help you recover wasted disk space in Linux. It’s also considered a best practice to prune out old files and dependencies.