This is a quick post to document the steps I took to compile the 3.0 kernel, the latest Linux kernel, on my Ubuntu 11.04 box. This should be child’s play for anyone familiar with compiling custom kernels, but I just thought I would document it to help those new to Linux to overcome the fear associated with the phrase “compiling a new kernel”. Since this post is aimed at “newbies”, I will try to keep the discussion as “lay-person friendly” as possible, with technical jargon held to a minimum. OK, enough talk, lets get you started on your road to compiling kernels.
First, you need to get the latest version of the kernel source code on your machine. You should visit this site for a list of the latest kernel versions. Choose the version of the kernel that you want to download. For the purpose of this post, I will assume that we want to download the
latest version of the kernel last release candidate for version 3.0 of the kernel, 3.0-rc7. We can do this using the command below:
PS: Thanks to Francois Lebel for pointing out that I was working with the release candidate of the kernel, not the final version of the kernel which was released a couple of days ago. The steps remain the same, but you will want to download the release version of the kernel instead of the release candidate to get the most up-to-date kernel image.
The next step is not strictly necessary, but I like to keep my home folder clean. In order to keep the home folder uncluttered, we create a new folder, kernel_3.0, and move the downloaded archive into this folder using the commands below:
mkdir kernel_3.0 mv linux-3.0-rc7.tar.gz kernel_3.0/
Once this is done, we enter the new folder and unzip the archive:
cd kernel_3.0 tar -xvf linux-3.0-rc7.tar.gz
This should create a new folder, linux-3.0-rc7, containing the source code of the new kernel. We are now ready to start compiling the code. Before doing so, however, it’s a good idea to make sure that you have all the necessary packages on your system. If you do a lot of programming on your system, you probably have all the necessary programming tools on your machine — if not, you will need to download the build-essential package. You will also need the libncurses5 and libncurses5-dev libraries on your system. These libraries are required by the console mode kernel configuration utility, make menuconfig, that I like to use to configure my kernel. Make sure that you have these packages by issuing the following command:
sudo apt-get install libncurses5 libncurses5-dev
You are now ready to begin compiling your new kernel. Go into the linux-3.0-rc7 folder and begin configuring the kernel — i.e., choose the set of features you want activated in your kernel. My favorite method for configuring the kernel is to copy the old configuration file of the kernel I am currently using, and make modification to this file to suit my new needs.
You will find several config-<kernel_version> files in your /boot folder — one for each version of the Linux kernel on your machine. For example, if you are currently using the 2.6.38-10 kernel, you will find a file called config-2.6.38-10-generic in your boot folder. You should copy this file to the folder containing the source code of the new kernel in order to use it as a starting point for your new configuration.
Linux expects the configuration file for the kernel being compiled to reside in a file called .config. Therefore, you need to copy config-2.6.38-10-generic from the boot folder to .config in the folder containing the source code of the new kernel. Once you have copied the file, you can begin using it as the starting point of your configuration. This can be done as follows:
cd linux-3.0-rc7/ cp /boot/config-2.6.38-10-generic .config make localmodconfig
Invoking make localmodconfig starts the configuration process. Using make localmodconfig makes the configuration process extremely easy. Instead of configuring everything by hand, make localmodconfig detects the modules that are currently being used, and activates those modules in the configuration of the new kernel. For the uninitiated, modules represent, mainly, device drivers, so this means that we can configure the new kernel to include only the drivers that we need.
Note that make localmodconfig does not completely automate the configuration process. There will be options in your new kernel that were not supported in the old kernel, and you will be presented with a number of questions to configure the settings of these options. But the amount of work is much smaller than if you had attempted to configure everything from scratch. You can tweak the configuration by issuing make menuconfig and modifying the settings you would like to change — this command starts a GUI that you can use to select or de-select settings.
Once this is done, you are ready to compile and install your kernel. You can do so by issuing:
make all sudo make modules_install sudo make install
Note that make all will take a lot of time – from half an hour to an hour, depending on how many features you have activated in the kernel – so you may want to make yourself a cup of coffee while waiting for this to complete.
Your kernel has now been compiled and installed. All that remains is for you to create a ramdisk that contains a set of drivers that are necessary to mount the root file-system. This is necessary because the root file-system needs to be mounted by the kernel to load the OS, and the kernel can only mount a file-system if the necessary drivers are loaded into memory. The ramdisk contains a minimal set of drivers that are loaded into memory during boot-time to allow the kernel to mount the root file-system and load the rest of the OS from there. You can do this by issuing the following command from within the /boot folder:
sudo mkinitramfs -o initrd.img-3.0.0-rc7 3.0.0-rc7
This will create the new ramdisk. The only remaining step is to tell your bootloader about the location of your new kernel. If you are using grub, the standard bootloader on Ubuntu, you can do this by issuing the command:
That is it! You are now good to go, reboot your computer and choose your new kernel from the bootloader screen. Enjoy! Let me know if you have any trouble following these steps in the comments section and I will try to respond as soon as possible — have fun kernel compiling 🙂