Keeping the Ubuntu system clean and running smoothly often requires some maintenance, one aspect of which is managing old kernel versions. Over time, as the system is updated, multiple kernel versions accumulate, taking up valuable disk space and sometimes causing confusion during boot. Let’s learn the necessary commands to remove old kernel versions in Ubuntu.
Before removing old kernels, it’s important to check which kernel versions are currently installed on our system. This will help us identify which ones can be safely removed. We can do that using the following command:
dpkg --list | grep linux-image
This command will generate a list of all installed kernel versions on our system. We’ll carefully examine the output to identify the older kernels that we no longer need. As a general rule, we’ll keep the latest kernel version for stability and security and optionally retain one older version as a backup.
Once the old kernel versions have been identified that are no longer needed, we can proceed to remove them using the apt package manager. To do this, we’ll replace x.x.x-generic
with the specific version number of the kernel we wish to remove:
sudo apt-get purge linux-image-x.x.x-generic
For example, if we want to remove the kernel version 5.4.0-137-generic we can use the following command:
sudo apt-get purge linux-image-5.4.0-137-generic -y
Note: The
-y
flag in theapt-get install
command is used to automatically answer “yes” to all prompts and questions that the installation process might raise.
After removing the old kernel versions, it’s a good practice to update the package list to ensure that the system is aware of the latest available software versions:
sudo apt-get update
To remove any unused dependencies, we can run sudo apt autoremove
. This will remove packages installed as dependencies for the old kernel versions but are no longer needed.
Now, let’s try to install a linux image and then delete it using the following commands:
List all the installed kernel versions:
dpkg --list | grep linux-image
Update the packages:
sudo apt-get update
Install the Linux image linux-image-5.4.0-137-generic
:
sudo apt-get install linux-image-5.4.0-137-generic -y
Verify if the Linux image linux-image-5.4.0-137-generic
is successfully installed:
dpkg --list | grep linux-image
Delete the newly installed Linux image:
sudo apt-get purge linux-image-5.4.0-137-generic -y > /dev/null 2>&1
Let's practice the commands above in the following terminal to see how the process works:
By following the outlined steps outlined, we can efficiently remove old kernel versions from our Ubuntu system, freeing up valuable disk space and maintaining a clutter-free environment. We have to keep the package list updated and retain at least one backup kernel version to ensure system stability.