How to Install Linux Kernel Headers on Ubuntu (26.04, 24.04, 22.04)

Last updated Sunday, March 1, 2026 11:37 am 7 min read

Linux kernel headers provide the interface files that userspace tools and driver build systems rely on when they need to compile against your current kernel. To install Linux kernel headers on Ubuntu, you typically install the header package that matches your running kernel version so DKMS modules and third-party drivers can build without version mismatch errors.

This walkthrough keeps the workflow version-agnostic for Ubuntu and focuses on what matters when kernels move forward after updates. You will install matching headers, verify files under /usr/src, troubleshoot common package and DKMS failures, keep headers in sync, and remove them cleanly if local module builds are no longer needed.

These steps cover Ubuntu 26.04 LTS, 24.04 LTS, and 22.04 LTS. Commands are the same across supported LTS releases unless you use a custom kernel outside Ubuntu’s default repositories.

Install Linux Kernel Headers on Ubuntu

Choose the package strategy that matches your workflow before you run the install commands:

Package OptionWhen to Use ItTracks Future Kernel UpdatesCommand
linux-headers-$(uname -r)You need headers for the kernel currently running nowNo, single running-kernel targetsudo apt install linux-headers-$(uname -r)
linux-headers-genericYou follow the default GA kernel trackYes, via GA metapackagesudo apt install linux-headers-generic
linux-headers-generic-hwe-$RELEASEYou use the HWE kernel trackYes, via HWE metapackagesudo apt install linux-headers-generic-hwe-$RELEASE

Start by refreshing package metadata and applying pending upgrades. Kernel headers should track your kernel packages, so updating first helps prevent avoidable mismatch issues.

sudo apt update && sudo apt upgrade

This guide uses sudo for commands that need root privileges. If your user is not in the sudoers file yet, run the commands as root or follow the guide on how to add and manage sudo users on Ubuntu.

If the upgrade installs a newer kernel, reboot before installing headers so uname -r reflects the kernel you actually plan to run:

sudo reboot

Install Headers for Your Running Ubuntu Kernel

Install the exact header package for the active kernel with $(uname -r) substitution:

sudo apt install linux-headers-$(uname -r)
Reading package lists... Done
Building dependency tree... Done
linux-headers-6.x.x-xx-generic is already the newest version (6.x.x-xx.xx).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

For driver compilation workflows such as NVIDIA drivers on Ubuntu or VirtualBox on Ubuntu, install build tools and DKMS alongside the headers:

sudo apt install linux-headers-$(uname -r) build-essential dkms

Install Ubuntu Header Metapackages for Ongoing Updates

If you want header packages to track Ubuntu kernel updates automatically through metapackages, use one of these commands based on your kernel track:

sudo apt install linux-headers-generic

On systems using the HWE kernel track, install the matching HWE metapackage for the current release:

RELEASE="$(. /etc/os-release && echo "$VERSION_ID")"
sudo apt install linux-headers-generic-hwe-$RELEASE

If this package is unavailable, continue using linux-headers-generic unless you explicitly switched to an HWE kernel policy.

If you are not sure whether you use GA or HWE, check your kernel policy first, then choose the matching metapackage. If needed, follow the HWE kernel guide for Ubuntu before installing HWE headers.

Verify Linux Kernel Headers on Ubuntu

Confirm that the installed header package matches the running kernel:

uname -r
dpkg -l | grep linux-headers-$(uname -r)

Expected output shows your active kernel and an installed header package line beginning with ii:

6.x.x-xx-generic
ii  linux-headers-6.x.x-xx-generic  6.x.x-xx.xx  amd64  Linux kernel headers for version 6.x.x

Verify the header directory exists under /usr/src/:

ls -d /usr/src/linux-headers-$(uname -r)
/usr/src/linux-headers-6.x.x-xx-generic

If you installed DKMS tooling, verify registered modules:

dkms status
virtualbox/7.x.x, 6.x.x-xx-generic, x86_64: installed

Understand Linux Kernel Headers and DKMS on Ubuntu

These packages are related but solve different problems. Knowing which one you need helps you avoid unnecessary installs.

linux-headers-$(uname -r)

  • Provides header files matching your running kernel build
  • Required for most module compilation workflows that target the active kernel
  • Installs header trees under /usr/src/linux-headers-<kernel-version>

linux-headers-generic or linux-headers-generic-hwe-*

  • Metapackages that pull in matching headers as kernel tracks advance
  • Useful when you want ongoing header updates without manually targeting each version
  • Best for long-term workstation and driver-maintenance workflows

dkms and build-essential

  • dkms rebuilds registered modules when kernel packages change
  • build-essential installs the compiler and toolchain needed to build modules
  • Together with matching headers, they cover most third-party driver compile needs

Troubleshoot Linux Kernel Headers on Ubuntu

Fix Unable to Locate Package linux-headers-$(uname -r) on Ubuntu

If installation fails with a package lookup error, your package metadata may be stale or your running kernel may not come from Ubuntu’s standard repositories.

E: Unable to locate package linux-headers-6.x.x-xx-generic

Diagnose the active kernel and whether APT can see a matching header package:

uname -r
apt-cache policy linux-headers-$(uname -r)
linux-headers-6.x.x-xx-generic:
  Installed: (none)
  Candidate: (none)

Refresh package indexes and retry installation:

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

Verify the package now exists:

dpkg -l | grep linux-headers-$(uname -r)
ii  linux-headers-6.x.x-xx-generic  6.x.x-xx.xx  amd64  Linux kernel headers for version 6.x.x

