How to Remove Old kernel Versions from Boot on Ubuntu 18.04

Channel: Linux
Abstract: # apt remove linux-image-4.15.0-13-genericrc linux-image-4.15.0-13-generic 4.15.0-13.14 amd64 Linux kernel image for version 4.15.0 on 64

Canonical provides regular updates for Ubuntu 18.04 system including kernel updates to manage the system’s resources as best as possible. This article will provide you some different methods to remove old kernels from the boot menu of your Ubuntu 18.04. When you install a new kernel, the old ones are not deleted because it can help you to boot if you do a mistake with the new one or for some other reason. Notice that the old kernel consume some spaces which can be useful for something else. We sometimes need to update the kernel for compatibilities because it is responsible for interfacing all of the applications.

Check for old kernels

In order to avoid any mistakes with the current boot kernel of your Ubuntu 18.04 system, make sure to check its version

# uname -r
4.15.0-23-generic

Before trying to remove the old kernel, you must first check if there are old kernels presents in the systems. To do that, you can look at the grub when the system startups

How to Speed up an Old PC

To view this video please enable JavaScript, and consider upgrading to a web browser that supports HTML5 video

How to Speed up an Old PC

you can select "advanced option for Ubuntu". You can see the current kernel on the top of the list with the old kernel one just following 4.15.0.13-generic

There is another method to check the old kernel in the command line. This will help you to list the old kernel and not the current one

# dpkg -l | grep -E 'linux-image-[0-9]+' | grep -Fv $(uname -r)
ii  linux-image-4.15.0-13-generic    4.15.0-13.14        amd64      Linux kernel image for version 4.15.0 on 64 bit x86 SMP

You can see that you effectively have an old kernel installed marked with ii. You can see more old kernels if you have done some upgrade or manual installation. There is some status when you check the kernel

  • ii: means that the kernel/packages are installed and eligible for removal.
  • rc: indicates that the kernel has already been removed.
  • iU: is something like a warning tells to DON’T REMOVE. That means not installed but queued for install in apt.

Make sure to see the status before doing anything. Now that we have found some kernels installed, now let's see the different methods to remove each or the totality.

1) Remove old kernels in command line

There are some useful commands to remove the old kernels whether you have installed it manually or directly via the system updates. Normally you install it via the regular system updates but for some reasons, you can decide to do a manual installation.

a) apt command

The apt command help to uninstall packages including the old kernels that you don't need in your Ubuntu 18.04 systems. You can check the ones which were automatically installed as below

# apt-mark showauto 'linux-image-.*'
linux-image-4.15.0-13-generic
linux-image-4.15.0-23-generic
linux-image-extra-4.15.0-13-generic
linux-image-generic

and you can check those that were manually installed as below

# apt-mark showmanual 'linux-image-.*'

As you can see, we don't have any kernel manually installed. You can remove all the old kernel with the remove parameter as below:

# apt remove linux-image-4.15.0-13-generic
Reading package lists... Done
Building dependency tree 
Reading state information... Done
The following packages will be REMOVED:
 linux-image-4.15.0-13-generic* linux-image-extra-4.15.0-13-generic
....
....
you may need to re-run your boot loader[grub]

When you remove a kernel, make sure to update the grub to see if any error occurs while generating the grub

# update-grub
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-4.15.0-23-generic
Found initrd image: /boot/initrd.img-4.15.0-23-generic
Found memtest86+ image: /boot/memtest86+.elf
Found memtest86+ image: /boot/memtest86+.bin
done

Now you can check again if it has been removed

# dpkg -l | grep -E 'linux-image-[0-9]+' | grep -Fv $(uname -r)
rc  linux-image-4.15.0-13-generic    4.15.0-13.14     amd64      Linux kernel image for version 4.15.0 on 64 bit x86 SMP

You can see the status rc which show that it's has been removed. You can check also on the boot menu and see that only the current boot kernel appear

You can also use the apt autoremove command but you should notice that the command is used to remove packages that were automatically installed to satisfy dependencies for other packages and are no longer needed as dependencies. So check if there is not a package that you need present in the list of packages to remove

# apt autoremove --purge
b) dpkg command

You can also use the dpkg command to remove a specific kernel on your Ubuntu 18.04 system. This time you will need to indicate package and the extra dependencies

# dpkg --purge linux-image-4.15.0-13-generic linux-image-extra-4.15.0-13-generic 
(Reading database ... 165680 files and directories currently installed.)
...
...
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-4.15.0-23-generic
Found initrd image: /boot/initrd.img-4.15.0-23-generic
Found memtest86+ image: /boot/memtest86+.elf
Found memtest86+ image: /boot/memtest86+.bin
done
...

You can check again the traces but you will see there are completely removed because you will not have an output showing that it has been removed (rc)

# dpkg -l | grep -E 'linux-image-[0-9]+' | grep -Fv $(uname -r)

You can also look the grub

c) Using a script

We have found an interesting interactive script of Kivisade on Github which helps you to remove the old kernels. It uses the dpkg command but the interesting thing is the fact that it lists the kernels to remove and asks your opinion. First install git

