86

I'm running Ubuntu 12.04, and two active linux images 3.2.0-37 and 3.2.0-36. In the /usr/src/ directory i find:

linux-headers-3.2.0-23    
linux-headers-3.2.0-23-generic    
linux-headers-3.2.0-26    
linux-headers-3.2.0-26-generic
linux-headers-3.2.0-29    
linux-headers-3.2.0-29-generic
linux-headers-3.2.0-31    
linux-headers-3.2.0-31-generic    
linux-headers-3.2.0-32    
linux-headers-3.2.0-32-generic    
linux-headers-3.2.0-33    
linux-headers-3.2.0-33-generic    
linux-headers-3.2.0-34    
linux-headers-3.2.0-34-generic    
linux-headers-3.2.0-35    
linux-headers-3.2.0-35-generic    
linux-headers-3.2.0-36    
linux-headers-3.2.0-36-generic     
linux-headers-3.2.0-37     
linux-headers-3.2.0-37-generic

Today I sudo apt-get purge linux-image-x.x.x.x-generic *34 and *35

I have to add that /boot contains only *36 and *37.

Is it safe to remove all of these except *37 and *36 ? Thank you in advance.

Paul van Schayck
  • 1,564
  • 1
  • 12
  • 16
SSO Master
  • 863
  • 1
  • 6
  • 5
  • https://stackoverflow.com/questions/60248189/linux-headers-are-consuming-a-lot-of-disk-space-on-the-ec2-machine-is-it-safe-t – Deepak Oct 06 '20 at 07:27

4 Answers4

108

Those are header files are contained in the linux-headers-* and linux-headers-*-generic packages. It should be safe to remove them through apt-get. Maybe apt-get autoremove will already suggest that to you. Please do not remove them manually!

Paul van Schayck
  • 1,564
  • 1
  • 12
  • 16
  • 7
    `apt-get autoremove` did not remove the header files for me – user12345 Oct 17 '14 at 23:51
  • 11
    Except sometimes `apt-get autoremove` can't run because of an `out of disk space` error, due in part to `/usr/src/linux*` taking up 2G of space on a small drive. – Mark Stosberg Feb 22 '16 at 21:12
  • 4
    Yap. Sometimes it is not the disk space, but no more inodes being left -> if `df -i` shows 100% usage, you are de facto left with no space whatsoever. Only solution is then to delete _some_ /usr/src/linux* stuff, to get apt to work again. – Christian Ulbrich Apr 06 '17 at 00:48
  • 3
    Try `sudo apt autoremove` rather than apt-get. – jaybrau Jun 12 '18 at 14:22
  • @jaybrau Does not work either. – Andreï V. Kostyrka Jul 08 '18 at 14:53
  • 10
    FWIW, `apt-get autoremove` worked for me -- freed 3.3GB of space on my 8GB AWS instance. Thank you! – Jeff McMahan Aug 16 '18 at 02:11
  • 1
    In my case, `apt-get autoremove` did not work initially. I had to run `sudo apt-get -f install` in order for it to work. That command also failed initially, as it needs some free inodes. I found a folder that I could safely delete. Then, I was able to run the above 2 commands, which brought my inode usage from 99% down to 33%. – Daniel Waltrip Jan 21 '19 at 17:56
  • 1
    If I don't want to compile ANYTHING (not device drivers, not kernel, not my silly C code), can I manually PURGE EM ALL to save space in my / 2GB SD Card? – Chameleon Apr 05 '20 at 12:13
  • If none of these work, then login to root directly first to run them -- `sudo -i`. That can give you access to some drive space reserved for root that `sudo` doesn't have access to. – Barrett Conrad Aug 16 '21 at 19:40
18

I just had this issue, and the suggestion to use apt-get autoremove did not work for me.

I resolved it by doing:

sudo apt-get purge linux-headers-3.2.0-23

on such linux headers that lived in /usr/src. I did not remove the headers that correspond to the kernel currently being used.

Restarted the server successfully.

user12345
  • 4,371
  • 1
  • 19
  • 9
  • 6
    A oneliner to automate this: https://ubuntugenius.wordpress.com/2011/01/08/ubuntu-cleanup-how-to-remove-all-unused-linux-kernel-headers-images-and-modules/ Works like a charm. (I had to modify it a little for my situation.) – Reinier Post Sep 29 '15 at 15:18
1

This is very similar, but more readable, than the helpful comment from Reinier Post in user12345's answer.

apt list --installed linux-*5* | grep -v $(uname -r) | xargs sudo apt -y purge

It missed one dev header package without the standard numbering, but also didnt pickup the linux-sound-base for Alsa, which might be desired. You can compare both by removing the xargs pipe, like this dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/(.*)-([^0-9]+)/1/")"'/d;s/^[^ ]* [^ ]* ([^ ]*).*/1/;/[0-9]/!d'

alchemy
  • 744
  • 1
  • 9
  • 27
1

To anyone coming in off a Google search years later: You can also uninstall old kernels in the Synaptic Package Manager. Just double check which one you're currently using by typing 'uname -a' into the terminal.

Jeremy Kerr
  • 26,769
  • 4
  • 48
  • 62
  • 4
    Your answer should explain how to do this, specifically. – Elijah Lynn Mar 03 '17 at 20:08
  • If I'm already at the command line issuing a command to determine which kernel version I'm using, I may as well issue the very simple `apt-get autoremove`. – ephsmith Aug 28 '20 at 01:30