How to Build Linux Kernel From Scratch {Step-By-Step Guide}

November 12, 2020

Introduction

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

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

In this step-by-step guide, you will learn how to build and compile a Linux kernel from scratch.

How to Build Linux Kernel

Prerequisites

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

Building Linux Kernel

The process of building a Linux kernel takes seven easy steps to complete. However, the procedure requires a significant amount of time to complete, depending on the system speed.

Follow the steps below to build the latest Linux kernel at the time of writing this article.


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


Step 1: Download the Source Code

1. Visit the official kernel website and download the latest kernel version. The downloaded file contains a compressed source code.

Downloading the latest Linux kernel.

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

The output shows the “saved” message when the download completes.

Using the wget command to download the source code.

Step 2: Extract the Source Code

When the file is ready, run the tar command to extract the source code:

tar xvf linux-5.9.6.tar.xz

The output displays the extracted kernel source code:

Extracting the source code.

Step 3: Install Required Packages

Install additional packages before building a kernel. To do so, run this 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 packages:

PackagePackage description
gitTracks and makes a record of all changes during development in the source code. It also allows reverting the changes.
fakerootPackaging tool that makes the fake root environment.
build-essentialInstalls development tools such as C, C++, gcc, and g++.
ncurses-devProgramming library that provides API for the text-based terminals.
xz-utilsProvides fast file compression and decompression.
libssl-devSupports SSL and TSL that encrypt data and make the internet connection secure.
bc (Basic Calculator)A mathematical scripting language that supports the interactive execution of statements.
flex (Fast Lexical Analyzer Generator)Generates lexical analyzers that convert characters into tokens.
libelf-devIssues a shared library for managing ELF files (executable files, core dumps and object code)
bisonGNU parser generator that converts grammar description to a C program.

Installing additional packages.

Step 4: Configure Kernel

The Linux kernel source code comes with the default configuration. However, you can adjust it to your needs. To do so, follow the steps below:

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

cd linux-5.9.6

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

cp -v /boot/config-$(uname -r) .config
Navigating to the kernel folder and copying configuration file.

3. To make changes to the configuration file, run the make command:

make menuconfig

The command launches several scripts, which then open the configuration menu:

Loading the configuration menu.

4. The configuration menu includes options such as firmware, file system, network, and memory settings. Use the arrows to make a selection or choose HELP to learn more about the options. When you finish making the changes, select SAVE, and then exit the menu.

Changing the settings in the navigation menu.

Note: Changing settings for some options can lead to a non-functional kernel. If you are unsure what to change, leave the default settings.


Step 5: Build the Kernel

1. Start building the kernel by running the following command:

make

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

The terminal lists all Linux kernel components: memory management, hardware device drivers, filesystem drivers, network drivers, and process management.

Using the make command to build kernel.

2. Install the required modules with this command:

sudo make modules_install
Installing kernel modules.

3. Finally, install the kernel by typing:

sudo make install 

The output shows done when finished:

Installing kernel.

Step 6: Update the Bootloader (Optional)

The GRUB bootloader is the first program that runs when the system powers on.

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

1. Update the initramfs to the installed kernel version:

sudo update-initramfs -c -k 5.9.6

2. Update the GRUB bootloader with this command:

sudo update-grub

The terminal prints out the process and confirmation message:

Updating the grub.

Step 7: Reboot and Verify Kernel Version

When you complete the steps above, reboot the machine.

When the system boots up, verify the kernel version using the uname command:

uname -mrs

The terminal prints out the current Linux kernel version.

Verifying the kernel version.

Conclusion

In this step-by-step guide, you learned how to build a 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 has a modular design. Functionality is extendible with modules or drivers. Learn how to use the modprobe command to add or remove modules on Linux.

Was this article helpful?
YesNo
Nevena Pavlicic
Nevena Pavlicic is an aspiring Technical Writer at PhoenixNAP with experience in writing user manuals and blog posts. She has always enjoyed researching and building knowledge on cutting edge technologies, but she is also passionate about simplifying complex concepts.
Next you should read
How to Remove Old Kernels on Ubuntu 16.04, 18.04, & 19.04
March 11, 2020

There are several methods to remove old or unused kernels. It's also considered good system hygiene practice...
Read more
How to Upgrade Linux Kernel in CentOS 7
October 8, 2019

The Linux kernel is the underlying software architecture of each Linux distribution. It provides and defines...
Read more
How to Uninstall or Remove Software Packages From Ubuntu
September 4, 2019

This guide will walk you through several methods for removing old software from Ubuntu...
Read more
How to Check Kernel Version in Linux in Command Line
June 25, 2019

The Linux kernel is much like the central brain of the operating system. Although it is open-source, meaning...
Read more
  • © 2021 Copyright phoenixNAP | Global IT Services. All Rights Reserved.