# apt install git

Now clone the project

# git clone https://github.com/kivisade/kernel-purge.git
Cloning into 'kernel-purge'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 3
Unpacking objects: 100% (3/3), done.

enter to the folder and give the permission

# cd kernel-purge && chmod +x kernel-purge.sh

Now run the script

# ./kernel-purge.sh 
Running kernel version is: 4.15.0-23
The following (unused) KERNEL packages will be removed:
linux-headers-4.15.0-13
linux-headers-4.15.0-13-generic
linux-image-4.15.0-13-generic
linux-image-extra-4.15.0-13-generic
Do you want to continue [yN]? y
Reading package lists... Done
Building dependency tree 
Reading state information... Done
The following packages will be REMOVED:
 linux-headers-4.15.0-13* linux-headers-4.15.0-13-generic*
 linux-image-4.15.0-13-generic* linux-image-extra-4.15.0-13-generic*
0 upgraded, 0 newly installed, 4 to remove and 0 not upgraded.
After this operation, 335 MB disk space will be freed.
(Reading database ... 168812 files and directories currently installed.)
...
...
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-4.15.0-23-generic
Found initrd image: /boot/initrd.img-4.15.0-23-generic
Found memtest86+ image: /boot/memtest86+.elf
Found memtest86+ image: /boot/memtest86+.bin
done
...
...

Now you can check if the kernels have been effectively removed. You will not have an output so old kernels have been removed

# dpkg -l | grep -E 'linux-image-[0-9]+' | grep -Fv $(uname -r)
2) Use graphical tools to remove old kernels

You can also use the graphical tool to uninstall the kernel packages. The tool will ask for your password during the process because you need to have the administrative permission.

a) Ubuntu cleaner

Ubuntu Cleaner is a user-friendly system utility designed to clean browser caches, removed unneeded apps, and get shot of old kernels. Ubuntu can be helpful if you want to remove:

  • Application caches which include most major browsers
  • Apt cache
  • Old kernel
  • Unneeded packages

The package is not present by default in the official repository so we need to add the PPA. Remember that on Ubuntu 18.04 you don't need to update the packages cache after adding the PPA because it's automatically launched. So add the PPA as below

~# add-apt-repository ppa:gerardpuig/ppa
 Official Ubuntu Cleaner stable repository
 More info: https://launchpad.net/~gerardpuig/+archive/ubuntu/ppa
Press [ENTER] to continue or Ctrl-c to cancel adding it.

Hit:1 http://cm.archive.ubuntu.com/ubuntu bionic InRelease                    
Get:2 http://cm.archive.ubuntu.com/ubuntu bionic-updates InRelease [83.2 kB]  
Get:3 http://security.ubuntu.com/ubuntu bionic-security InRelease [83.2 kB]   
Get:4 http://cm.archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]
Get:5 http://security.ubuntu.com/ubuntu bionic-security/main amd64 DEP-11 Metadata [204 B]
...
...

Now you can install the tool

# apt install ubuntu-cleaner
Reading package lists... Done
Building dependency tree 
Reading state information... Done
The following additional packages will be installed:
 libpython-stdlib python python-apt python-aptdaemon
...
...

Then launch the software

Then choose the old kernel section and select the kernels to remove

You will be asked for the password and the process will start

Now you can check if the kernels are still present. You can see the status which indicates that it's removed

# dpkg -l | grep -E 'linux-image-[0-9]+' | grep -Fv $(uname -r)
rc  linux-image-4.15.0-13-generic    4.15.0-13.14     amd64      Linux kernel image for version 4.15.0 on 64 bit x86 SMP
b) Synaptic

Synaptic is a graphical interface to the Advanced Packaging Tool (APT) used by Debian based system to handle the installation of the packages/softwares. It isn't a user-friendly tool so it can be very confusing to use, that is why you have to be very careful.

# apt install synaptic
Reading package lists... Done
Building dependency tree 
Reading state information... Done
The following additional packages will be installed:
 docbook-xml libept1.5.0 libgtk2-perl libpango-perl librarian0 rarian-compat
 sgml-base sgml-data xml-core

Now launch the application

Now move to Section -> Kernel and Modules, choose the kernels to remove and mark for a complete removal. Make sure to choose the right kernels on the list

It will ask your confirmation

You can see that it automatically marks the dependency. Apply to remove the kernels

It will ask for the confirmation. Apply

You can see a windows showing the process. If you look carefully, you will see at the end that the grub has been generated and the old kernels are not presents.

Conclusion

Now you know how you can remove the old kernel from the boot of your Ubuntu 18.04. You should notice that it's recommended to delete all the old kernel because if you are not able to boot with your current kernel for whatever reason. So it recommended to keep at least two or preferably three kernels including the latest. The reason for the recommendation is that you will have at least one or two other kernels to boot with.

Read also
  •  How to Use BCC Kernel Tracing Tools for Linux Performance Analysis
  • How to Remove and Add PPA on Ubuntu 18.04
  • 6 Commands to Clear Linux Terminal

Ref From: linoxide
Channels:

Related articles