Kernel Compilation In Ubuntu

This will guide you to compile a new kernel from for an Debian or Ubuntu machine. This worked for me in both my Debian and Ubuntu without any glitch. The methods used are the safest ones available.

Actually, there are two methods by which you can do this. One method is to end up the compilation to give you a .deb file which can be saved and used again and again. Other method is just to get the new kernel installed, but if you want to do this on another machine with same configuration or after a OS re-install, this will be time taking.

I am following the method of building a deb file. So here you go.

First of all, we need to gather some essential packages for kernel compilation. For that, just take a terminal and type the below line in there.

# apt-get update
# apt-get install kernel-package libncurses5-dev fakeroot wget bzip2 build-essential

Once complete, we can download the kernel from Wget is a command line utility which can be used to download files. This is more convenient to using Graphical download managers. So I am following this method. In the terminal, type this:

# cd /usr/src
# wget

We we did in the above lines was to enter the /usr/src directory and then download the compressed kernel file (linux- from the location specified above. Once the download is complete, the file needs to be uncompressed. To do that, just specify the command given below:

# tar xjf linux-
# cd /usr/src/linux

Usually you can start at this point, but if you need to get some patches applied to your Kernel, then here is how you do that. Once you download the patch, lets call it 'patch.bz2', execute the command below.

# bzip2 -cd /usr/src/patch.bz2 | patch -p1 --dry-run

Here the option, "dry-run", actually prints the the results of applying the patches without actually changing any files. Once you are satisfied with the above result, you can do that for real by executing the same command without the "dryrun" option.

# bzip2 -cd /usr/src/patch.bz2 | patch -p1

That should patch your kernel. Now we can proceed with the Kernel Configuration.

Its better to customize your kernel from your existing config file that to create a new one from scratch. In debian based systems, the config file location will be /boot/config-'uname -r', where 'uname -r' will give you the current kernel version. So first copy the kernel config file from that location to the path where you extracted the Kernel source.

# cp /boot/config-`uname -r` /usr/src/linux/.config

Now we can start compiling the kernel. To do that, execute the below commands.

# make clean && make mrproper
# make menuconfig

The first command above will clear all temporary files. The menuconfig will bring up an 'ncurses' menu where you can customize the kernel.

To do a minimum customization, just select the second last option "Load an alternate configuration file", which will bring up a text entry box which has .config already selected. Just select OK which will get you back to the previous menu. There choose exit. Then you will be asked whether it should save the settings to the current config file. Select YES and proceed.

NOTE: Please note that, if you have a dual core system like Intel Core2Duo or AMD X2, then you can use the dual cores to increase the performance of the kernel compile to a remarkable level. Normally this can be achieved using the following command.

# make -j5

The value 5 I got there is for a dual core system. Here is how its calculated. 2 x (number of cores) + 1. So for dual cores, its 5. For quad cores, its 9 etc.

But as you see below, we are not using the command 'make' for building the kernel instead, we use make-kpkg. For this command, the -j parameter is not supported. But you can export the concurreny level as 5 and then start the compilation. To accomplish, give the command mentioned below.


Please note the number 5 is for dual cores as mentioned earlier. Change that according to your specification.

Now that should help you build up a new kernel with the configurations of your currently installed system. Now to build the kernel, execute the following commands.

# make-kpkg clean
# fakeroot make-kpkg --initrd --append-to-version=-<-custom_name> kernel_image kernel_headers

This custom_name above can be replaced with the name you want to appear at the end of the kernel. You can give any name for that option, but it should begin with a hyphen symbol (-).

Now sit back and relax. The time taken for compilation may vary from 7 minutes to 45 minutes depending on your system configuration. As we provided the 'make-kpkg' command, it will compile the kernel and will get you two .deb files. One will be linux-image-*.deb and other will be linux-headers-*.deb. This can be installed just like your other programs.

Anyway, once the compilation is finished, you will find the two .deb files as mentioned above in the folder just above your current location. If you followed the path given in this documentation, it will be located under /usr/src/ directory.

You can install them like this:

# dpkg -i linux-image-*.deb
# dpkg -i linux-headers-*.deb

The advantage in using this method is that, the .deb files that resulted, can be transfered to another machine having a Debian based system with same configuration and can be installed right away. Like, for a company or an Internet cafe, almost all systems will be of exactly same configuration. So the compilation procedure on each machine can be avoided. Also, even if a re-installation of the OS is required, the kernel can be installed with in minutes.

Now you can reboot the system and stop at the grub. There you will see the entry for the new kernel you complied with the custom_name you have given. Just select that and boot up. If all goes fine, then you will be able to taste the performance boost of your system with the new kernel. :)

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.