Compile a kernel

From LQWiki
Jump to: navigation, search


Not all Linux distributions come with all of the components needed to build a kernel. You'll need a C-compiler, make, and system header files installed before you can begin to build the kernel.

C compiler

Test if you have a C compiler.

cc -v

Should print

cc: no input files

If it does not, then install gcc.


Test if you have the make program

make -v

The response's first line should be something like

GNU Make 3.81

If it is not, then install make.

kernel sources

Test if you have the kernel sources

ls /usr/src/linux

The response should not be "No such file or directory".

If you get "No such file or directory", install the kernel sources.


It is recommended that you put new kernel sources in a subdirectory of your home directory for example:

mkdir -p /home/$user/src/kernel
cd /home/$user/src/kernel

Obtaining the kernel source

The variable $KV is for the kernel version. Replace x.x.x with the kernel version of the kernel you want to compile:


three ways to get the kernel sources:

From distribution

When installed it usually resides in a /usr/src/linux-x.x.x subdirectory. Link it using:

ln -s /usr/src/linux-x.x.x /home/$user/src/kernel/linux-x.x.x


  • Get the kernel source from Download the kernel sources (linux-x.x.x.tar.gz or linux-x.x.x.tar.bz2).

Extract the sources, using one of:

tar -zxvf linux-$KV.tar.gz 	 
tar -jxvf linux-$KV.tar.bz2

From git

  • It is also possible to get the latest kernel development version using git. If you clone it in '/home/$user/src/kernel, the git repository will be placed in /home/$user/src/kernel/linux-x.x

Enter the created subdirectory

cd linux-$KV

Patch Kernel

If you want to apply an additional patch to your kernel, you should do that before you configure your kernel, see patching a kernel. If you patched your kernel, don't forget to rename the kernel source directory accordingly.

Building the kernel

It is recommended that you do not build in your kernel source directory.

Preparing to build

Create another directory in your home directory:

mkdir -p /home/$user/build/kernel/$KV/

If this is not the first time you compile the kernel you'll have to clean up your build directory:

cd /home/$user/build/kernel/$KV
make mrproper

Obtaining a configuration

The running kernel mostly stores its configuration in /proc/config.gz. To use this, do:

zcat /proc/config.gz > /home/$user/build/kernel/$KV/.config

If you're going to upgrade to a newer kernel, first do:

make oldconfig

For ncurses GUI in the terminal

make menuconfig
Most options are about specifying whether you want a feature [*] compiled into the kernel image, [M] compiled as a module, or [ ] not compiled at all. Do not compile your hard drive and file system type as a module - these must be compiled in the kernel [*], e.g. " [*] ReiserFS".
Here you to specify an enormous number of features. It is advisable to skim through all the sections to get a feel for the different things you can do. The kernel configuration is one LINUX program that offers lots of help--select < Help > on any feature. The raw help file is /usr/src/linux/Documentation/ can also be worth reading. See also configuring linux kernel.
When you're done with the config, click exit and save current configuration. Your file is now known as .config .

Extra cleaning for pre 2.6 kernels

This can be skipped for 2.6.x kernels. First ensures that dependancies such as include files are in place, then clean your sources so they compile correctly.

cd /home/$user/build/kernel/$KV
make dep
make clean

compiling kernel and modules

make the kernel image (compile and creates compressed image of kernel). This may take a while...

cd /home/$user/build/kernel/$KV/
make -j4

make the modules

make -j4 modules


for the remainder you need to be root:


To install the newly compiled modules to /lib/modules/$KV/:

cd /home/$user/build/kernel/$KV
make modules_install

Move kernel and related files

In this chapter we assume you are on an X64 computer (find out your computer architecture). And we assume you are compiling kernel 2.6.33. Different kernels have different names for the computer architecture. Early kernels put the kernel for i386 architecture into arch/i386, new kernels use arch/x86.

Remove the following links:

rm -rf /boot/
rm -rf /boot/vmlinuz

Then copy the newly created kernel and to /boot

cp /home/$user/build/kernel/$KV/arch/x86_64/boot/bzImage /boot/vmlinuz-$KV
cp /home/$user/build/kernel/$KV/ /boot/$KV

Make the new links:

ln -s /boot/vmlinuz-$KV /boot/vmlinuz
ln -s /boot/$KV /boot/ 

Next, if present, remove existing initrd.img file:

rm -rf /boot/initrd-$KV.img

And create the new one:


Install a boot manager

Use the steps that pertain to you

  • if you use grub then edit grub.conf
  • if you use lilo then edit lilo.conf
remember to leave the entry to your old kernel image just in case, so modify what is in the config originally to point to the old image and make a new entry for you new image you just made...

You can determine your root partition with:

ROOTDEV=`mount -l | grep " \/ " | cut -d" " -f1`


Note today most distros now use menu.lst, so if you can't find grub or lilo, then you know what to look for.

title New Kernel :D
kernel /vmlinuz-$KV ro root=$ROOTDEV initrd /initrd-$KV.img
Look at the previous parameters in the grub.conf file and note what "root=" and use what is existing.

Exit and save grub.conf, and if you want (is not mandatory) type



Append the kernel as boot option to your /etc/lilo.config:

cat << EOF >> /etc/lilo.conf
  image = /boot/vmlinuz-$KV
          label = vml-$KV
          root = $ROOTDEV

install this lilo.conf (if listed, resolve errors before rebooting!):


Propietary nvidia module

For some it is required to install proprietary modules. This can be done after booting, see NVIDIA graphics drivers, or directly from the kernel directory after installation of the kernel if you're upgrading to a newer kernel version, by running as root:

/path/to/NVIDIA-Linux-* -a --kernel-name="`grep -o "2\..\.[0-9]*.*$" .config`" \
--no-x-check --no-runlevel-check --kernel-module-only -s;

See also