How to Compile Kernel for Raspberry Pi: Step-by-Step Guide
To compile the kernel for Raspberry Pi, first clone the Raspberry Pi Linux kernel source with
git, then configure it using make bcmrpi_defconfig or a similar config, and finally build it with make -j$(nproc). After compiling, copy the kernel and modules to your SD card's boot and root partitions.Syntax
Here is the basic syntax to compile the Raspberry Pi kernel:
git clone --depth=1 https://github.com/raspberrypi/linux- Downloads the kernel source.make bcmrpi_defconfig- Sets default config for Raspberry Pi 1 (usebcm2711_defconfigfor Pi 4).make -j$(nproc)- Compiles the kernel using all CPU cores.make modules_install INSTALL_MOD_PATH=../modules- Installs kernel modules to a folder.
bash
git clone --depth=1 https://github.com/raspberrypi/linux cd linux make bcmrpi_defconfig make -j$(nproc) make modules_install INSTALL_MOD_PATH=../modules
Example
This example shows how to compile the kernel for Raspberry Pi 4 and prepare the files for the SD card.
bash
git clone --depth=1 https://github.com/raspberrypi/linux cd linux make bcm2711_defconfig make -j$(nproc) make modules_install INSTALL_MOD_PATH=../modules # Copy kernel and modules to SD card (mounted at /mnt) sudo cp arch/arm64/boot/Image /mnt/boot/kernel8.img sudo cp -r ../modules/lib/modules /mnt/lib/ # Update firmware files if needed sudo cp -r ../modules/lib/firmware /mnt/lib/
Common Pitfalls
- Wrong config: Using
bcmrpi_defconfigfor Pi 4 will cause boot failure. Usebcm2711_defconfigfor Pi 4. - Missing dependencies: Kernel build needs packages like
bc,build-essential, andlibncurses-dev. Install them first. - Incorrect module install path: Always set
INSTALL_MOD_PATHto a temporary folder before copying to SD card. - Not cleaning old builds: Run
make cleanif you rebuild to avoid conflicts.
bash
## Wrong config example (Pi 4 boot fails): make bcmrpi_defconfig ## Correct config for Pi 4: make bcm2711_defconfig
Quick Reference
Summary tips for compiling Raspberry Pi kernel:
- Clone kernel source with
git clone --depth=1 https://github.com/raspberrypi/linux - Choose correct config:
bcmrpi_defconfig(Pi 1),bcm2709_defconfig(Pi 2/3),bcm2711_defconfig(Pi 4) - Install build tools:
sudo apt install bc build-essential libncurses-dev - Use
make -j$(nproc)to speed up compilation - Copy kernel and modules to SD card boot and root partitions
Key Takeaways
Clone the Raspberry Pi kernel source from the official GitHub repository.
Select the correct default config for your Raspberry Pi model before compiling.
Install all necessary build dependencies to avoid errors during compilation.
Use all CPU cores with
make -j$(nproc) to speed up the build.Copy the compiled kernel and modules properly to your SD card for booting.