4 min read

How to compile a custom Linux kernel for Raspberry Pi

How to compile a custom Linux kernel for Raspberry Pi
Photo by Vishnu Mohanan / Unsplash

Hi all, hope your are well.

In this post, I will be showing you how to compile your very own kernel.
The reason for this is, that there are many hardware add-ons for the RPI for example, that require to compile a custom kernel, so we need to know how to do this :)

What you will need:

  • your own computer (Linux, mac or windows), I will be showing you how to compile the kernel on Linux
  • some basic knowledge of shell commands, but don't worry, I will show everything that you need to execute in terminal
  • time, as kernel compilation is time consuming, depending on the power of your machine
  • kernel, in our case we will be compiling the Raspberry Pi kernel

Here we go:

BTW. scvgn and mac4u are the same thing :)

First thing that we will need to do, is get our kernel. Now there are many options for this, one of which if the [kernel.org] website (for the x86 kernel).
In this tutorial we will download the raspberry pi kernel from GitHub: https://github.com/raspberrypi/linux.git

Type in your terminal window the following command:

git clone https://github.com/raspberrypi/linux.git

The download process make take some time, depending on your internet connection. The RPI kernel is big :)

Now, as we have the kernel on our machine, let's go into the folder:

cd linux

Let's check what's inside the folder:

ls -a

As you can see there is a lot of file here. To compile your own custom version of kernel you will need a .conf file. You can achieve that by executing this command:

make menuconfig

This will enable a nice GUI to help with the selection of the elements of the kernel that you need.
I will not add anything into the kernel, this is just to show you how to compile it.
![[Screen Shot 2022-04-13 at 13.49.39.png]]

When you done selecting all the elements of the kernel that you need, click save, confirm the file as .config, EXIT, and again EXIT.

Just to let you know, there are some software requirements to compile a Raspberry Pi kernel, or any other kernel. Here is the link to the kernel_org documentation with software requirements: [https://www.kernel.org/doc/html/latest/process/changes.html]

Depending on the Raspberry Pi that you want the kernel for, you will need to set some preferences. Inside the Linux folder type the following commands (depending on your model of Pi):

For Raspberry Pi 1, Zero and Zero W, and Raspberry Pi Compute Module 1 default (32-bit only) build configuration

KERNEL=kernel
make bcmrpi_defconfig

Raspberry Pi 2, 3, 3+ and Zero 2 W, and Raspberry Pi Compute Modules 3 and 3+ default 32-bit build configuration

KERNEL=kernel7
make bcm2709_defconfig

Raspberry Pi 4 and 400, and Raspberry Pi Compute Module 4 default 32-bit build configuration

KERNEL=kernel7l
make bcm2711_defconfig

Raspberry Pi 3, 3+, 4, 400 and Zero 2 W, and Raspberry Pi Compute Modules 3, 3+ and 4 default 64-bit build configuration

KERNEL=kernel8
make bcm2711_defconfig

Now we can compile the kernel by running (zImage and Image are the new names for the kernels that we will compile, you can set this to something different if you want):

the 32-bit kernel:

make zImage modules dtbs
sudo make modules_install

the 64-bit kernel:

make Image modules dtbs
sudo make modules_install

This will take a second, or more :)

When all done, the only thing left is to copy the new kernel into you RPi machine, to do so copy the image (from the Linux folder that we compiled the kernel) to the boot directory on the microSD card of your PI:

again 32-bit version:

sudo cp arch/arm/boot/dts/*.dtb /mnt/boot/
sudo cp arch/arm/boot/dts/overlays/*.dtb* /mnt/boot/overlays/
sudo cp arch/arm/boot/dts/overlays/README /mnt/boot/overlays/
sudo cp arch/arm/boot/zImage /mnt/boot/$KERNEL.img

and 64-bit version:

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 cp arch/arm64/boot/Image /mnt/boot/$KERNEL.img

Bear in mind that the boot location above is inside the /mnt/ folder in my case, I have mounted the micro-SD card from my RPI to that location, your location of the micro-SD card boot could be different.

Any questions, let me know in comments.

Enjoy!