"Trust me, Linux kernel compilation is not for everyone."

But still, today we shall know how to compile the Linux kernel. Before going directly into the point, first let's learn some basics and we will discuss some questions, that might be in your mind.

What is a Kernel?
A kernel is a piece of software, that controls the hardware and does some basic functions like file management. Every operating system has one. The Linux kernel is an open source; meaning you can view, modify and publish it's source code. To learn more on what is a kernel, click here .

Who really needs to compile the Linux kernel?
As said above, not everyone needs to compile their own Linux kernel from the source code. If you are a computer enthusiast, you can do this for learning or for fun. If you are a developer, you might be searching for a bug to help the Linux community. You maybe compiling the kernel for a new hardware which is not supported yet. Whatever maybe the reason, if you are reading this article, it shows that you are interested to know how a Linux kernel is compiled.

Now that we have discussed some basic things, let's begin the compilation process.

Configuring Grub

In case our new kernel fails to start, we need some time to select the old kernel in the grub boot menu which works.

Note: If your grub has enough timeout you can skip this step.

To change the grub timeout, open the grub config file in nano as root by giving the below command in terminal.

sudo nano /etc/default/grub

Comment these lines, by inserting "#" at the beginning of the lines. After commenting, the lines should look like as below.

# GRUB_HIDDEN_TIMEOUT=0
# GRUB_HIDDEN_TIMEOUT_QUIET=true

"GRUB_TIMEOUT" property should not be commented. To increase the grub timeout to 10 seconds, change this line as below.

GRUB_TIMEOUT=10

Press (Ctrl+O) to save the file and then press (Ctrl+X) to exit out of nano.

As we have made some changes to the grub config, we need to update our grub by giving the below command.

sudo update-grub

Downloading The Kernel Source

You can download the Linux kernel source code by visiting the website Kernel.org.

Note: If you absolutely need the latest kernel; you can clone a repository. But beware that those kernels might be extremely un-stable.

Avoid downloading RC release kernels, as they might be un-stable. Choosing a stable and long-term release kernel is recommended. If the kernel version has EOL written after it's version number, which means it has come to end of life and will not receive any further updates and security patches.

Once you have chosen the kernel version that you want to download. Click on the link that says "[tarball]". Now the download should start and hopefully you will be downloading a file that ends with either ".tar.gz" or ".tar.xz".

After the downloading has been completed, run the below command to extract the downloaded file.

Note: While extracting if you don't want want the terminal to output filenames you can replace the "xvf" in the below command with "xf". By doing that; the extraction will be a bit faster.

mkdir "linux_kernel"
tar xvf linux-* -C linux_kernel/ --strip-components=1

And then, change into the extracted directory by running the command:

cd ./linux_kernel/

Configuring The Downloaded Kernel

Before we can compile our downloaded kernel source, we need to have our configuration file in place. The configuration file tells the compiler what features, drivers, filesystems etc. to include. It is very hard to fire up a text editor and edit the configuration file manually because; there are thousands of options and modules. So, we have two ways to create the config file:

  1. Use the config file that came with your distribution or,
  2. Generate a new config file based on the currently connected devices to your computer.

Note: Use the config file that came with your distribution, if you frequently connect new hardware to your Linux machine.

The config file that came with your distro already has around 4000 different kernel modules and drivers for supporting different hardware you may connect to your machine. In-case you generate a new config file and try to connect new devices, you will need to re-create the config file and then re-compile the kernel.

Note: The kernel compilation time with the config file from your distro will take much longer and will result in a heavier kernel.

1. Use The Configaration File That Came With Your Distro

In this tutorial, I will use the config file that came with my Ubuntu Linux. To copy the config file from your distro, enter the following command:

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

2. Generate A Configaraion File Based On Connected Devices
If you want to use the second method, which will generate the config file based on the currently connected devices and on your hardware configuration, enter the following command.

make localmodconfig

You will be asked some questions weather to include some new features that the kernel provides. Press the enter key until all the questions are over otherwise. If you know what you are doing; you can take time to answer each question.

Setting Up The Environment

Run the below command to install all the required dependency packages, for a successful compilation.

sudo apt install build-essential libssl-dev libncurses5-dev gcc bc bison flex libelf-dev

Note: The dependencies may change over time. Also, the names of some packages defer from distribution to distribution, so please use other websites like StackOverflow to troubleshoot any compilation problems.

Before we start the compilation process, ensure screensaver and auto-sleep are disabled, as they would interrupt the compilation process.

Compiling The Linux Kernel

Now, that everything is set-up, let's compile the Linux kernel. As the compilation process will take a long time, it is your choice to run the compilation process under a single core or split the job to all the available cores in your processor.

To start the compilation process; run the below command in the terminal after replacing the "[x]" with the number of cores your processor has.

make -j[x] deb-pkg

Note: Running the compilation process using single core is recommended, though it takes more time, the compilation will result in good quality. Using more than one core takes less compilation time but, compilation quality may decrease.

To start the compilation process, using one core, run the below command in terminal.

make deb-pkg

Keep pressing enter key for all the the prompts asking to enable new features. Depending upon your computer's horsepower; compilation process may take anywhere from minutes to days. Do not interrupt the compilation process, otherwise you will have to start from the beginning.

Installing The Compiled Kernel

Once the compilation process has been completed successfully without any errors, you will find some deb packages in the parent directory. Now you can distribute these deb packages as a compiled kernel. I recommend you to first try installing on a virtual machine and test the kernel and see if everything works fine.

After ensuring the compiled deb packages work as expected, you can install them by running the following command.

cd ../
sudo dpkg -i linux-*.deb

The installation will take a few minutes. Finally reboot your Linux machine and enjoy your new kernel.

Conclusion

Compiling a kernel is not so easy and only developers who are into that kind of stuff do it. There are different forums that help you in-case there is any problem. To get a better idea, please watch this video.

Note: Video will be available after it has been released on YouTube.

Also, comment down below your reasons for compiling your own Linux kernel.

Happy Kernel Compilation 🙂