Compile and Install a Kernel in Ubuntu

The Linux kernel is the operating system kernel used by the Linux family of Unix-like operating systems . It is one of the most prominent examples of free and open source software. Ubuntu is a computer operating system based on the Linux distribution and distributed as free and open source software, using its own desktop environment.

Why upgrade the kernel?

There are several reasons to upgrade the kernel. One is to take advantage of a specific new feature or driver; another is to be protected against a security vulnerability, or just to maintain an up-to-date and healthy system. So, from time to time it may be wise to upgrade your Linux kernel. Even if you choose not to update to every new kernel revision, it is recommended that you at least upgrade from time to time. It is strongly recommended that you immediately upgrade to a new kernel if that new release solves a security problem.

Upgrade the kernel

This tutorial has been tested on Ubuntu 11.04 (Natty Narwhal) ,Ubuntu 11.10 (Oneiric Ocelot) and Ubuntu 12.04 LTS (Precise Pangolin). NOTE: All the commands below are to be run without quotation marks.

1. Install Required Packages for Kernel Compilation

sudo apt-get install kernel-package libncurses5-dev fakeroot wget bzip2

2. Download the Kernel Source

Go to www.kernel.org and select the latest stable kernel you want to install. Here, I’ve downloaded linux-3.3.7.tar.bz2. Download it to

/usr/src

Un-tar the kernel sources and create a symlink linux to the kernel sources directory (by typing):

tar xjf linux-3.3.7.tar.bz2
ln -s linux-3.3.7 linux
cd /usr/src/linux

3. Configuring the kernel

Configuration files are needed for compiling the kernel. You can create your own config file if you know how to do it or use the default one. For beginners, it’s a good idea to use the configuration of your current working kernel as a basis for your new kernel. Therefore, we’ll copy the existing configuration to /usr/src/linux :

cp /boot/config-`uname -r` ./.config

Then type :

make menuconfig

which brings up the kernel configuration menu. Go to Load an Alternate Configuration File’ and choose .config (which contains the configuration of your current working kernel) as the configuration file:

Then browse through the kernel configuration menu and make your choices. When you are finished then select Exit, answer the question Do you wish to save your new kernel configuration?’ with Yes.

4. Building the Kernel

To build the kernel, execute these two commands:

make-kpkg clean
fakeroot make-kpkg –initrd –append-to-version=-custom kernel_image kernel_headers

Note: custom’ in the above command is the name given to new kernel. You can name the kernel as you wish. Just replace word custom by the name you want. This name must begin with a minus (-) and must not contain whitespace.

Kernel compilation can take time depending on your processor speed and your kernel configuration.

5. Installation of the Kernel

After the successful kernel build, you can find two .deb packages in the /usr/src directory.

cd /usr/src
ls -l

On my test system they were called linux-image-3.3.7-custom_3.3.7-custom-10.00.Custom_i386.deb (which contains the actual kernel) and linux-headers-3.3.7-custom_3.3.7-custom-10.00.Custom_i386.deb (which contains files needed if you want to compile additional kernel modules later on). Install them by typing:

dpkg -i linux-image-2.6.18.1-custom_3.3.7-custom-10.00.Custom_i386.deb
dpkg -i linux-headers-2.6.18.1-custom_3.3.7-custom-10.00.Custom_i386.deb

It’s done. Now, change the grub menu:

You can find the menu in /boot/grub/ folder. You must find the menu to be modified.

vi /boot/grub/menu.lst   or vi /boot/grub/grub.cfg files.

Now reboot the system:

shutdown -r now

If everything goes well, the grub menu will show a new entry for the installed kernel. Select this option. You can check if it’s really using your new kernel by running:

uname -r

This should display something like:

3.3.7-custom

Select your old kernel and start the system. You can now try again to compile a working kernel. Don’t forget to remove the two stanzas of the not-working kernel from /boot/grub/menu.lst or /boot/grub/grub.cfg.

Compiling and installing a new kernel is an easy task if it’s done step by step. Follow the procedure as directed and you’ll be able to easily compile your own version of Linux kernel in your Ubuntu. It can also work in Ubuntu’s derivative distributions like Linux Mint, Crunchbang, etc. But, I’ve not tested it on those systems, so you must take responsibility before trying it out.