The accumulation of old kernels until /boot is full, thereby breaking apt, is a bug: LP #1357093, with a fix implemented in all flavors of Ubuntu 16.04 and newer.
Most users who notice this problem installed 'whole-disk encryption', which creates a tiny unencrypted /boot partition. Since it's tiny, the partition fills quickly, and these users notice the problem much sooner than others.
When a new kernel is installed, the /etc/kernel/postinst.d/apt-auto-removal script marks older kernels as eligible for autoremoval...but doesn't run autoremove. The lack of autoremove was originally intended to allow a human to review the list of removed packages.
The bugfix to LP #1357093 works for most users - if it doesn't work for you, then there is another underlying cause preventing old kernel packages from being apt-marked as eligible for autoremove. Seek one-on-one help in the Ubuntu support channels.
In older versions of Ubuntu, you must fix weed old kernels to preserve space manually. How you fix it manually depends upon whether your /boot is full or not. Most users don't notice the problem until /boot is full, and they are getting apt and dpkg no-space-left-on-device errors.
If /boot is not full, and apt works properly, a simple
sudo apt-get autoremove ## Ubuntu 14.04 and older
sudo apt autoremove ## Ubuntu 16.04 and newer
should remove all kernel packages that are eligible for autoremoval.
If /boot is full, and apt actions fail with the dpkg error 'no space left on device', then it is too late to use autoremove.
It's too late because Aptdaemon queues package actions. Autoremove is at the back of the queue, and apt aborts the entire remaining queue when any action fails...including running out of space. (This is arguably a bug in apt/aptdaemon)
The best practice here is to use 'uname' and 'dpkg' to remove one or two old kernels, freeing space for apt to complete it's queued actions. Then autoremove will work.
Example:
$ uname -r
3.16.0-36-generic ## This is kernel you MUST NOT remove.
$ dpkg -l | grep linux-image
rc linux-image-3.16.0-23-generic ## 'rc' means already removed
rc linux-image-3.16.0-28-generic ## 'rc' can be safely ignored
rc linux-image-3.16.0-29-generic
ii linux-image-3.16.0-30-generic ## 'ii' means installed. Removable
ii linux-image-3.16.0-31-generic ## Removable
ii linux-image-3.16.0-33-generic ## Removable
ii linux-image-3.16.0-34-generic ## Backup working kernel. Don't remove
ii linux-image-3.16.0-36-generic ## Current kernel. DO NOT REMOVE
## Use dpkg to remove one older kernel, freeing enough space for apt to work
$ sudo dpkg --remove linux-image-3.16.0-30-generic
$ sudo apt-get autoremove ## Ubuntu 14.04 and older
$ sudo apt autoremove ## Ubuntu 16.04 and newer
If you have been ignoring the problem for a long time, then there are second-order effects, like linux-image-generic pointing to the wrong kernel version, and apt dependency errors. There is no single way of fixing all of these at once.
Generally, the easy way to clean up most of these problems is to clean the old packages out of your local package cache, update your package database, and reinstall the offending packages from the Ubuntu repositories (instead of your local cache).
$ sudo apt-get update ## Refresh the package database (14.04 and older)
$ sudo apt update ## (16.04 and newer)
$ sudo apt-get autoclean ## Delete the obsolete packages from your local cache (14.04 and older)
$ sudo apt autoclean ## (16.04 and newer)
$ sudo apt-get install --reinstall <packagename> ## Reinstall the offending package with the latest version in the Ubuntu repositories (14.04 and older)
$ sudo apt install --reinstall <packagename> ## (16.04 and newer)
If you still encounter apt and/or dpkg errors, seek one-on-one help in the Ubuntu support channels.
I know this post is a bit old, but I found an answer here for anybody that may stumble upon this post: https://help.ubuntu.com/community/RemoveOldKernels
In case that link is broken, here is the relevant snippet:
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.
$ sudo rm -rv ${TMPDIR:-/var/tmp}/mkinitramfs-*
## In Ubuntu 16.04 and earlier there may be leftover temporary
## files to delete.
## See: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=814345
$ 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.
## First you need to remove the image initrd.img file manually
## due to Bug #1678187.
$ sudo update-initramfs -d -k 4.2.0-15-generic
$ sudo dpkg --purge linux-image-4.2.0-15-generic linux-image-extra-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.
I followed this with:
sudo apt-get autoremove --purge
Best Answer
Try this.
Open a terminal and enter:
You will see something like this:
The output may (and will) differ according to your set up.
Take a close look at the folders named
linux-headers-*
. These are the headers from all your linux images that you have installed. If you have too many of them, they will take up A LOT of space and inodes. You only need to keep the latest one you boot from.Let's say you have an older kernel, eg. 3.2.0-53. To remove it, type:
for each one of them.
If all of the above fail, then try to delete the OLDER KERNEL headers' folders manually: