Obtaining, Compiling and Installing the Linux Kernel

Compliling your own Kernel can be very frustrating if you don't know what you are doing. I have done it numerous times yet I always manage to forget a step or two along the way, so I've created this quick and dirty guide to remind myself (and hopefully remind/enlighten you) as to the process.

There a few things that you will need before you can begin the process of compiling and installing a new kernel. You will need to ensure that you have the right compilers and sources installed in order to compile.

Installing the compiler

The compiler we need for this is called gcc, the GNU C Compiler and is used to compile a large number of projects in Linux, but it is still something you need to go out of your way to install, like so:

sudo apt-get install gcc

Note that apt-get should be used for debian based distros only. For other systems just replace apt-get with your relevant package manager, eg yum, aptitude etc...
Installing the sources

Next you will need to install the sources, this can be done similar to the compiler with

sudo apt-get install linux-source

Once you have both of these steps done, it's time to download the kernel. You can do this a number of ways, the first is navigating to kernel.org and downloading the latest archive. Another is to use wget.

wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-x.y.z.tar.bz2

Replacing x y and z with the version number of the kernel. eg 2.6.31 was the latest version available at the time of this post. Once you've done this you will need to extract the archive, so navigate to where ever you have downloaded the archive to and run:

tar xivf linux-x.y.x.tar.bz2

This will extract the kernel to a directory, once it is finished navigate into the directory.

Now the fun stuff!
We now have all the ground work needed to begin configuring and compiling the kernel. So, let's do it!.

Once you are in the directory you just extracted you will need to configure the kernel with the options we will be compiling it with.

If you are in a Gnome desktop you can use:
make gconfig

For KDE
make xconfig

Or if you prefer to do everything in the terminal you can use:
make config

All of these commands will bring up a menu of some form that will allow you to pick and choose from a ludicrously large number of options for configuring your kernel. Most of the time the already selected options will be perfectly fine, but if you have time or the interest it may be worth going through the various options and configuring it to be suit your needs and remove any unnecessary modules.

Once you have finished choosing options click or select save, this will save to config file that the compiler will use when building the kernel. If you find yourself compiling your own kernel often, it may be worth keeping a copy of this file so that you don't have to choose the options again everytime.

Next we actually get to build the kernel. This will take a while so it's probably a good idea to run the command and then go and make yourself a cup of coffee.
make
If you have a fairly modern computer you may be able to spend up the process by telling make to create multiple jobs at a time. You can do this like so:

make -j4

Note the flag -j4, this tells make to issue 4 jobs at a time. It's usually a good idea to issue twice as many jobs as your computer has processor cores. So if you have a dual core machine, you should issue 4 jobs, like the above commmand. This should speed things up considerably.
Once the considerable process of building the kernel has finished we will need to build the various modules.
make modules
Install them

make modules_install

And then install the new kernel

make install

Next we need to create an initrd for the new kernel, this will contain various drivers that the kernel will need to load the operating system, this is not always needed, but it is good practice to create one anyway.


cd /boot
mkinitramfs -o initrd.img-x.y.z x.y.z

Again replacing x y and z with the version number of the kernel you are installing.
And finally

We've now finished compiling and installing the new kernel. All we need now is an entry in the boot-loader so that we can boot the OS with the new kernel.


update-grub

Now reboot the computer, select the new kernel from the list and you should be able to boot into your shiny new kernel.