How to remove old kernel versions in Ubuntu

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.

1. Check for the installed kernel versions

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
Command to check for the installed kernel versions

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.

2. Remove old kernel versions

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
Command to remove old kernel version

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
Command to remove old kernel version

Note: The -y flag in the apt-get install command is used to automatically answer “yes” to all prompts and questions that the installation process might raise.

3. Update the packages

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
Command to update the packages

4. Autoremove unused packages (Optional)

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.

Remember to exercise caution and double-check the kernel versions removed to avoid unintended consequences.

Let’s try the commands

Now, let’s try to install a linux image and then delete it using the following commands:

  1. List all the installed kernel versions:

dpkg --list | grep linux-image
Command to check installed kernel versions
  1. Update the packages:

sudo apt-get update
Command to update the packages
  1. Install the Linux image linux-image-5.4.0-137-generic:

sudo apt-get install linux-image-5.4.0-137-generic -y
Command to install the Linux image
  1. Verify if the Linux image linux-image-5.4.0-137-generic is successfully installed:

dpkg --list | grep linux-image
Command to verify if the Linux image is installed
  1. Delete the newly installed Linux image:

sudo apt-get purge linux-image-5.4.0-137-generic -y > /dev/null 2>&1
Command to delete the installed Linux image

Let's practice the commands above in the following terminal to see how the process works:

Terminal 1
Terminal
Loading...

Conclusion

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.

Copyright ©2024 Educative, Inc. All rights reserved