How To: Compile Linux Kernel on Ubuntu

Current stable kernel version: 5.19.1

Current development kernel version: None right now

You are wondering why would you compile Linux kernel, when you already have generic one installed. Some of the reasons are: Ubuntu is not really famous for updating kernel to the newest version in 6 months cycle. New kernel brings new hardware support. And you can tweak the kernel as you like, so it can run faster and be slimmer. This HowTo will show you how to do exactly that. A lot of people thinks that compiling kernel is something that a ordinary PC user can’t do, but by following this tutorial, everyone can do it.
So, let’s start.

1) Installation of necessary packages:

sudo apt-get install build-essential bin86 kernel-package libqt3-headers libqt3-mt-dev wget libncurses5 libncurses5-dev qttools5-dev-tools qt4-dev-tools libqt4-dev qtbase5-dev qtchooser qt5-qmake qtbase5-dev-tools

2) Go to the directory in which you would like your kernel source to be. In this HowTo we will use KERNEL directory in our home folder.
– Open terminal by opening the Dash and doing a search for: terminal
– Now in terminal we write next command to create a directory KERNEL:

mkdir KERNEL

3) If we don’t want to write “sudo” before every command, we need to become root by executing next command: sudo su

4) Now we will enter KERNEL directory, download kernel archive and extract it.

wget -c && tar -xvJf linux-5.19.1.tar.xz

5) Now we will enter directory linux-5.19.1:

cd linux-5.19.1

6) Next step is importing of configuration options from currently running kernel:

cp /boot/config-$(uname -r) .config && yes “” | make oldconfig

7) Next step is entering the graphical interface for configuring the kernel options, so we can configure the kernel as we like:

make xconfig

If this doesn’t work for you, you need to use gconfig insted of xconfig:

make gconfig

8) Now you will have the graphical interface opened with a lot of options. In this tutorial I will cover only few essential ones for desktop PC.

– First you need to press CTRL+F and search for “debug” (without qutes),  click on Search and turn off as many options which have word “debug” in them. This will decrease size of the kernel image a lot.

– After that exit the search window and go to: General setup. There you need to find options Optimize for size and Automatic process group schedular and turn them on.

– Next step is to go to Processor type and features and turn on option Tickless System (Dynamic Ticks) and High Resolution Timer Support. Then in the part Processor family you need to choose the family of your CPU.
– Next step is to find option: Timer frequency and choose 1000Hz.

– Now go to Paravirtualized guest support section and turn off Xen quest support.

That is essentially the most important options that you need to change, but you can go and explore and turn off drivers for hardware that you don’t have.

9) When you are done with configuration, you need to click on the “Save” icon and exit the configuration window.

10) Now write next command in terminal:

make-kpkg clean

– When that is finished write next command:

CONCURRENCY_LEVEL=21 make-kpkg –initrd –append-to-version=-custom kernel_image kernel_headers

ATTENTION: CONCURRENCY_LEVEL is the option that is useful if you have multi-core CPU. The number after “=” represents Cores+1, so if you have 10 core CPU with Hyper-threading, you will write 21.

Option –append-to-version=  is for the revision of the your new kernel. You can change that from “custom” to anything you like, but it must have “-” before the name. For example –append-version=-skynet. With that, your new kernel will be named linux-5.19.1-skynet.

Above command will start kernel compiling and creation of .deb packages for kernel image and kernel headers. This will take some time, so you can do something else while this is completed.

11) When the compilation is completed, now you need to install created .deb packages:

cd ..
dpkg -i *.deb

This will install all .deb packages in current directory (KERNEL).

That is all folks. If you have any problems you can always choose -generic kernel from Grub.