Build the kernel
The default compilers and linkers distributed with an OS are configured to build executables to run on that OS. Native builds use these default compilers and linkers. Cross-compilation is the process of building code for a target other than the one running the build process.
Cross-compilation of the Raspberry Pi kernel allows you to build a 64-bit kernel from a 32-bit OS, and the other way around. Alternatively, you can cross-compile a 32-bit or 64-bit Raspberry Pi kernel from a device other than a Raspberry Pi.
The instructions below are divided into native builds and cross-compilation. Choose the section appropriate for your situation; although the two processes share many steps, there are also some important differences.
|
On Raspberry Pi OS, the firmware loads the kernel image ( |
Download kernel source
Before you can build for any target, you need the kernel source. To get the kernel source, you need Git. Begin by installing Git on your device, if you don’t already have it:
$ sudo apt install git
Next, download the source code for the latest Raspberry Pi kernel:
$ git clone --depth=1 https://github.com/raspberrypi/linux
This can take several minutes.
|
The To download a different branch with no history, add the
For a full list of available branches, see the the Raspberry Pi kernel repository. |
Now that you have the kernel source, build a fresh kernel natively or through cross-compilation.
Natively build a kernel
This guide assumes that your Raspberry Pi runs the latest version of Raspberry Pi OS.
First, install the build dependencies:
$ sudo apt install bc bison flex libssl-dev make
Build configuration
This section describes how to apply the default configuration when you build a kernel. You can also configure your kernel in the following ways:
To prepare the default configuration, run the appropriate commands from the sections below depending on your architecture and Raspberry Pi model.
64-bit architecture
- Raspberry Pi 3, 3+, CM3, CM3+, Zero 2 W, 4, 400, CM4, CM4S, 5, 500, 500+, and CM5
-
$ cd linux $ KERNEL=kernel8 $ make bcm2711_defconfig - Raspberry Pi 5, 500, 500+, and CM5 (optimised)
-
$ cd linux $ KERNEL=kernel_2712 $ make bcm2712_defconfig
|
|
32-bit architecture
- Raspberry Pi 1, CM1, Zero, Zero W
-
$ cd linux $ KERNEL=kernel $ make bcmrpi_defconfig - Raspberry Pi 2, 3, 3+, CM3, CM3+, Zero 2 W
-
$ cd linux $ KERNEL=kernel7 $ make bcm2709_defconfig - Raspberry Pi 4, 400, CM4, CM4S (kernels before 6.18)
-
$ cd linux $ KERNEL=kernel7l $ make bcm2711_defconfig
|
The 32-bit distribution of Raspberry Pi OS on BCM2711 devices uses a 32-bit userland, but a 64-bit kernel. From kernel version 6.18, 32-bit kernel builds are not supported on BCM2711 devices (Raspberry Pi 4, 400, CM4, CM4S); use the 64-bit |
Customise the kernel version using LOCALVERSION
To prevent the kernel from overwriting existing modules in /lib/modules and to clarify that you run your own kernel in uname output, adjust LOCALVERSION.
To adjust LOCALVERSION, change the following line in .config:
CONFIG_LOCALVERSION="-v8-MY_CUSTOM_KERNEL"
Each default configuration sets its own LOCALVERSION suffix, for example, -v8 for bcm2711_defconfig (64-bit), -v8-16k for bcm2712_defconfig, and -v7 for bcm2709_defconfig. Keep that suffix and append your own text, as in the example above.
You can also change this setting graphically with menuconfig at General setup > Local version - append to kernel release. For more information about menuconfig, see the kernel configuration instructions.
|
Build
Next, build the kernel. This step can take a long time, depending on your Raspberry Pi model.
-
Run the following commands to build a 64-bit kernel:
$ make -j6 Image.gz modules dtbs -
Run the following command to build a 32-bit kernel:
$ make -j6 zImage modules dtbs
On multi-core Raspberry Pi models, the make -j<n> option distributes work between cores. This can speed up compilation significantly. Run nproc to see how many processors you have; we recommend passing a number 1.5x your number of processors.
|
Install the kernel
Next, install the kernel modules onto the boot media:
$ sudo make -j6 modules_install
Then, install the kernel and Device Tree blobs into the boot partition, backing up your original kernel.
|
On BCM2712 devices (Raspberry Pi 5, 500, 500+, and CM5), the firmware loads Raspberry Pi OS includes
|
If you don’t want to install the freshly-compiled kernel onto the Raspberry Pi where you run this command, copy the compiled kernel to the boot partition of a separate boot media instead of /boot/firmware/.
|
To install the 64-bit kernel:
-
Run the following commands to create a backup image of the current kernel, and install the fresh kernel image, Device Tree blobs, overlays, and README:
$ sudo cp /boot/firmware/$KERNEL.img /boot/firmware/$KERNEL-backup.img $ sudo cp arch/arm64/boot/Image.gz /boot/firmware/$KERNEL.img $ sudo cp arch/arm64/boot/dts/broadcom/*.dtb /boot/firmware/ $ sudo cp arch/arm64/boot/dts/overlays/*.dtb* /boot/firmware/overlays/ $ sudo cp arch/arm64/boot/dts/overlays/README /boot/firmware/overlays/
To install the 32-bit kernel:
-
Create a backup of your current kernel and install the fresh kernel image:
$ sudo cp /boot/firmware/$KERNEL.img /boot/firmware/$KERNEL-backup.img $ sudo cp arch/arm/boot/zImage /boot/firmware/$KERNEL.img -
Depending on your kernel version, run the following command:
-
For kernels up to version 6.4:
$ sudo cp arch/arm/boot/dts/*.dtb /boot/firmware/ -
For kernels version 6.5 and above:
$ sudo cp arch/arm/boot/dts/broadcom/*.dtb /boot/firmware/
-
-
Finally, copy over the overlays and README:
$ sudo cp arch/arm/boot/dts/overlays/*.dtb* /boot/firmware/overlays/ $ sudo cp arch/arm/boot/dts/overlays/README /boot/firmware/overlays/
Finally, run the following command to reboot your Raspberry Pi and run your freshly-compiled kernel:
$ sudo reboot
After your Raspberry Pi restarts, check that your custom kernel is running.
|
Alternatively, copy the kernel with a different filename (for example,
Combine this approach with a custom Raspberry Pi OS Bookworm and later include an When If you don’t provide a matching filename, you must specify the ramdisk using an explicit
|
|
Building and updating If you overwrite |
Cross-compile the kernel
First, you will need a suitable Linux cross-compilation host. We tend to use Ubuntu; since Raspberry Pi OS is also a Debian distribution, compilation commands are similar.
Install required dependencies and toolchain
To build the sources for cross-compilation, install the required dependencies onto your device. Run the following command to install most dependencies:
$ sudo apt install bc bison flex libssl-dev make libc6-dev libncurses5-dev
Then, install the proper toolchain for the kernel architecture you wish to build:
-
To install the 64-bit toolchain to build a 64-bit kernel, run the following command:
$ sudo apt install crossbuild-essential-arm64 -
To install the 32-bit toolchain to build a 32-bit kernel, run the following command:
$ sudo apt install crossbuild-essential-armhf
Build configuration
This section describes how to apply the default configuration when you build a kernel. You can also configure your kernel in the following ways:
To build the sources and Device Tree files, run the appropriate commands from the sections below depending on your architecture and Raspberry Pi model.
64-bit architecture
- Raspberry Pi 3, 3+, CM3, CM3+, Zero 2 W, 4, 400, CM4, CM4S, 5, 500, 500+, and CM5
-
$ cd linux $ KERNEL=kernel8 $ make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- bcm2711_defconfig - Raspberry Pi 5, 500, 500+, and CM5 (optimised)
-
$ cd linux $ KERNEL=kernel_2712 $ make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- bcm2712_defconfig
|
|
32-bit architecture
- Raspberry Pi 1, CM1, Zero, Zero W
-
$ cd linux $ KERNEL=kernel $ make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- bcmrpi_defconfig - Raspberry Pi 2, 3, 3+, CM3, CM3+, Zero 2 W
-
$ cd linux $ KERNEL=kernel7 $ make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- bcm2709_defconfig - Raspberry Pi 4, 400, CM4, and CM4S (kernels before 6.18)
-
$ cd linux $ KERNEL=kernel7l $ make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- bcm2711_defconfig
|
From kernel version 6.18, 32-bit kernel builds are not supported on BCM2711 devices (Raspberry Pi 4, 400, CM4, and CM4S); use the 64-bit |
Customise the kernel version using LOCALVERSION
To prevent the kernel from overwriting existing modules in /lib/modules and to clarify that you run your own kernel in uname output, adjust LOCALVERSION.
To adjust LOCALVERSION, change the following line in .config:
CONFIG_LOCALVERSION="-v8-MY_CUSTOM_KERNEL"
Each default configuration sets its own LOCALVERSION suffix, for example, -v8 for bcm2711_defconfig (64-bit), -v8-16k for bcm2712_defconfig, and -v7 for bcm2709_defconfig. Keep that suffix and append your own text, as in the example above.
You can also change this setting graphically with menuconfig at General setup > Local version - append to kernel release. For more information about menuconfig, see the kernel configuration instructions.
|
Build
-
Run the following command to build a 64-bit kernel:
$ make -j6 ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- Image modules dtbs -
Run the following command to build a 32-bit kernel:
$ make -j6 ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- zImage modules dtbs
On multi-core devices, the make -j<n> option distributes work between cores. This can speed up compilation significantly. Run nproc to see how many processors you have; we recommend passing a number 1.5x your number of processors.
|
Install the kernel
Having built the kernel, you need to copy it onto your Raspberry Pi boot media (likely an SD card or SSD) and install the modules.
Find your boot media
First, run lsblk. Then, connect your boot media. Run lsblk again; the new device represents your boot media. You should see output similar to the following:
sdb sdb1 sdb2
If sdb represents your boot media, sdb1 represents the the FAT32-formatted boot partition and sdb2 represents the (likely ext4-formatted) root partition.
First, mount these partitions as mnt/boot and mnt/root, adjusting the partition letter to match the location of your boot media:
$ mkdir mnt
$ mkdir mnt/boot
$ mkdir mnt/root
$ sudo mount /dev/sdb1 mnt/boot
$ sudo mount /dev/sdb2 mnt/root
Install
Next, install the kernel modules onto the boot media:
-
For 64-bit kernels:
$ sudo env "PATH=$PATH" make -j6 ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- INSTALL_MOD_PATH=mnt/root modules_install -
For 32-bit kernels:
$ sudo env "PATH=$PATH" make -j6 ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- INSTALL_MOD_PATH=mnt/root modules_install
Next, install the kernel and Device Tree blobs into the boot partition, backing up your original kernel.
|
On BCM2712 devices (Raspberry Pi 5, 500, 500+, and CM5), the firmware loads
|
To install the 64-bit kernel:
-
Run the following commands to create a backup image of the current kernel, install the fresh kernel image, overlays, README, and unmount the partitions:
$ sudo cp mnt/boot/$KERNEL.img mnt/boot/$KERNEL-backup.img $ sudo cp arch/arm64/boot/Image mnt/boot/$KERNEL.img $ sudo cp arch/arm64/boot/dts/broadcom/*.dtb mnt/boot/ $ sudo cp arch/arm64/boot/dts/overlays/*.dtb* mnt/boot/overlays/ $ sudo cp arch/arm64/boot/dts/overlays/README mnt/boot/overlays/ $ sudo umount mnt/boot $ sudo umount mnt/root
To install the 32-bit kernel:
-
Run the following commands to create a backup image of the current kernel and install the fresh kernel image:
$ sudo cp mnt/boot/$KERNEL.img mnt/boot/$KERNEL-backup.img $ sudo cp arch/arm/boot/zImage mnt/boot/$KERNEL.img -
Depending on your kernel version, run the following command to install Device Tree blobs:
-
For kernels up to version 6.4:
$ sudo cp arch/arm/boot/dts/*.dtb mnt/boot/ -
For kernels version 6.5 and above:
$ sudo cp arch/arm/boot/dts/broadcom/*.dtb mnt/boot/
-
-
Finally, install the overlays and README, and unmount the partitions:
$ sudo cp arch/arm/boot/dts/overlays/*.dtb* mnt/boot/overlays/ $ sudo cp arch/arm/boot/dts/overlays/README mnt/boot/overlays/ $ sudo umount mnt/boot $ sudo umount mnt/root
Finally, connect the boot media to your Raspberry Pi and connect it to power to run your freshly compiled kernel. Then, check that your custom kernel is running.
|
Alternatively, copy the kernel with a different filename (for example,
Combine this approach with a custom Raspberry Pi OS Bookworm and later include an When If you don’t provide a matching filename, you must specify the ramdisk using an explicit
|
Check that your custom kernel is running
After you boot your Raspberry Pi with the new kernel, run the following command:
$ uname -r
The output ends with the LOCALVERSION that you set, for example, 6.18.54-v8-MY_CUSTOM_KERNEL.
If the output shows the stock kernel version instead, check the following:
-
On Raspberry Pi 5, 500, 500+, and CM5, the firmware boots
kernel_2712.imgin preference tokernel8.img. If you builtKERNEL=kernel8, addkernel=kernel8.imgtoconfig.txt. -
A
kernel=oros_prefix=line inconfig.txtcan select a different kernel file from the one you installed. Checkconfig.txtfor these lines, including inside conditional filter sections such as[pi5]. -
Make sure you copied the kernel image to the boot partition (
/boot/firmware/on a running system), not to the root partition. -
When cross-compiling, make sure you unmounted the boot media before removing it, so that all files were written.