Build Kernel For Raspberry Pi
Building a custom Linux kernel for the Raspberry Pi lets you optimize performance, add features, and tailor the system to your needs. It involves configuring and compiling the official kernel source for Raspberry Pi hardware.
terminal
sudo apt upgrade
sudo apt install git bc bison flex libssl-dev make libc6-dev libncurses5-dev
sudo apt install crossbuild-essential-arm64
sudo apt install crossbuild-essential-armhf
crossbuild-essential-arm64
: install this package if raspberry is 64 bit (raspberry pi 4, raspberry pi 5,...)crossbuild-essential-armhf
: install this package if raspberry is 32 bit(raspberry pi 3, raspberry pi 3+,...)terminal
git clone --depth=1 --branch <branch> https://github.com/raspberrypi/linux
If using: git clone --depth=1 https://github.com/raspberrypi/linux
, clone latest branch
To prepare the default configuration, run the appropriate commands from the table below for your Raspberry Pi model.
terminal
cd linux
KERNEL=kernel8
terminal
cd linux
KERNEL=kernel_2712
terminal
cd linux
KERNEL=kernel
terminal
cd linux
KERNEL=kernel7
terminal
cd linux
KERNEL=kernel7l
terminal
# Raspberry Pi 3, Compute Module 3, Raspberry Pi 3+, Raspberry Pi Zero 2 W, Raspberry Pi 4, Pi 400, Compute Module 4, Compute Module 4S
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- bcm2711_defconfig
# Raspberry Pi 5, Pi 500, Compute Module 5
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- bcm2712_defconfig
terminal
# Raspberry Pi 1, Compute Module 1, Zero, Zero W
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- bcmrpi_defconfig
# Raspberry Pi 2, Raspberry Pi 3, Compute Module 3, Raspberry Pi 3+, Compute Module 3+, Zero 2 W
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- bcm2709_defconfig
# Raspberry Pi 4, Pi 400, Compute Module 4, Compute Module 4S
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- bcm2711_defconfig
Menuconfig
terminal
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- menuconfig
terminal
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- menuconfig
Build source
terminal
make -j$(nproc) ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- Image modules dtb
terminal
make -j$(nproc) ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- zImage modules dtbs
On multi-core Raspberry Pi models, themake -j<n>
option distributes work between cores. This can speed up compilation significantly. Runnproc
to see how many processors you have; we recommend passing a number 1.5x your number of processors.
lsblk
; the new device represents your boot media. You should see output similar to the following:terminal
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.
3. First, mount these partitions as mnt/boot
and mnt/root
, adjusting the partition letter to match the location of your boot media:
terminal
mkdir mnt
mkdir mnt/boot
mkdir mnt/root
sudo mount /dev/sdb1 mnt/boot
sudo mount /dev/sdb2 mnt/root
4. Next, install the kernel modules onto the boot media:
terminal
sudo env PATH=$PATH make -j$(nproc) ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- INSTALL_MOD_PATH=mnt/root modules_install
terminal
sudo env PATH=$PATH make -j$(nproc) ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- INSTALL_MOD_PATH=mnt/root modules_install
5. Next, install the kernel and Device Tree blobs into the boot partition, backing up your original kernel.
terminal
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
Run the following commands to create a backup image of the current kernel and install the fresh kernel image:
terminal
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:
terminal
# For kernels up to version 6.4
sudo cp arch/arm/boot/dts/*.dtb mnt/boot/
terminal
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:
terminal
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
6. Finally, connect the boot media to your Raspberry Pi and connect it to power to run your freshly-compiled kernel.
Comments