Without the Linux kernel, Ubuntu (and all the other Linux distros) would not exist. Despite this, most Linux users don’t know what a kernel is nor understand that the reason that Ubuntu is a Linux distro is because the kernel is Linux. All most users of any Linux distro is likely to know is that they will eventually have a large number of kernels installed on their system cluttering up their boot menu.
Describing what a kernel is and what it does is beyond the scope of this post. I do highly recommend that you first read the Wikipedia article on computer kernels followed by the article on the Linux kernel itself. I’d really like to do a post explaining what a kernel is in simple terms so that readers can gain a better appreciation for it and what it does for your distro. Please leave a comment letting me know if you are interested in this writeup.
Anyways, back to the topic at hand. What do we do with all these kernels that litter our boot menu? How do we remove them not just from our boot menu but from our file system as well?
Why Do I Have All These Kernels?
Before we get onto those questions, there is an important first question to answer: Why are all these kernels left on the system in the first place?
As you do system updates using Synaptic, new kernels will be installed as they become available in your repositories. New kernels will always take precedence over the older kernels in the boot menu.
The reason that the older kernels aren’t automatically removed is to ensure that the new kernel can be loaded properly. If the new kernel resulted in a system that could not be booted, you can easily boot into an older kernel to fix the problem.
So, while this system of leaving the old kernels alone is a great way to ensure system stability and availability, it does leave quite a mess after a while.
Make Note of Your Current Kernel
This is very, very important. You don’t want to remove your current kernel, as that will cause all sorts of problems. So, you will want to find out what your current kernel is called.
Load up Terminal (Applications > Accessories > Terminal) and run the following:
The response will tell you which kernel you are currently running. For example, one of my home systems responded with:
So, this kernel is the one that I don’t want to touch.
Removing the Old Kernels
Now that we know what not to remove, it’s time to find the rest and get rid of them. Fortunately, this is very simple to do in Ubuntu since we have the Synaptic package manager.
Load up Synaptic (System > Administration > Synaptic Package Manager). Type in “linux-image” in the “Quick search” box and selected “Installed” from the pane on the left.
Unfortunately, this search will produce many more results than just the specific ones we want. Click the Package listing title to sort the list by package. Scroll through the list until you come across the listings that begin with “linux-image”. Depending on the number of kernels you have installed, you may have a large number of results.
In addition to the “linux-image-*” packages, we also need to remove the “linux-headers-*” and “linux-restricted-modules-*” packages for the old kernels.
Important: Do not remove the linux-generic, linux-headers-generic, linux-image-generic, linux-restricted-modules-common, or linux-restricted-modules-generic packages. In other words, only remove packages that have the version number of the kernel in the package name.
On my system, that means that I need to remove the following packages:
For each kernel that you need to remove, right-click the listing and select “Mark for Removal”. You will likely be presented with a screen saying that additional packages will need to be marked for removal. Click the Mark button to include the necessary packages to the removal list. Unfortunately, when you do this, the list resorts, simply click the Package header again to sort by package names and find your spot in the list again to keep marking packages for removal.
Once you have all the packages you need to remove marked, double-check that you have not marked your current kernel’s packages to be removed. In my case, I need to keep the following packages:
Now that you’ve marked all the packages you need to remove while double-checking to ensure that you aren’t removing your current kernel packages, click the Apply button at the top of Synaptic to have the packages removed.
Once the removal process is done, I click the Reload button in Synaptic, change the selection in the left pane from Installed to All, and verify that the current kernel packages are installed. This may be a bit overly-paranoid, but I would rather be paranoid than have a failed OS on my hands.
While removing your old kernels isn’t strictly necessary, it does streamline your boot options while clearing out some old files and packages. It’s a good idea to clean out old kernels as part of routine system maintenance since the old kernels won’t take care of themselves.
Did I help you? Send me a tip.