Fix DKMS Module Build Failures After Installing Ubuntu Headers

When modules fail to compile, the common cause is missing build dependencies or a header mismatch against the active kernel.

Error! Your kernel headers for kernel 6.x.x-xx-generic cannot be found.

Run a quick diagnostic:

uname -r
dpkg -l | grep linux-headers-$(uname -r)
dpkg -l | grep -E 'build-essential|dkms'
6.x.x-xx-generic
ii  linux-headers-6.x.x-xx-generic  6.x.x-xx.xx  amd64  Linux kernel headers for version 6.x.x
ii  build-essential                 12.xx         amd64  Informational list of build-essential packages
ii  dkms                            3.x.x         all    Dynamic Kernel Module Support Framework

Install missing components and trigger DKMS rebuild:

sudo apt install linux-headers-$(uname -r) build-essential dkms
sudo dkms autoinstall

Verify module status after rebuild:

dkms status
virtualbox/7.x.x, 6.x.x-xx-generic, x86_64: installed

Update Linux Kernel Headers on Ubuntu

Kernel headers update through normal APT maintenance. Run a full upgrade, reboot if a new kernel installs, then verify the active kernel and header package still match. If you want a broader maintenance refresher, review updating packages via Ubuntu command line.

sudo apt update && sudo apt upgrade
sudo reboot

After reboot, verify alignment:

uname -r
dpkg -l | grep linux-headers-$(uname -r)
6.x.x-yy-generic
ii  linux-headers-6.x.x-yy-generic  6.x.x-yy.yy  amd64  Linux kernel headers for version 6.x.x

On systems with pending kernel upgrades, reboot can advance your active kernel version, for example from 6.8.0-100-generic to 6.8.0-101-generic. When that happens, rerun header verification against the new uname -r result before rebuilding modules.

Remove Linux Kernel Headers from Ubuntu

If you no longer build kernel modules locally, remove the header packages and autoremove unused dependencies. For broader package cleanup behavior, see removing packages on Ubuntu command line.

On HWE systems, removing linux-headers-$(uname -r) can also remove metapackages such as linux-headers-generic-hwe-* and linux-generic-hwe-*. If you rely on automatic kernel/header tracking, reinstall the matching metapackages afterward.

sudo apt remove linux-headers-$(uname -r)
sudo apt autoremove
The following packages will be REMOVED:
  linux-headers-6.x.x-yy-generic
After this operation, XX.X MB disk space will be freed.

If you installed metapackages for automatic header tracking, remove those as well:

# GA metapackage
sudo apt remove linux-headers-generic linux-generic

# HWE metapackage (if present)
RELEASE="$(. /etc/os-release && echo "$VERSION_ID")"
dpkg -l | grep "linux-headers-generic-hwe-$RELEASE"
sudo apt remove linux-headers-generic-hwe-$RELEASE linux-generic-hwe-$RELEASE

sudo apt autoremove

To restore automatic header tracking after removal, reinstall the metapackage that matches your kernel policy:

# GA tracking
sudo apt install linux-headers-generic linux-generic

# HWE tracking
RELEASE="$(. /etc/os-release && echo "$VERSION_ID")"
sudo apt install linux-headers-generic-hwe-$RELEASE linux-generic-hwe-$RELEASE

Verify removal state:

dpkg -l | grep linux-headers-$(uname -r) || echo "Kernel headers for running kernel removed"
Kernel headers for running kernel removed

Removing kernel headers does not remove your kernel image, but DKMS modules and third-party driver builds will fail until matching headers are installed again.

Frequently Asked Questions About Linux Kernel Headers on Ubuntu

Do I need linux-headers-$(uname -r) or linux-headers-generic on Ubuntu?

Use linux-headers-$(uname -r) when you need headers for the currently running kernel right now. Use linux-headers-generic or linux-headers-generic-hwe-* when you want header updates to track your kernel metapackage over time.

Why does Ubuntu show “Unable to locate package linux-headers-$(uname -r)”?

This usually means package metadata is stale or your running kernel does not match a package in enabled Ubuntu repositories. Run sudo apt update first, then retry the install command for your active kernel version.

Do Linux kernel headers need to match the running kernel version for DKMS?

Yes, for reliable DKMS module builds you should install headers that match uname -r. Mismatched versions often trigger build failures for NVIDIA drivers, VirtualBox modules, and other third-party kernel modules.

Can I remove old Linux kernel headers on Ubuntu safely?

Yes. Removing old header packages is generally safe and can reclaim disk space, but keep headers for the kernel you boot most often if you compile modules. If needed later, reinstall matching headers with sudo apt install linux-headers-$(uname -r).

Conclusion

Linux kernel headers are now set up on Ubuntu with matching verification and recovery steps for the common package and DKMS failure paths. This keeps module-based workflows stable when kernels change. If you are expanding your driver stack next, review NVIDIA drivers on Ubuntu or use VirtualBox on Ubuntu for virtualization workloads that depend on clean header alignment.

Search LinuxCapable

Need another guide?

Search LinuxCapable for package installs, commands, troubleshooting, and follow-up guides related to what you just read.

Found this guide useful?

Support LinuxCapable to keep tutorials free and up to date.

Buy me a coffee Buy me a coffee

Before commenting, please review our Comments Policy.
Formatting tips for your comment

You can use basic HTML to format your comment. Useful tags currently allowed:

You type Result
<code>command</code> command
<strong>bold</strong> bold
<em>italic</em> italic
<blockquote>quote</blockquote> quote block

Leave a Comment

We read and reply to every comment - let us know how we can help or improve this guide.

Let us know you are human: