ubuntu orange logoI ran into an issue the other day installing VMWare Tools on a virtual server whereby the installer was moaning about not having enough space in the /boot directory in order to continue with the installation.

(Now of course, unless you’re a seasoned Linux pro, you don’t really want to be messing about with your /boot folder, even if you’re confident that after running uname -r you’re kind of 100% sure which kernel files you can safely remove! In other words, tread carefully and always have backups on hand.)

Thankfully there are some pretty useful commands that you can run in order to help you free up some much needed space, commands that remove unused kernels, left over dependencies, and just plain old cached .deb files!

To remove old kernels, inspect your /boot folder. If you run uname -r you’ll see your currently used kernel version. This is the one you want to keep, so make a note. (Oh, and it’s also usually a good idea to hang on one older one for just in case.)

The Ubuntu Forums offered up this particularly handy little command to list all the kernels and headers that can be removed, excluding the current running kernel:

dpkg -l linux-{image,headers}-"[0-9]*" | awk '/ii/{print $2}' | grep -ve "$(uname -r | sed -r 's/-[a-z]+//')"

Now you can remove unneeded kernels one by one using the apt-get purge command (e.g. sudo apt-get purge linux-image-x.x.x-x), or you can make use of the above snippet to blast everything away at once:

sudo apt-get purge $(dpkg -l linux-{image,headers}-"[0-9]*" | awk '/ii/{print $2}' | grep -ve "$(uname -r | sed -r 's/-[a-z]+//')")

(Risky, I know, but it is a time saver if you have a lot of unused kernels lying around)

Next up in terms of clearing up some space on your Ubuntu install is making use of the sudo apt-get commands ‘autoclean’, ‘clean’ and ‘autoremove’.

From the apt-get MAN pages:

clean: clean clears out the local repository of retrieved package files. It removes everything but the lock file from /var/cache/apt/archives/ and /var/cache/apt/archives/partial/.

autoclean: Like clean, autoclean clears out the local repository of retrieved package files. The difference is that it only removes package files that can no longer be downloaded, and are largely useless. This allows a cache to be maintained over a long period without it growing out of control.

autoremove: is used to remove packages that were automatically installed to satisfy dependencies for some package and that are no longer needed.

First, run the autoclean package command that essentially removes partial package from the system, followed by the clean command that removes .deb packages that apt caches when you install, update, or upgrade:

sudo apt-get autoclean
sudo apt-get clean

Now run the autoremove command to remove any packages that should technically no longer be needed:

sudo apt-get autoremove

There, that should have freed up some space for you!

(Reminder to myself: I tend to Google this to get here: ubuntu server /dev/sda1 full)