How to compile the Linux kernel

To the source

Before we can begin, you need to install the kernel source. To start with, we will use the source that matches your existing kernel, because this will already have a working configuration that we can use as a starting point. Using your distro’s package manager, install the correct package. The name varies between distros but is usually something like linux-source or kernel-source. To find the version of the running kernel, use the terminal command

uname -r

You also need to install a compiler and associated tools; installing GCC should pull in everything you need. Some distros provide a development group of packages; if you run Ubuntu you should install the build-essentials package, which pulls in a few other packages as dependencies. Linux source code is usually installed in /usr/src/linux-version-number, with a symbolic link to /usr/src/linux. If your distro does not create the link, do so yourself with

cd /usr/src
ln -s linux

Note that in Ubuntu, installing the linux-source packages drops the file into /usr/src, so you’ll have to extract it before making the link.

Because we are using the existing configuration as a starting point, we must make sure that there is a copy of the configuration file in /usr/src/linux. The file is called .config; if it isn’t present, there will probably be a copy stored in /boot or as part of the linux headers package. Running locate .config should find it for you, then copy it to /usr/src/linux. In Ubuntu 9.10, for instance, the file is /boot/config-2.6.31-14-generic (version numbers may vary), so copy that to .config in the source directory.

Once you have installed the source, you can start configuring it by executing the following commands in a root terminal. If you are using an X terminal, you will find it easier if you set its window to its maximum size.

cd /usr/src/linux
make menuconfig
make menuconfigRunning ‘make menuconfig’ starts an ncurses-based text mode kernel configuration tool.

You are now in the Ncurses configuration tool. Spend some time browsing around and trying out various options (navigate with the cursor keys). You will be asked if you want to save your kernel configuration when you exit; unless you say Yes, you can experiment all you want without affecting anything. Most of the options are fairly obvious. Any item ending with ‘—>’ leads to a sub-menu, which you can see with Enter. Exit actually takes you back up a level (that is, it exits the current level), and it only exits the program itself when at the top level. You can use the Esc key for Exit.