LVM installs and encrypted installs use a small separate /boot partition. The small partition is capable of holding only four or five kernels, and fills to capacity quickly. To prevent /boot partition from getting full, you need to configure automatic removing of old kernels, or remove old kernels regularly manually, since automatic removing of old kernels is not enabled by default; see Bug #1357093.
Changing the kernel-containing packages on your system requires commands with root access, so please do read RootSudo.
Removing old kernels is easy to do on a properly-operating system. You can do it manually, or set unattended-upgrades to do it automatically. If you are receiving package management errors, regular maintenance may not work until the problem is fixed. See Problems below.
If your system is operating without error, you should be able to remove old kernels with a simple autoremove command in shell:
sudo apt-get autoremove --purge
The system keeps track of which kernels are older and marks them eligible for removal using this method; this also removes other automatically installed packages that (recursively) no manually installed package depends on. However, this works only, if you have installed the kernels automatically, or if you have used Sofware Updater for installing them in Wily or newer release; see Bug 1492709.
Alternatively, and to be able to remove even manually installed kernels, you could use
If the program is not installed, run it without sudo in terminal to find which package you need to install in Ubuntu 14.04 or later; the command works also in 12.04, if you install byobu package from byobu PPA, although you can use the same PPA on later Ubuntu releases as well. Check that the presented set of packages to remove looks fine before ratifying the actual purge.
The unattended-upgrades package, included with the default install of all Ubuntu flavors, includes a setting to run automatic remove automatically. Enabling this setting is a two-step process.
Enable Unattended Upgrades
The first step is to enable unattended-upgrades. You can do it by using the GUI or the shell.
Enable unattended upgrades using Software & Updates --> Updates Tab:
- Check the box for *-security (and/or any other repositories you wish)
- Automatically check for updates: Set to any frequency (except 'Never')
- When there are security updates: Set to Download and Install Automatically
sudo dpkg-reconfigure -plow unattended-upgrades
By default this installs possible security upgrades every day. If you want to change which sort of packages it will upgrade (if any), or how often, see here for details.
Configure Unattended Upgrades to Remove Unneeded Kernels Automatically
Note: The following methods may only remove kernels that are marked as being automatically installed. In Ubuntu 16.04 kernels installed by Software Updater are marked as being automatically installed. In Ubuntu 14.04 only kernels installed by Unattended Upgrades are marked as being automatically installed.
The second step is to edit the configuration file /etc/apt/apt.conf.d/50unattended-upgrades to enable completely automatic remove. It's owned by root, so remember to use sudo!
All Ubuntu Releases
The following setting configures unattended-upgrade to remove excessive automatically installed packages after unattended upgrades.
Locate the line:
Uncomment the line (i.e. remove // from the beginning) AND change the value to "true"
Thereafter system should remove old automatically installed* kernels and packages associated to them - automatically after unattended upgrade. (It does not purge them, however.) It also removes other unneeded packages, as well. You could do the same manually by running
sudo apt-get autoremove
*) Actually, in Ubuntu 16.04 the kernels do not have to be installed automatically, but technically they are technically marked as being automatically installed, if you install them by Software Updater.
Ubuntu 16.04 and Later
Disclaimer: This is a new feature, so it needs some testing.
Unattended-upgrades version 0.90 supports new configuration variable that makes it possible to automatically remove only packages that become excessive during unattended upgrade. It is enabled i.e. "true" by default, so make sure there is NO line
and NO line
in the configuration file, if you want to remove only new unused dependencies automatically after unattended upgrades. This way may not remove as many packages as the previous way.
If your package management is broken, or if regular maintenance above is not working, any of several problems may have occurred. You may be out of storage space, or you may have a package version mismatch, or you may have another problem.
Safely Removing Old Kernels
For users of LVM systems, encrypted systems or limited-storage systems, the most frequent problem is that the /boot partition is simply full. The package manager cannot install a pending upgrade due to lack of space. Besides, apt-get can not remove a package due to broken dependency.
This problem can be fixed quickly and easily from the shell. Simply identify one or two old kernels to remove manually, which will provide the package manager enough space to install the queued upgrade.
$ uname -r ## This command identifies the currently-running kernel 4.2.0-21-generic ## This is the current kernel. ## DO NOT REMOVE it! $ dpkg -l | tail -n +6 | grep -E 'linux-image-[0-9]+' | grep -Fv $(uname -r) ## This command lists all the kernels excluding the booted ## kernel in the package database, and their status. rc linux-image-4.2.0-14-generic ## The oldest kernel in the database ## Status 'rc' means it's already been removed ii linux-image-4.2.0-15-generic ## The oldest installed kernel. Eligible for removal. ## Status 'ii' means Installed. ii linux-image-4.2.0-16-generic ## Another old installed kernel. Eligible for removal ii linux-image-4.2.0-18-generic ## Another old installed kernel. Eligible for removal ii linux-image-4.2.0-19-generic ## The previous good kernel. Keep iU linux-image-4.2.0-22-generic ## DO NOT REMOVE. Status 'iU' means it's not installed, ## but queued for install in apt. ## This is the package we want apt to install. ## Purge the oldest kernel package using dpkg instead of apt. $ sudo dpkg --purge linux-image-4.2.0-15-generic ## If the previous command fails, some installed package ## depends on the kernel. The output of dpkg tells the name ## of the package. Purge it first. ## Also purge the respective header package. $ sudo dpkg --purge linux-headers-4.2.0-15-generic ## Try also purging the common header package. $ sudo dpkg --purge linux-headers-4.2.0-15 ## Do not worry, if the previous command fails. $ sudo apt-get -f install ## Try to fix the broken dependency.
If the last command works without an error, continue to chapter Manual Maintenance to remove more kernels. Otherwise, if the last command still failed due to insufficient disk space in /boot, you have to purge another kernel same way. Many variations on this strategy exist. You can remove *all* the older kernels using dpkg. Some people have even scripted the process. Use a method that makes sense to you *and* to the package manager.
Warning: Do NOT use the 'rm' command to delete files that were placed by the package manager, including kernel files. It merely creates a new headache for you to solve when the package manager cannot remove packages due to 'file not found'. Always use the package manager to remove files that were placed by the package manager.
Unmet Dependency Errors
If you have ignored apt and dpkg errors for a while, then some packages may have upgraded while others did not. This is an expected follow-on effect. You will see mysterious, persistent version errors upon normal upgrades.
The simple way to fix most version mismatch errors is to update the package database, clean out the package cache, and download-and-reinstall the newer version of the offending package.
This is easier than it sounds. For example, if the 'hello' package is one of those unmet dependencies:
$ sudo apt-get update ## Update the package database $ sudo apt-get clean hello ## Delete the hello package from the local cache $ sudo apt-get install --reinstall hello ## Download and reinstall the latest version of hello
Oops, Removed All Kernels!
If you got a bit carried away and deleted all the kernels, you fall into the "You did WHAT?!?!" class. You will be pleased to know that you are not the first, nor will be the last, person to do this. Get your LiveCD and head over to GRUB reports no operating system.
Cryptic Shell Commands
There are various cryptic shell incantations floating around help sites and search engines that promise to remove older kernels. Copy-and-paste mysterious incantations into your system at your own risk.