How to: Compile Linux kernel 2.6

by nixcraft · 66 comments

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.

Featured Articles:

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!

{ 66 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…)

44 mavgh1 07.07.09 at 12:09 am

Great Tutorial .I have E8200 , Gigabyte EP43-DS3L kernel 2.6.18 that does not recognize coretemp,I compiled 2.6.30.1 kernel and it’s working.Thanks

45 Max Baird 07.13.09 at 2:44 pm

Hey this was great, worked for me… I had some small issues where I was missing some files like ncurses etc, but after getting them it all flowed smoothly… thanks alot again :)

46 Kousik 07.20.09 at 8:33 am

This is nice documentation. But I think some modification is required.

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
Instead of mkinitrd I use mkinitramfs in ubuntu 9.04

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

update-grub is not required in ubuntu 9.04

47 Lady 09.10.09 at 9:47 am

Can any body describe to me next: about the process0, process0 which happened in Linux kernel 2.6,
1. describe how to greate it
2. describe what they do

48 Sivakumar 09.23.09 at 1:08 am

Hi
I am using ubuntu distribution, I like to update my kernel , my present version kernel is 2.6.28-11-generic #42-Ubuntu SMP Fri Apr 17 01:57:59 UTC 2009 i686 GNU/Linux.
i downloaded latest kernel source from kernel.org “linux-2.6.31.tar.bz2″ i followed the following steps to configure my kernel

1. i extracted the kernel source file in usr/src
2. I updated my gcc complier

root@sivakumar-desktop:/usr/src# apt-get install gcc
Reading package lists… Done
Building dependency tree
Reading state information… Done
gcc is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 228 not upgraded.

3. Then i tried to run make menuconfig, i got the following error

root@sivakumar-desktop:/usr/src# make menuconfig
make: *** No rule to make target `menuconfig’. Stop.

4. then i tried

root@sivakumar-desktop:/usr/src/linux-2.6.31# make menuconfig
*** Unable to find the ncurses libraries or the
*** required header files.
*** ‘make menuconfig’ requires the ncurses libraries.
***
*** Install ncurses (ncurses-devel) and try again.
***
make[1]: *** [scripts/kconfig/dochecklxdialog] Error 1
make: *** [menuconfig] Error 2

49 saki 09.28.09 at 7:09 am

i am also getting the same problem …….plz suggest

50 Mukesh 10.11.09 at 1:26 am

after extracting there is some error when i configure the kernel
that error is make: *** No rule to make target ‘menuconfig’, Stop.

51 Mukesh 10.11.09 at 1:29 am

Hello,
Can Any One Solve My Problem That Is
after extracting there is some error when i configure the kernel
that error is make: *** No rule to make target ‘menuconfig’, Stop.
plz tell me the fast it is urgent friends….
plz

52 Redd 10.13.09 at 2:37 pm

Mukesh – seems like you need to cd into the extarcted directory to run the make.

53 Redd 10.13.09 at 2:39 pm

Has anyone made a suggestion as to how to fix the kernel panic issue that was pointed to several time here? I have the same issue. After installing the new kernel and trying to boot into it, I see the kernel panic since it doesn’t see any of my drives ( “/”, “/usr”, etc). Anyideas where I went amiss?

54 Raj 10.30.09 at 4:10 am

Hi!!
i am using Redhat 4 – kernel 2.6.9 and now i build kernel 2.7.13. it was sucessfully and after reboot i can see my new kernel in the grub.. but when i select .and proceed . i am getting the following error:

“Enforcing mode requested but no policy loaded. Halting now.
Kernel panic – not syncing: Attempted to kill init!”

please tell me wht might the problem and suggest me how to load the kenel 2.7.13..
Thanx in advance….

55 GARG 11.06.09 at 1:10 pm

problem occur on booting…
mount: unknown file system type ‘devfs’
Kernel panic – not syncing: Attempted to kill init!”

56 ben 11.11.09 at 4:17 am

In the process make install .there are two files created in the direction /boot
Kernel image: /boot/vmlinuz-2.6.31.4-smp
Initrd image: /boot/initrd-2.6.31.4-smp
However,One file config is missed.
I update the menu.list
Then ,reboot.
On the way booting,there is no error.
but when I inter the X-window ,my keyboard and mouse doesn’t work.

57 mahdi 11.24.09 at 9:39 pm

Hi everybody,
I have followed all the steps above and succeeded in every step. But when I choose in the grub to run the 2.6.31.6 kernel I have the following message:
Unable to find a suitable fs in /proc/mounts, is it mounted ?
Use –subdomainfs to override.

I have installed the linux kernel from Ubuntu 9.10. I am not sure, but I think because I have installed ubuntu with ext4 file system, the kernel could not recognize it. The kernel is residing in /usr/src which is in the same partition as ubuntu.

Have anyone an idea out there ?
Thx,

58 bonz 12.03.09 at 10:49 pm

if you want people to do your survey you should make it accessable for us lynx users

59 Noober 12.06.09 at 6:12 am

I need help.I follow stepbystep all is done.And but at the reboot say “error 16: inconsistent filesystem structure” what wrong whit it? My kernel is 2.6.24-16 want to upgrade kernel 2.6.31.6 plz help me thx

60 Azade 12.24.09 at 5:18 pm

Does it work for SUSE?

61 vaidya1 01.13.10 at 8:22 pm

hey guys…….can anyone help me……….in my /boot i am not getting config file for newly compiled and installed kernel and also after successful installation of kernel.2.6.32 , i am not able to boot from this kernel as it says file system not detected , /dev/hda not detected……..and kernel panic……….

62 BongoBongo 01.16.10 at 9:30 pm

Nice.

Came in handy as I needed to recompile kernel in order to remove KVM virtualization support. VirtualBox won’t run with KVM modules loaded.

Why am I telling you this?

63 DaVince 01.28.10 at 3:12 pm

Some of the instructions are kind of outdated on modern systems, could you perhaps update the article?

- Some systems now use GRUB 2, which doesn’t have a menu.lst.
- Modern systems tend to use mkinitramfs or yaird, not mkinitrd.

Thanks a lot for the article, though. Off to test out my new kernel.

64 ahmeds 02.01.10 at 2:32 pm

This article actually very usefull. It helped me to kompile linux kernel after my resultles
attemtions for a long period. The only issue I had was creating initrd image.
My Ubuntu 8.10 interpid (also 8.4 hardy) do not have mkinitrd command so instead
I used next steps to compile linux kernel(2.6.27.7).Here they are.
1. first copy your /boot folder to somewhere , home folder
2. go to top of your linux source tree and run(root prev required)
#make-kpkg clean && make-kpkg –initrd –revision=custom.1.0 kernel_image
and after its finished with no errors run
#dpkg -i .deb
the latest command will update your /boot also /boot/grub/menu.lst file , but this is not what
do we want , and after reboot you may find your pc not booting at all.
what do we need is new kernel and new initrd image .
our new kernel is vmlinuz-2.6.27.7 and image is initrd.img-2.6.27.7
so we have to copy these files to some temp folder , then restore our old /boot folder
from backup and copy back again our fresh kernel and image from temp folder to /boot.
to finish we need edit /boot/grub/menu.lst file.
here we can simply copy our generic boot items and make no change except names
of kernel and image to new ones. reboot and enjoy.

65 vig 02.01.10 at 11:20 pm

it really worked .. clean .. thanks .. a lot

66 Danny 02.03.10 at 11:54 am

> Step # 2 Extract tar (.tar.bz3) file
I lol’d.

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>

Previous post:

Next post: