How to: Compile Linux kernel 2.6

Compiling custom kernel has its own advantages and disadvantages. However, new Linux user / admin find it difficult to compile Linux kernel. Compiling kernel needs to understand few things and then just type couple of commands. This step by step howto covers compiling Linux kernel version 2.6.xx under Debian GNU Linux. However, instructions remains the same for any other distribution except for apt-get command.

Step # 1 Get Latest Linux kernel code

Visit http://kernel.org/ and download the latest source code. File name would be linux-x.y.z.tar.bz2, where x.y.z is actual version number. For example file inux-2.6.25.tar.bz2 represents 2.6.25 kernel version. Use wget command to download kernel source code:
$ cd /tmp
$ wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-x.y.z.tar.bz2

Note: Replace x.y.z with actual version number.

Step # 2 Extract tar (.tar.bz3) file

Type the following command:
# tar -xjvf linux-2.6.25.tar.bz2 -C /usr/src
# cd /usr/src

Step # 3 Configure kernel

Before you configure kernel make sure you have development tools (gcc compilers and related tools) are installed on your system. If gcc compiler and tools are not installed then use apt-get command under Debian Linux to install development tools.
# apt-get install gcc

Now you can start kernel configuration by typing any one of the command:

  • $ make menuconfig - Text based color menus, radiolists & dialogs. This option also useful on remote server if you wanna compile kernel remotely.
  • $ make xconfig - X windows (Qt) based configuration tool, works best under KDE desktop
  • $ make gconfig - X windows (Gtk) based configuration tool, works best under Gnome Dekstop.

For example make menuconfig command launches following screen:
$ make menuconfig

You have to select different options as per your need. Each configuration option has HELP button associated with it so select help button to get help.

Step # 4 Compile kernel

Start compiling to create a compressed kernel image, enter:
$ make
Start compiling to kernel modules:
$ make modules

Install kernel modules (become a root user, use su command):
$ su -
# make modules_install

Step # 5 Install kernel

So far we have compiled kernel and installed kernel modules. It is time to install kernel itself.
# make install

It will install three files into /boot directory as well as modification to your kernel grub configuration file:

  • System.map-2.6.25
  • config-2.6.25
  • vmlinuz-2.6.25

Step # 6: Create an initrd image

Type the following command at a shell prompt:
# cd /boot
# mkinitrd -o initrd.img-2.6.25 2.6.25

initrd images contains device driver which needed to load rest of the operating system later on. Not all computer requires initrd, but it is safe to create one.

Step # 7 Modify Grub configuration file - /boot/grub/menu.lst

Open file using vi:
# vi /boot/grub/menu.lst

title           Debian GNU/Linux, kernel 2.6.25 Default
root            (hd0,0)
kernel          /boot/vmlinuz root=/dev/hdb1 ro
initrd          /boot/initrd.img-2.6.25
savedefault
boot

Remember to setup correct root=/dev/hdXX device. Save and close the file. If you think editing and writing all lines by hand is too much for you, try out update-grub command to update the lines for each kernel in /boot/grub/menu.lst file. Just type the command:
# update-grub
Neat. Huh?

Step # 8 : Reboot computer and boot into your new kernel

Just issue reboot command:
# reboot
For more information see:

  • Our Exploring Linux kernel article and Compiling Linux Kernel module only.
  • Official README file has more information on kernel and software requirement to compile it. This file is kernel source directory tree.
  • Documentation/ directory has interesting kernel documentation for you in kernel source tree.
Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 43 comments… read them below or add one }

1 Anonymous 03.07.06 at 1:40 am

According to the kernel documentation, the kernel should be compiled using gcc 2.95.x, with x >= 3. “apt-get install gcc” under debian gives you a recent gcc (version > 4).

2 nixcraft 03.07.06 at 9:52 am

It works with gcc version >= 4.0.
gcc –version
gcc (GCC) 4.0.3 20060212 (prerelease) (Debian 4.0.2-9)

uname -a
Linux debian 2.6.15.4 #1 PREEMPT Fri Feb 17 20:46:04 IST 2006 i686 GNU/Linux

I have compiled above kernel using gcc 4.0.2-9. INSTALL file may be not updated.

3 Anonymous 03.17.06 at 9:47 pm

Thanks a lot Gabber Anna,
It’s works for me :-))

4 nixcraft 03.17.06 at 11:25 pm

LOL
Note sure who are you?
But you bring back some old memories…

Take care buddy :)

5 Learn Hacking 03.25.07 at 7:57 am

any guess about boot loader?

6 debianusr 08.18.07 at 5:57 pm

After building the kernel and updating grub I’ve rebooted the new kernel successfully. Now I am wondering if this process includes updating the kernel header files in /usr/include/. If not, what is the best way to do that and should any new directories be added to ld.so.conf so I can update glibc?

7 vivek 08.18.07 at 6:55 pm

If you also upgraded kernel-devel and headers package you should get updates. No need to upgrade ld.so.conf or cache

8 Animesh 10.13.07 at 4:59 pm

Info
—–

Kernel 2.6.23 has issues with kernel headers. If you have distro’s like fedora it gonna give up some wierd fixdep.c error messages.

Some forums say you need to have 2.4 headers for compiling it. Kernel header is a huge mess scattered around and well serious debates have gone down the drain…

http://kerneltrap.org/node/6536

9 Pradeep 04.30.08 at 3:04 pm

Does this kernel support RHEL4 update 5

I am using kernel 2.6.9-55.EL can i upgade the

10 dushyant 05.09.08 at 11:50 am

How much time does it take approximately?

11 Krishna Priya Bandyopadhyay 05.15.08 at 6:45 pm

i am trying it on mandriva 2008.i can not do “$ make menuconfig”.is there any option to do that.please help me to get out of this as soon as possible.i wonder there is no option of “make”.

12 Pothikan Suresh 06.04.08 at 8:05 am

Priya,
To make ‘make’ work, go inside the kernel directory ie., /usr/src/. There, u can find the make file. Now, just give “$make menuconfig”. It will work…. :)

13 Blues 06.04.08 at 11:28 pm

With 2.6 kernel there is no need for make modules. make builds kernel and modules. Have you ever read the README in kernel source directory?

14 Anil Kumar Jena 06.24.08 at 8:38 pm

It will take near about 1 to 1.30 hours. for complete
installation

15 Lin 06.25.08 at 9:38 am

I tried make 2.6.25 in my desktop (FC5, kernel version 2.6.15), everything goes okay, but failed to boot the new kernel image with the follow error messages:

PCI: BIOS Bug: MCFG area at e0000000 is not E820-reserved
PCI: Not using MMCONFIG

Read Hat nash version 5.0.32 Starting
mount: error mounting /dev/root on /sysroot at ext3: No such device
….

In initrd.img file, I found no libata.ko or any other .ko modules are added before mkrootdev.

Is this problem caused by incorrect or even no HDD controller driver loaded? I did not see appropriate entry for libata in .config file. Do you guys have any suggestion on make it through?

Thanks.

-Lin

16 Logic 07.08.08 at 12:22 am

About the 4th step, as the “make help” says: you do not need to do make modules again, so when using “make” it already compiles the modules.

:D

17 Ramkey 10.09.08 at 11:22 pm

Lin,

Did your problem sorted out?Thans

18 Rohit Awasthi 10.10.08 at 1:24 pm

Hi.. wt if somethin goes wrong… is there any way to get OS to its former stage, say ( backup or uninstall Kernel or something ).. or will need to install whole OS again…

19 Ravi 10.31.08 at 5:42 am

I followed the above steps in order to compile Linux kernel “linux-2.6.27″ but when tried to boot my system from latest compiled kernel got the Kernel Panic error which says can’t find
file system /boot/root at the start.

please suggest

20 Jared 11.02.08 at 9:12 pm

This is a nice tutorial, but I have a note to make. On my installation, Ubuntu 8.04.1 64-bit, the command in Step # 6 is “mkinitramfs” instead of “mkinitrd”.

Can’t wait to try out my new kernel…

21 same 11.02.08 at 10:45 pm

the same thing happened to me as ravi.

22 Roman 12.07.08 at 9:20 am

I have compiled custom kernel and I am missing in /boot directory config-2.6 file. What have I done wrong? I see config files from old kernel there. thanks in advance.

23 saga 12.14.08 at 3:43 pm

you have compiled a upstream kernel.How should i compile distribution specific kernels,like if i install fedora it has its kernel source in /usr/src/, fedora community might have added its own patches.Will the steps described in this article work for distribution specific kernels?

24 anand 12.21.08 at 4:06 am

Except for step where you create the initrd img file, rest of the steps explained here worked fine. The Kernel version I tried was 2.6.27.10 and the linux box had ubuntu 8.10 installed.

On ubuntu if you try to use mkinitrd you might get the message “No command found”. Try using mkinitramfs instead. It worked perfectly fine.

25 Maker 12.22.08 at 5:51 pm

There is no such thing as bz3.

26 Axon 12.28.08 at 7:57 am

It’s cool.
Thanks alot NIXTCRAFT.

27 Carnes 12.29.08 at 9:53 am

I tried to cheat and copied over an older .config.. broke something and had to redo all the options by hand : /

Anyways, the instructions worked great with Gentoo. I had to emerge “mkinitrd” but otherwise everything went very quickly, maybe 20 min to compile everything (very short in Gentoo world).

I suggest everyone keep their old kernel and a lilo/grub option for it just incase :) Most of the problems in the comments seem to be from misconfiguring the kernel. If you aren’t sure what your hardware is or don’t have the time to run through every option. I suggest you pop in a live cd and copy “/proc/config.gz” to use as your base config (”gzip -d config.gz” will uncompress it).

If your system is already unusable, pop in a live cd and try the following as root:
“cd /”
“mkdir /mnt/fixme”
“mount /dev/ROOTDRIVE /mnt/fixme”
“mount /dev/BOOTDRIVE /mnt/fixme/boot”
“mount -t proc proc /mnt/fixme/proc”
“chroot /mnt/fixme /bin/bash”

That should put you back into your computer to compile/fix whatever.

28 benny lootens 01.04.09 at 2:40 am

i’ve just build the 2.6.28 kernel on ubuntu 8.04 LTS and it’s working like a charm. It was required, as I had some troubles installing my 500 GB Samsung drive that was attached to a Trust eSATA IF-3300 card. Thanks for this great tutorial !!

29 Otto 01.06.09 at 6:41 pm

You don’t need make modules anymore. I compiled a few times the last days and just make && make modules_install will do.

30 Akash 01.07.09 at 12:59 pm

Following error occured during make command of compilation step for linux 2.6.23
make: *** [.tmp_vmlinux1] Error 1

Plz provide me solution for it.
Thanks
Akash

31 Akash 01.07.09 at 1:06 pm

make command error:
make: *** [.tmp_vmlinux1] Error 1
Kindly suggest solution
regards
————-
Akash

32 Pekka 01.14.09 at 11:47 am

Hi

On Debian the kernel compile is much more straightforward than this describes.

First you of course need to take the kernel source. Then:

1. make menuconfig (or whatever you want)
2. make-kpkg –revision kernel_image
3. Install the resulting .deb package using dpkg.

The debian package management system then knows about your custom kernel and it handles all needed actions to get the kernel properly working. For example initrd is done automagically if needed.

I compile the root file system and the way to access it directly into kernel and customize the kernel sharply to the computer. That way you wont need initrd anymore :) and the machine boots quite fast, because the chipset drivers are active from wery beginning of booting.

33 komal 02.07.09 at 4:05 pm

I compiled the whole thing and then booted ..I see the updated version of fedora but when I try to run it ..it says kernal panic and doesn’t proceed any further

34 Anonymous 02.17.09 at 9:04 am

If you have multiple CPU-kernels, you’d consider using the -j option for make, for example make -j5 , which will use 5 instance of make to built the process. This will speed up things, nice tip I got on IRC :)

35 kontinumas 02.18.09 at 7:37 pm

Hello!

got some problems while compiling new kernel using make. Error below:


{standard input}: Assembler messages:
{standard input}:745: Error: suffix or operands invalid for `mov'
{standard input}:746: Error: suffix or operands invalid for `mov'
{standard input}:950: Error: suffix or operands invalid for `mov'
{standard input}:951: Error: suffix or operands invalid for `mov'
{standard input}:1024: Error: suffix or operands invalid for `mov'
{standard input}:1025: Error: suffix or operands invalid for `mov'
{standard input}:1120: Error: suffix or operands invalid for `mov'
{standard input}:1121: Error: suffix or operands invalid for `mov'
{standard input}:1208: Error: suffix or operands invalid for `mov'
{standard input}:1220: Error: suffix or operands invalid for `mov'
make[1]: *** [arch/i386/kernel/process.o] Error 1
make: *** [arch/i386/kernel] Error 2

gcc version 4.1.2
debian etch

got some ideas? thanx

36 trevor 02.19.09 at 9:10 am

a very good clarification about kernels,i’m impressed!

37 ondrg 03.18.09 at 10:04 am

Thanks, it works perfectly.

38 nugga 04.04.09 at 9:31 pm

hello when i try to boot my kernel it says error file not found.

i use the following command
title Debian GNU/Linux, kernel 2.6.25 Default
root (hd0,0)
kernel /boot/vmlinuz root=/dev/hdb1 ro
initrd /boot/initrd.img-2.6.25
savedefault
boot

im pretty noob i dont know what im doing wrong.

plzz contact me or post here.

39 nugga 04.05.09 at 10:16 am
40 Hussain Jaffar 05.05.09 at 3:16 am

37 ondrg, Which gcc version you used ?

Can any body tell that How to remove a previously compiled and installed kernel. So as to make space for compiling and installing a new kernel ?

41 Acevedo 05.07.09 at 6:17 pm

NIce step-by-step process, BUT Following error occured during make command of configuration step for linux 2.6.29.2
make[1]: *** [script/kconfig/dochecklxdialog] Error 1
make: *** [menuconfig] Error 2

Plz provide me solution for it.
Thanks
J.Acevedo

42 Anitha 05.12.09 at 10:22 am

Hi… Kindly help me please… i m in serious trouble…
I m trying to install RTLinux on Linux 2.6.xx
The installation guide specified to patch the linux 2.6.xx, configure and then compile. But
i m able to proceed til the configuration of the kernel…
When i give the make command, i m coming up with errors…
Also i tried to install linux 2.6.xx without patching it with RTLinux,again i m struck up while compiling……

Kindly help please…Wil be grateful to you…

43 chindam 05.29.09 at 5:45 am

It’s good, but i need your help to compile;boot from a flash drive and configure options such that the size of the kernel is max 5Mb(minimum application like terminal,usb…)

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Tagged as: , , , , , , , , , , , , , ,

Previous post: Knoppix 4.0.2 DVD Released

Next post: The importance of Linux partitions