Unable to install VIrtualbox, `"Specify KERN_DIR=<directory>"` -- installing Virtual Box to Debian?

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?


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


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:

  • Path to kernel header
  • How to install Oracle VM VirtualBox in Fedora 20

@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.