To install specific Linux kernel headers, you must match the exact version string of your currently running kernel. Use uname -r to identify this version, then install the corresponding package via your distribution's package manager (such as apt on Debian-based systems or dnf on RHEL-based systems). This process ensures that external modules, like proprietary drivers or DKMS-built software, compile correctly against the active kernel source.
Identifying Your Current Kernel Version
The most critical step in installing kernel headers is ensuring the version matches your running system exactly. If you install headers for a different version, compilation will fail because the internal structures and macros will not align with the loaded kernel module. To find your precise version string, open a terminal and execute the following command:uname -r
This command returns output similar to 5.15.0-46-generic or 6.1.0-9-amd64. You need every part of this string, including the architecture suffix if present. For example, on Ubuntu systems, the generic flavor often includes -generic, while server kernels might use -server. On Debian, you might see -amd64. Copy this entire string, as it will be used to construct the package name or search query for your installer.
It is important to note that simply knowing the major version number (like 5.15 or 6.1) is insufficient. Package managers require the full revision string to locate the correct binary. If you have recently updated your kernel but are still logged into an old session, uname -r might show the older version until you reboot. Always verify the version after a reboot if you suspect a mismatch.
Installing Headers on Debian-Based Systems
For users of Ubuntu, Debian, Linux Mint, and Kali Linux, the Advanced Package Tool (apt) is the standard method for managing kernel components. The package naming convention usually follows the pattern linux-headers-version.
First, update your local package index to ensure you can access the latest available versions:
sudo apt update
Next, install the headers using the version string identified earlier. Replace <version> with your actual output from uname -r:
sudo apt install linux-headers-<version>
If you are unsure of the exact package name, you can search for it using apt-cache search. For instance:
apt-cache search linux-headers | grep <version>
This helps confirm the package exists before attempting installation. In some cases, particularly on Kali Linux, users may encounter errors related to dpkg if the package database is corrupted. If you see "E: Sub-process /usr/bin/dpkg returned an error code," you should first run sudo dpkg --configure -a to fix broken configurations before retrying the header installation. Refer to our guide on fixing dpkg errors if you face persistent package management issues.
After installation, verify that the files exist by checking the directory /lib/modules/<version>/build. This symbolic link points to the extracted source tree required for building out-of-tree modules.
Installing Headers on RHEL-Based Systems
Red Hat Enterprise Linux, Fedora, CentOS, and Rocky Linux use the DNF or YUM package managers. Note that modern distributions have largely transitioned fromyum to dnf, though yum often remains a symlink for backward compatibility. It is best practice to use dnf directly to avoid deprecation warnings.
The package naming convention here typically involves kernel-devel and sometimes kernel-headers. The kernel-devel package contains the full development environment needed to compile modules, while kernel-headers provides only the C header files for user-space programs. For most driver installations, you need kernel-devel.
Install the matching development packages with:
sudo dnf install kernel-devel-<version> kernel-headers-<version>
If you do not know the exact version, you can list available kernel packages:
dnf list available kernel-devel* | grep <version>
Ensure that the kernel-devel version matches the kernel version exactly. Mismatches here are a common cause of build failures for VirtualBox guest additions or NVIDIA drivers. If you previously attempted an installation that failed due to dependency conflicts, cleaning the cache with sudo dnf clean all and retrying may resolve the issue.
Verifying the Installation
Regardless of the distribution, verification is essential before proceeding with driver compilation. Navigate to the modules directory for your specific kernel version:cd /lib/modules/$(uname -r)/build
If the directory exists and contains files such as Makefile and Kbuild, the installation was successful. If you receive a "No such file or directory" error, the headers were not installed correctly, or the version string used during installation did not match the running kernel. Double-check your uname -r output and repeat the installation steps.
Properly installed headers allow tools like DKMS (Dynamic Kernel Module Support) to automatically rebuild modules when the kernel is updated. This is crucial for maintaining functionality of third-party drivers across system updates. For more information on managing kernel tasks and isolated cores, you may find our article on Ubuntu kernel task isolation helpful.
Frequently Asked Questions
Do I need to reboot after installing kernel headers?
No, a reboot is not required immediately after installation. However, you must be running the kernel version whose headers you just installed. If you installed headers for a new kernel version but are still booted into the old one, the build process will fail because the paths will not match.
What is the difference between kernel-headers and kernel-devel?
kernel-headers contains only the C header files necessary for compiling user-space applications that interact with the kernel. kernel-devel includes the headers plus the Makefiles and configuration scripts required to compile out-of-tree kernel modules, such as graphics drivers or network adapters. You generally need kernel-devel.
Why does my DKMS build fail even with headers installed?
This usually indicates a version mismatch between the running kernel and the installed headers. Verify that uname -r matches the version string of the installed linux-headers or kernel-devel package exactly. Even a minor patch difference (e.g., 5.15.0-46 vs 5.15.0-47) will cause compilation to fail.
Can I install headers for multiple kernel versions simultaneously?
Yes, you can install headers for any number of kernel versions. This is useful if you dual-boot or keep fallback kernels. However, only the headers for the currently running kernel (as shown by uname -r) will be actively used by DKMS or manual builds unless you specify otherwise.
How do I remove kernel headers?
You can remove them using your package manager. For Debian/Ubuntu, use sudo apt purge linux-headers-<version>. For RHEL/Fedora, use sudo dnf remove kernel-devel-<version>. Removing unused headers frees up disk space but does not affect the running system.