39

I am getting this error:

Makefile:181: *** Error: unable to find the sources of your current Linux kernel. Specify KERN_DIR= and run Make again. Stop.

Basically I'm doing:

$ sudo /etc/init.d/vboxdrv setup
Removing existing VirtualBox non-DKMS kernel modules       [  OK  ]
Building the VirtualBox Guest Additions kernel modules
The headers for the current running kernel were not found. If the following
module compilation fails then this could be the reason.
The missing package can be probably installed with
yum install kernel-devel-2.6.32-573.18.1.el6.x86_64

Building the main Guest Additions module                   [FAILED]
(Look at /var/log/vboxadd-install.log to find out what went wrong)

And the error is:

Makefile:181: *** Error: unable to find the sources of your current Linux kernel. Specify KERN_DIR= and run Make again. Stop

What is this error and how to fix it?

7 Answers 7

35
+50

Make sure you install Kernel Headers Package.

http://www.cyberciti.biz/faq/howto-install-kernel-headers-package

Make sure you have updated version

Type the following command
$ sudo apt-get update

OR as root user enter:
# apt-get update

Search for kernel version (optional)

Type the following command:
$ apt-cache search linux-headers-$(uname -r)

Install linux-header package under Debina or Ubuntu Linux

Type the following command:
$ sudo apt-get install linux-headers-$(uname -r)

OR as root user:
# apt-get install linux-headers-$(uname -r)

EDIT:

I just happen to need to install it also on my own server and got the same error when trying to use the virtualbox in debian repos. I went ahead and added VirtualBox repo and installed from there. This is better because they are more up-to-date.

As root

$ nano /etc/apt/sources.list 

Add line below for squeeze

deb http://download.virtualbox.org/virtualbox/debian squeeze contrib non-free

Then add the The Oracle public key for apt-secure

$ wget -q http://download.virtualbox.org/virtualbox/debian/oracle_vbox.asc -O- | sudo apt-key add -

then run update and to install latest 4.2

apt-get update
apt-get install virtualbox-4.2

Install the dkms package to ensure that the VirtualBox host kernel modules are up to date

apt-get install dkms

Should be good to go. Got these instructions from VirtualBox's website for Linux which seems to be down at the moment but the link is https://www.virtualbox.org/wiki/Linux_Downloads

4
  • 2
    The command apt-cache search linux-headers-$(uname -r) returns nothing for me even though I did the above commands.
    – hhh
    Nov 3, 2012 at 22:12
  • @hhh I think this answer is good one, and should be accepted. I do not know what you want with this **linux-headers-$(uname -r) **. Please, explain or open a new question. I think this thread is finished. Nov 20, 2013 at 15:37
  • @hhh Some distributions don't have the repositories of the linux-header. you can check this one: computingforgeeks.com/2015/08/…
    – Evan
    Jan 7, 2016 at 8:39
  • None of these answers worked for me because the distro I downloaded had an older kernel than that available in repos. I had to run apt-get update && apt-get -y dist-upgrade.
    – Plato
    Apr 4, 2017 at 4:56
30

How to fix this for Fedora/CentOS

First make sure that your installed packages (especially kernel) are up to date:

$ sudo yum update     

If it updates the kernel version, you will have to restart so uname -r matches the directory name in /usr/src/kernels/. Then

$ sudo yum install kernel-devel gcc
$ echo export KERN_DIR=/usr/src/kernels/`uname -r` >> ~/.bashrc
$ source ~/.bashrc     # to set the variable in your current shell
$ sudo echo $KERN_DIR  # verify the value is set
$ sudo ls $KERN_DIR    # verify the directory exists 

After this compiling VirtualBox kernel modules should works without any problems.

Sources:

6
  • 1
    After running above steps, had to restart guest system before reinstalling vbox guest additions again.
    – Reddy
    Jun 2, 2015 at 11:14
  • This was the right package to install, however recompiling the kernel modules required root privileges, and running something in root will lose your environment variables (including KERN_DIR). I instead did sudo -s followed by export KERN_DIR=/usr/src/kernels/XXX but I'm sure there's a better way to persist variables into sudo mode
    – Migwell
    Jan 8, 2016 at 0:14
  • In my case (CentOS 6.7) [uname -r] is [2.6.32-573.el6.x86_64], but [ls /usr/src/kernels/] prints [2.6.32-573.22.1.el6.x86_64] So be careful and use actual existing dir name
    – jnr
    Apr 8, 2016 at 13:09
  • 1
    source ~/.bashrc to set KERN_DIR in the current environment without having to restart.
    – Justin
    Aug 16, 2016 at 14:16
  • I got errors that its not a directory. Note that it needs a slash at the end.. echo export KERN_DIR=/usr/src/kernels/uname -r/ >> ~/.bashrc Sep 30, 2016 at 5:41
2

@hhh You should search only for linux-headers and find the good one for your architecture. In my case:

# aptitude search linux-headers      
v   linux-headers                                                       -                                                                              
p   linux-headers-2.6-amd64                                             - Header files for Linux amd64 configuration (dummy package)                   
p   linux-headers-3.2.0-4-all                                           - All header files for Linux 3.2 (meta-package)                                
p   linux-headers-3.2.0-4-all-amd64                                     - All header files for Linux 3.2 (meta-package)                                
i A linux-headers-3.2.0-4-amd64                                         - Header files for Linux 3.2.0-4-amd64                                         
i A linux-headers-3.2.0-4-common                                        - Common header files for Linux 3.2.0-4                                        
p   linux-headers-3.2.0-4-common-rt                                     - Common header files for Linux 3.2.0-4-rt                                     
p   linux-headers-3.2.0-4-rt-amd64                                      - Header files for Linux 3.2.0-4-rt-amd64                                      
i   linux-headers-amd64                                                 - Header files for Linux amd64 configuration (meta-package)                    
p   linux-headers-rt-amd64                                              - Header files for Linux rt-amd64 configuration (meta-package)

So, in my case, I have an 64-bit architecture, I have to install linux-headers-amd64.

Another thing is apt-get add does not exist. Replace apt-get by apt-key.

2

I had the KERN_DIR error in CentOS. Reviewed the shell install script, and found that it sets KERN_DIR to /usr/src/kernels. Then did "uname -r" shows I was running a lower version of the kernel ... than the source code under /usr/src/kernels.

I looked at /boot/grub/menu.lst ... and confirmed the kernel mismatch. Weird.

So fix was ... "yum -y update kernel", then reboot. VMWare tools then got installed.

1

Try:

yum install kernel* dkms gcc

Then

yum install kernel-uek-devel

Then reboot your machine

1
  • Debian uses apt-get, not yum.
    – RobinJ
    Jun 9, 2014 at 13:53
0

The error means that vboxdrv/vboxadd can't find your kernel sources.

The script expects that /lib/modules/$(uname -r)/build folder points to /usr/src/kernels/$(uname -r) which should have the Makefile where it can execute kernelrelease task, so it can compare against your current installed kernel version (uname -r).

So if this command fails:

make -C /usr/src/kernels/$(uname -r)/build kernelrelease

you should install the Linux headers by:

sudo apt-get install linux-headers-$(uname -r)

For further details, please check this post.

0

For any OS you need to make sure all needed packages are installed. In my case on Debian I was missing packages dkms and linux-headers-generic.

#apt-get install dkms build-essential linux-headers-generic linux-headers-$(uname -r)

For full instructions see this VirtualBox forum post.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .