HowTo: Compile Linux Kernel on Ubuntu

Current stable kernel version: 4.14.11

Current development kernel version: 4.15-rc6

 

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 famouse for updateing 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 qt4-dev-tools libqt4-dev

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.

cd KERNEL
wget -c https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.14.11.tar.xz && tar -xvJf linux-4.14.11.tar.xz

5) Now we will enter directory linux-4.14.11:

cd linux-4.14.11

 

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 essentionaly 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=3 make-kpkg –initrd –append-to-version=-custom kernel_image kernel_headers

ATTENTION: CONCURRENCY_LEVEL is the option that is usefull if you have multicore CPU. The number after “=” represents Cores+1, so if you have dual core CPU, you will write 3, and if you have quad core CPU, you will write 5, etc.

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-4.14.11-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.