Linux Kernel

Compile and install kernel ubuntu

A Kernel is simply a computer program and is the main component or heart of an operating system (OS) as it has control over everything of the system exactly like an Operating system. Whenever you start your system/os the first program that loads is the kernel after bootloader as Kernel has to handle the running processes and manage hardware devices (hard disk) and handling interrupts. The kernel remains in the OS’s memory until it is shut down.

Low-level tasks such as the memory, disk, task management, etc are done by the kernel and provide an interface between system hardware components and the user/system processes. A separate protected area of memory is provided for the kernel called Kernel Space and Kernel Space is not available for other applications of the system so that the kernel is safely loaded in this Kernel Space.

Often times you are put up to the task to compile the kernel and there can be a bunch of reasons as to why you can compile the kernel and some of them are:

  • Simply want to test how it’s done
  • enable/disable certain options of kernel
  • Enable hardware support that might not be available in the standard kernel
  • Using a distribution for which compiling kernel is required
  • Given an assignment in school/college

So in this article, we will install and compile the kernel on Ubuntu 20.04. And without wasting any more time let’s get into it.

Step1: Download Kernel

The first step involves downloading the source code of the Linux kernel from the following link:

https://www.kernel.org/

Click on the yellow button that says Latest Stable Kernel shown in the above screenshot.

You can also download via command line by executing the below command in your terminal by replacing version 5.14.3 with your latest version:

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

Step2: Installing requirements

This step involves installing all the requirements for the kernel to compile and can be achieved by executing a single command:

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

It should be noted that there should be at least 12 GB of free space on your hard drive so that the kernel compilation may take place smoothly.

Step3: Source Extraction

This step involves extracting the downloaded file which can be done by executing with the below command in your Ubuntu terminal:

$ tar xvf linux-5.14.3.tar.xz

Let us now change our directory to the newly created directory via the following command:

$ cd linux-5.14.3

Step4: Kernel Configuration

Let us now configure our Kernel so that we know which modules to include for which first we have to copy the config file for which execute the following command:

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

Now that we have copied the config file we can make changes to this file and execute the make menuconfig command which will open up a configuration tool and will show us every module and whether to enable or disable it:

$ make menuconfig

Step5: Compiling Kernel

Now that we are done with configuring Kernel let us start compiling the Kernel for which we will use a single command where the first part will be compiling the kernel with the make command and the second part will be installing the kernel modules. To achieve the second task execute make modules_install. Then comes the third part where make install will copy the Kernel and .config file to the /boot folder hence generating the system. map file.

$ make

The above questions involve which kernel you are upgrading from and which kernel you are upgrading to so based on your kernel answer the questions.

The -j option is used to assign more cores to the process so that the process speeds up.

To know the number of cores available, use the “nproc” command:

$ nproc

After knowing the number of cores, you can mention the number cores as shown below to the make command as well:

$ make -j 4

Let us now install all the modules that you enabled during the step4 for which we will execute the following command:

$ make modules_install

Step6: Install Kernel

After installing the modules we need to install Kernel by executing the below command:

$ sudo make install

Step7: Enable Kernel for boot

Once you are done with installing Kernel, then we have to enable Kernel for a boot, for which execute the below command:

$ sudo update-initramfs -c -k 5.14.13

Remember to replace the version in the above command with your version of the kernel you just compiled.

The next step is to update-grub for which type or copy the following command in your Ubuntu terminal and then press enter:

$ sudo update-grub

Step8: Reboot System

This step involves rebooting your system for which execute the reboot command in your terminal:

$ reboot

Step9: Verification of Linux Kernel

This last step involves verifying the new Linux Kernel version which can be achieved with the following command:

$ uname -mrs

Conclusion

A kernel being the heart of the operating system controls everything that the operating system controls and handles, running processes, and manages hardware devices. The kernel is loaded in the protected Kernel Space and the Kernel remains in the memory of the operating system until the OS is shut down.

We completed our process of compiling and installing Kernel on Ubuntu in this article and hopes that you were successful. If you’re having trouble, go back to the beginning and make sure you’re following each step carefully.

About the author

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.