How do I build a Linux kernel from scratch? Detailed step-by-step guide

Introduction to the Linux kernel construction tutorial

The Linux kernel is the foundation of a Unix-like operating system. The kernel is responsible for the communication between hardware and software and the allocation of available resources.

All Linux distributions are based on a predefined kernel. However, if you want to disable multiple options and drivers or try experimental patches, you’ll need to build a Linux kernel.

How to build a Linux kernel? In this step-by-step guide, you’ll learn how to build and compile the Linux kernel from scratch.

Prerequisite

  • A system running Linux
  • Access the terminal/command line
  • A user account with sudo/root privileges
  • There is 12GB of free space on the hard drive

Build the Linux kernel

How to build a Linux kernel? The process of building a Linux kernel can be completed in seven simple steps. However, the process takes a lot of time to complete, depending on the system speed.

At the time of writing, follow the steps below to build the latest Linux kernel.


Note: If the version on the kernel website does not match the version in the following steps, use these commands and replace the kernel version number.


Step 1: Download the source code

1. How does Linux build the kernel? Visit the official kernel website to download the latest kernel version. The downloaded file contains the compressed source code.

2. Open the terminal and use the wget command to download the Linux kernel source code:

wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.9.6.tar.xz

Once the download is complete, the output shows a “saved” message.

Step 2: Extract the source code

Linux kernel build tutorial: When the file is ready, run the tar command to extract the source code:

tar xvf linux-5.9.6.tar.xz

The output shows the extracted kernel source code:

Step 3: Install the required packages

How does Linux build the kernel? Install additional packages before building the kernel. To do this, run the following command:

sudo apt-get install git fakeroot build-essential ncurses-dev xz-utils libssl-dev bc flex libelf-dev bison

The command we used above installs the following package:

parcelPacking instructions
gitTrack and document all changes in the development process in the source code. It also allows you to revert your changes.
fakerootA packaging tool for making a false root environment.
build-essentialInstall development tools such as C, C++, gcc, and g++.
ncurses-devA programming library that provides an API for text-based terminals.
xz-utilsProvides fast file compression and decompression.
libssl-devSSL and TSL that encrypt data and secure your internet connection are supported.
BC (Basic Calculator)A mathematical scripting language that supports the interactive execution of statements.
Flex (Quick Lexer Builder)Generate a lexical analyzer that converts characters into tokens.
libelf-devPublish a shared library for managing ELF files (executables, core dumps, and object code).
bisonA GNU parser generator that converts syntactic descriptions into C programs.

Step 4: Configure the kernel

How to build a Linux kernel? The Linux kernel source code comes with a default configuration. However, you can adjust it as needed. To do this, follow these steps:

1. Navigate to linux-5.9.6. directory using cd command:

cd linux-5.9.6

2. Copy the existing configuration file using the following cp command:

cp -v /boot/config-$(uname -r) .config

3. To change the configuration file, run the following make command:

make menuconfig

The command launches several scripts and then opens the configuration menu:

4. The configuration menu includes options such as firmware, file system, network, and memory settings. Use the arrows to make selections or select Help to learn more about the options. When you’re done making changes, select Save, and then exit the menu.


Note: Changing the settings for some options may cause the kernel to not work properly. If you’re not sure what to change, leave the default settings.


Step 5: Build the kernel

1. How do I build a Linux kernel? Start building the kernel by running the following command:

make

The process of building and compiling the Linux kernel takes some time to complete.

Terminal lists all Linux kernel components: memory management, hardware device drivers, file system drivers, network drivers, and process management.

2. Use the following command to install the required modules:

sudo make modules_install

3. Finally, enter the following command to install the kernel:

sudo make install 

When done, the output shows complete:

Step 6: Update the bootloader (optional)

Linux Kernel Build Tutorial: The GRUB bootloader is the first program that runs when the system is booted up.

The make install command does this process automatically, but you can also do it manually.

1. Update initramfs to the installed kernel version:

sudo update-initramfs -c -k 5.9.6

2. Update the GRUB bootloader with the following command:

sudo update-grub

The terminal prints out the process and confirmation information:

Step 7: Reboot and verify the kernel version

How does Linux build the kernel? Once you have completed the above steps, restart your machine.

When the system starts, use the following uname command to verify the kernel version:

uname -mrs

The terminal prints out the current Linux kernel version.

Conclusion of the Linux kernel build tutorial

How to build a Linux kernel? In this step-by-step guide, you learned how to build the Linux kernel from scratch and install the required packages.

If you follow the instructions carefully, the process will complete successfully on your Linux machine.

The Linux kernel is modular in design. Functionality can be extended via modules or drivers. Learn how to add or remove modules on Linux using modprobe commands.