How to Compile the Linux Kernel

6202

 

Compiling the Linux kernel used to be a rite of passage, or just a necessity, for running Linux. It’s not something most users need to do any more, but if you really want to compile your own kernel we’ll show you how.

Even though it’s not necessary, a lot of users still want to know how to compile the Linux kernel. Unfortunately, there’s not just one way to do it. I’ll show one way to build a kernel from source, but you may want to consult your distribution’s documentation for the proper way to build from source for your distribution. Here we’re going to build from source and install directly without creating an RPM or Debian package for the kernel. If there’s a lot of demand, we’ll do specific tutorials on creating kernels for Ubuntu and Fedora.

Word of Warning

When I first started using Linux in 1996, it was almost mandatory to compile your own kernel. Thankfully, things have change a lot since then. It’s very rare that you need to compile your own kernel these days, unless you really know what you’re doing (in which case, you probably don’t need this tutorial), want to help with kernel testing, or run into a very unusual situation.

You may need to compile modules, which is somewhat different. Because support for devices can be compiled as modules rather than requiring a kernel rebuild, you can simply compile modules and insert them into the existing kernel.

If you want to help with kernel testing, great! If you have a few hours and can compile and boot a kernel (most problems show up right away), then you’ll be doing the Linux community a favor by testing it out. If you’re wanting to run a custom kernel on your desktop, laptop, or servers, that’s fine — but don’t expect your distribution vendor or project to support it or accept bug reports about a kernel problem unless you’re running the stock builds that they provide. If you’re compiling a kernel just because you can, that’s great — but don’t expect any technical support!

Finally, the kernel you get from your distribution and the kernel that comes from kernel.org are generally not going to be the same thing — even if they’re the same version number. This is because almost every distribution ships a modified Linux kernel with their own patches and tweaks.

Getting Ready to Compile

Before compiling the kernel, you have to have the development tools and you’ll need to create a configuration file that specifies what should (and shouldn’t) be compiled with the kernel. Also, plan to have an hour or more for the whole exercise, depending on how fast your machine is. If you’re compiling a kernel on a quad-core Core i7 with loads of RAM, it’ll go much faster. If you’re compiling the kernel on an Atom-based netbook with 1GB of RAM, it’s probably not going to be very speedy — though probably still much faster than the Pentium 133MHz system I compiled my first kernel on.

If you’re new to the whole “compiling a kernel” thing, I strongly recommend that you do not do this on a production machine. If you have VMware or VirtualBox installed, use those to compile your custom kernel a few times. If you have a spare box, use that. If you are going to compile the kernel on your production desktop, server, laptop, or netbook the first time (against my advice) make sure you have a backup and a rescue disk handy just to be on the safe side. Odds are you won’t need them, but it’s always better to have backups and such and not need them than the converse.

Finally, you’ll also need kernel source — either the source for the kernel that ships with your distro, or one of the kernels from kernel.org. We’re going to do this with a kernel.org kernel.

On CentOS 5.5, you’ll need to install the ncurses-devel package, and the Development Tools group of packages, if you don’t have them already. Note that I pulled some of this info from the CentOS wiki, to give credit where due. To install these packages, run:

yum groupinstall "Development Tools"
yum install ncurses-devel
yum install qt-devel
yum install unifdef

Next it’s time to create the directories where you’re going to compile the source. You’re going to compile the kernel as a normal user, and then install the kernel as root. I keep a src directory in my home directory, you’ll probably want to do the same. Download the kernel source in the src directory, and then uncompress it:

tar -jxvf linux-2.6.35.4.tar.bz2

The version number will probably differ. Now cd into the linux-2.6.35.4 directory. The kernel has a dazzling array of tools to help create your config. You can use standard make config to get a series of questions, or make menuconfig for an ncurses (text) interface to compile the kernel. Alternatively, you can use a GUI to walk through the menus, like make xconfig.

If this is the first time you’ve attempted a kernel compile, the menus can be a bit intimidating. It’s hard to give any advice as to what options you should or shouldn’t select because your reasons for compiling a kernel are going to be different than mine.

You might want to start with the config file that your distribution uses. Grab the source RPM for the kernel and install it:

rpm -i http://mirror.centos.org/centos/5/updates/SRPMS/kernel-2.6.18-194.11.3.el5.src.rpm

Now look under /usr/src/kernels, mine’s located at /usr/src/kernels/2.6.18-194.11.3.el5-x86_64 but this will vary depending on architecture and version number. Under this directory there’s a .config file. Copy it to the directory with your Linux source. Run make xconfig and then load the .config that you copied over. Make any changes to the configuration you wan to, and then save the configuration.

Still with me? It’s time to do some compilin’! Run make, and get ready to sit back for a while. It will take a while for the kernel to compile. Really, even on a fast machine, it still will take the kernel quite some time to compile.

Once the kernel is finished compiling, you need to install the kernel and modules, and then configure grub. You’ll need to switch to root or use sudo here:

make modules install
make install

Now check your grub.conf under /boot/grub/grub.conf and make sure you have the configuration for your kernel. Previously I’ve had to hand-edit grub or LILO, but when I was writing this up I found that the make install process had apparently done it for me. You should see some lines like this:

 

#boot=/dev/sda
default=1
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS (2.6.35.4)
        root (hd0,0)
        kernel /vmlinuz-2.6.35.4 ro root=/dev/VolGroup00/LogVol00
        initrd /initrd-2.6.35.4.img
title CentOS (2.6.18-194.11.3.el5)
        root (hd0,0)
        kernel /vmlinuz-2.6.18-194.11.3.el5 ro root=/dev/VolGroup00/LogVol00
        initrd /initrd-2.6.18-194.11.3.el5.img
title CentOS (2.6.18-194.el5)
        root (hd0,0)
        kernel /vmlinuz-2.6.18-194.el5 ro root=/dev/VolGroup00/LogVol00
        initrd /initrd-2.6.18-194.el5.img

Note that grub counts from 0 and not 1, so you’ll need to change accordingly. And, time to reboot!

Finishing Up

Assuming all went well, you’re now running a custom kernel you compiled yourself. Congratulations! If not, you can reboot and choose one of the known good kernels and start again. If you need to report a bug in the kernel, refer to this note.

Compiling a kernel can be a great way to fine-tune your system or enable support for experimental features, etc. However, I can’t stress enough that it’s not really necessary to do so unless you want to help test the kernel or have a good reason to compile your own. The stock kernels provided by most distributions are just fine. But if you enjoy tinkering with the kernel, by all means have at it!