How to Compile Linux kernel

There were 22 Billion internet connected devices in the world at the end of 2018. Therefore much more total computing devices including the ones that are not connected. Plus, by 2024 the number must be substantially higher. These devices run various programs written in different programming languages. Some run boring mainframe code, while others run trendy AI and ML models. Something really really fundamental to all these devices is that they run an OS – an Operating System. Majority of them run Linux. Lets go back to basics today and do something really fundamental. Let’s learn how to compile Linux kernel.

Step 1: Install Dependencies Before compiling the kernel, you’ll need to install some dependencies. These may include development tools, compilers, and libraries. The required packages vary depending on your distribution. For example, on Debian-based systems, you can install the necessary packages with the following command:

sudo apt-get install build-essential libncurses-dev bison flex libssl-dev libelf-dev

Step 2: Download the Kernel Source Code You can download the kernel source code from the official Linux kernel website (https://www.kernel.org/). Choose a long term supported version e.g. linux-6.6.24.tar.xz and download the corresponding tarball.

Step 3: Extract the Source Code Navigate to the directory where you downloaded the tarball and extract it using the following command:

tar xvf linux-6.6.24.tar.xz

Step 4: Configure the Kernel Change into the kernel source directory:

cd linux-6.6.24
Steps to compile Linux kernel
Steps to compile Linux kernel

Run the following command to start the kernel configuration:

make menuconfig

This command opens a text-based menu where you can configure various kernel options. You can navigate through the menu using the arrow keys and select options using the spacebar. Once you’re done configuring, save your changes and exit the menu.

Compile the Kernel Once you’ve configured the kernel, you’re ready to compile it. Run the following commands:

make -j$(nproc)

This command starts the compilation process. The “-j$(nproc)” option tells make to use as many parallel processes as there are CPU cores, which can speed up the compilation process significantly.

Install the Kernel Modules After the compilation is complete, you can install the kernel modules using the following command:

sudo make modules_install

Install the Kernel To install the newly compiled kernel, run the following command:

sudo make install

This command installs the kernel image, kernel modules, and other necessary files.

Step 5: Update Boot Loader Configuration Finally, you need to update your boot loader configuration to include the new kernel. The procedure for doing this varies depending on your boot loader (e.g., GRUB, LILO).

Reboot Once you’ve updated the boot loader configuration, reboot your system to boot into the newly compiled kernel.

That’s it! You’ve successfully compiled and installed the Linux kernel.