How to install Linux kernel headers on Raspberry Pi

A Linux kernel is the core of a Linux distribution such as Raspberry Pi OS and consists of three things: the kernel itself, the kernel’s headers, and the kernel’s extra modules. The kernel headers are used to define device interfaces. For example, they can be used to compile the module that controls your computer’s video card and driver.

The main reason you may find yourself needing to install kernel headers is if you are compiling kernel modules and need your Raspberry Pi to access and communicate with hardware as intended. Sometimes device drivers may require you to update the kernel headers on your system to function properly. Other times, there may be compatibility issues with new versions and you will need to roll back an update.

The process for doing this can vary on each distribution, but in this tutorial we will be going over the steps specifically for Raspberry Pi OS on the Raspberry Pi. Continue reading to find out how to install kernel headers, check the version of installed kernel headers, and switch between kernel header versions on the Raspberry Pi.

In this tutorial you will learn:

  • How to search for kernel headers to install
  • How to install kernel headers on Raspberry Pi
  • How to check for currently installed kernel headers
How to install Linux kernel headers on Raspberry Pi
How to install Linux kernel headers on Raspberry Pi
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Raspberry Pi
Software Kernel headers
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Kernel Header Installation for Raspberry Pi step by step instructions




Follow the step by step instructions below to see how to check the version of the currently installed kernel headers on Raspberry Pi, how to install the kernel headers package, and switch between different versions of kernel headers on Raspberry Pi.

  1. Let’s start by opening a command line terminal and using the following apt command to check what kernel headers are currently installed:
    $ sudo apt list --installed | grep headers
    

    The output from the command above will indicate whether or not you have any kernel headers already installed.

    NOTE
    Your Raspberry Pi may contain a few different versions of kernel headers. Older versions are typically retained by the system in case we encounter a problem with one of the newer versions. However, the newest version listed in the above output will be the one that your Raspberry Pi is currently using.
  2. Generally, the version of our system’s kernel headers should correspond to the version of the kernel itself. This may not be the case if you are having compatibility issues and want to install an older version or if you need to test out the newest kernel header packages by installing a more upgraded version. Open a terminal and type the following command to check the version of your Raspberry Pi system’s kernel:
    $ uname -r
    6.1.21-v8+
    




    As you will see from the above output, the version of the kernel on our Raspberry Pi is 6.1.21-48+.

  3. Now, let’s update our kernel headers to be on par with the kernel itself. Or, if you do not have kernel headers installed at all, this command will install them for you. Type the following command in your terminal:
    $ sudo apt update
    $ sudo apt install raspberrypi-linux-headers
    
    Installing the Raspberry Pi kernel headers with package manager
    Installing the Raspberry Pi kernel headers with package manager
  4. You can check again to make sure that the package installed and see what version you have:
    $ sudo apt list --installed | grep headers
    
    Verifying that kernel headers were successfully installed on the Raspberry Pi
    Verifying that kernel headers were successfully installed on the Raspberry Pi
  5. If you are looking for a specific version of Linux kernel headers to install – for example, if you want to upgrade to a development version or downgrade your current kernel headers – we can check which versions of kernel headers are available in the official repository by executing this command:
    $ sudo apt search headers
    
    Searching for different kernel header versions available on the Raspberry Pi
    Searching for different kernel header versions available on the Raspberry Pi
  6. You may need to reboot your Raspberry Pi system to start using the kernel headers.
    $ reboot
    

Closing Thoughts




In this tutorial, we saw how to install, upgrade, and switch between kernel header versions on a Raspberry Pi system. This should allow for additional functionality with a range of peripheral devices that can be plugged into the device, even allowing for experimental features only available in development versions of the kernel headers.



Comments and Discussions
Linux Forum