How to compile Linux Kernel on Ubuntu

Linux Kernel is an interesting project to play with. If you're interested in computer science, you may want to edit and build it by yourself. In this post, I'll show you how to compile Linux Kernel on Ubuntu system.

Getting Linux source code

There are many options to obtain source code of Linux. I'll show you them here.

From a GitHub mirror

There is a GitHub mirror of Linux Kernel. I prefer this method because cloning from GitHub is much faster.

From The Linux Kernel Archives

There is an official archive of Linux Kernel. You can download the tarballs of almost any release here.

You can decompress tarballs by using command below.

$ tar -xvf <tarball_name>

From Torvald's repository

You can clone it from Torvald's repository by using command below.

$ git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git

Preparing dependencies

There are few things to prepare. Just type command below.

$ sudo apt-get install build-essential git flex bison ncurses-dev libssl-dev libelf-dev 

Configuring and compiling

You can configure the build process by using command below at the root of the kernel source tree.

$ make menuconfig

You will see a screen like below. You can configure whatever you want here using your keyboard.

After configuration, it generates a .config file. Make sure that you have a proper .config file whenever you build the kernel.

If you struggle to configure the build process, you can use command below.

$ make defconfig

It generates a default .config file based on your system's architecture.

If you want to run the compiled kernel on your machine, you need to use your Linux distribution's configuration file. You can copy it by using command below.

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

Then verify the configuration file by using command below.

$ make oldconfig

Finally, you can compile the kernel by using command below.

$ make -j$(nproc)

Further readings

You can find useful documents from The Linux Kernel Documents.