How to install kernel headers on Linux

Question: I need to install kernel headers to install a device driver for my kernel. What is a proper way to install matching kernel headers on [insert your Linux distro]?

When you compile a device driver a custom kernel module, you need to have kernel header files installed on your Linux system. Kernel headers are needed also when you build a userspace application which links directly against the kernel. When you install kernel headers in such cases, you must make sure to kernel headers are exactly matched with the kernel version of your system (e.g., 3.13.0-24-generic).

If your kernel is the default version that comes with the distribution, or you upgraded it using the default package manager (e.g., apt-get, aptitude or yum) from base repositories, you can install matching kernel headers using the package manager as well. On the other hand, if you downloaded the kernel source and compiled it manually, you can install matching kernel headers by using make command.

Here we assume that your kernel comes from base repositories of your Linux distribution, and see how we can install matching kernel headers.

Install Kernel Headers on Debian, Ubuntu or Linux Mint

Assuming that you did not manually compile the kernel, you can install matching kernel headers using apt-get command.

First, check if matching kernel headers are already available on your system using dpkg-query command.

$ dpkg-query -s linux-headers-$(uname -r)
dpkg-query: package 'linux-headers-3.11.0-26-generic' is not installed and no information is available

Go ahead and install matching kernel headers as follows.

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

Verify that matching kernel headers are successfully installed.

$ dpkg-query -s linux-headers-$(uname -r)
Package: linux-headers-3.11.0-26-generic
Status: install ok installed

The default location of kernel headers on Debian, Ubuntu or Linux Mint is /usr/src.

Install Kernel Headers on Fedora, CentOS or RHEL

If you did not manually upgrade the kernel, you can install matching kernel headers using yum command.

First, check if matching kernel headers are already installed on your system. The default location of kernel header files is /usr/src/kernels/.

If no matching kernel headers are found in that directory, go ahead and install kernel headers with yum command as follows.

$ sudo yum install kernel-devel

If the kernel header files installed by the above command do not match with your current kernel, this is because your kernel is outdated. In this case, update the installed kernel to the latest by running the following command. This will install the matching kernel automatically. You will need to reboot after updating the kernel.

$ sudo yum install kernel

Now verify that matching kernel headers are installed by running:

$ ls /usr/src/kernels/$(uname -r)
arch     firmware  ipc      Makefile        samples
block    fs        Kconfig  mm              scripts   tools
crypto   include   kernel   Module.symvers  security  usr
drivers  init      lib      net             sound     virt
Download this article as ad-free PDF (made possible by your kind donation): 
Download PDF

Subscribe to Ask Xmodulo

Do you want to receive Linux related questions & answers published at Ask Xmodulo? Enter your email address below, and we will deliver our Linux Q&A straight to your email box, for free. Delivery powered by Google Feedburner.

Support Xmodulo

Did you find this tutorial helpful? Then please be generous and support Xmodulo!

2 thoughts on “How to install kernel headers on Linux

  1. Unfortunately does not work for me your proposal.
    I use Ubuntu, kernel-generic 3.11.0-26 for ubuntu "14_04"
    Here the screenshot:

    denes@ubuntu:~$ uname -r
    denes@ubuntu:~$ sudo apt-get install linux-headers-$(uname -r)
    Reading package lists ... Done Building
    Dependency tree.
    Status information can be read .... Done
    Package linux-headers-generic-3.11.0-26 is not available, but by another package
    referenced. This may mean that the package is missing, that it replaced
    was or is only available from a different source.

    Best regards

Leave a comment

Your email address will not be published. Required fields are marked *