How to compile kernel step by step

Recently I compiled the latest kernel by manually for just fun. so i logged my experience here. I show the step by step to the compilation process. I followed the this reference for compile the kernel.

Why Need to compile the Kernel?
  • Some time some software or modules expected that some kernel flags needed to set while compile (build) the kernel. This kind of scenario we need to rebuild the kernel with specified configuration flags are set.
  • When new kernel is released,  compile the new kernel and install in our system. But this is not recommend way to install the system. so use our distribution(Ubuntu, red-hat release )  kernel update.

Check your Linux kernel version
        In Linux system use uname -r or cat /proc/version to find the current kernel version
Linux Kernel Version
here is current system kernel version

Step 1 : Download the latest kernel
 Goto website and get the latest version of the kernel source. i will get 3.3.3


and extract the source code.
tar xf linux-3.3.3.tar.bz2
cd linux-3.3.3
in linux-3.3.3 folder contain lots of files and folders. some folders are use following purpose

arch - this folder shows the what are the architecture port the Linux
crypto- contain cryptography implementations like AES, DES
drivers-  all device drivers modules (IDE, SCSI, Ethernet device, Wireless)
fs- all file-system implementation code (ext4, FAT, NTFS)
net - all network protocol implementations (IPv4, IPv6, tcp, 802.11)

Step 2 : make the configuration file
  While compile the kernel source code, we need configuration file. that configuration file contain lots of variable to help to understand what are the modules we need to compile.

For x86 specific kernel variable check this reference 

using make command we can build the configuration, but this command is interactive, its ask more than 1000 question about enable or disable the particular module, like which file-system r u want and IP-Tables related modules like SIP components (state-full inspection firewall).

so the best way is copy the current Linux config file. its stored in /boot/config-<version>.
cp /boot/config-      .config 

now we got old configuration. now we change/add new configuration settings.
for this purpose therse is lots of options are avialable

make help       ==>Provides the help

There is GUI option is also available

make   menuconfig          ==> this provides Text based GUI Configuration

here load the old config file and add/change the settings then save the config file.

make   gconfig      ==> this provides GTK 2 based GUI Configuration for GNOME

for this we need to install libgtk2.0-dev, libglad2-dev through apt-get install

make   xconfig      ==> this provides QT based GUI Configuration for KDE

for this we need qt-dev tools

Using any one of the above method modify the configuration.

Step 3 : Compile the Kernel
        There are two ways to compile the kernel

  • Generic way (lots of steps)
  • Debian specific way (simple, Use make-kpkg tool)

Step 3.1 : Generic Way

      Step 3.1.1 Compile the Kernel and its modules

       above code is take 1 to 2 hours depending on system performance. In this step it compile the kernel and store the kernel in binary form to arch/x86/boot/bzImage file
then based on kernel headers it will compile the kernel  modules (device driver, file-system, network,...)
and generate .ko files. ko means kernel object. These modules also called Loadable Kernel Modules (LKM).

   Step 3.1.2 Install Kernel modules

     make  modules_install
     This step copy the all kernel modules (*.ko) to /lib/modules/<version>/kernel/   folder.

       Step 3.1.3 Install Kernel 

       make  install
      This step copy the the kenel from arch/x86/boot/bzImage to /boot folder and copy the .config file to /boot/config-<latest-version> and generate the file.

      Step 3.1.4 Create Initramfs file
     up to now kernel and its modules are compiled and installed. when next boot up time we need to choose latest kernel. so we need to prepare the boot-loader and its support files. When system turns on, after bios and boot loader load the kernel to main memory and mount initial dummy file system as a root file system of system. this initial file system have necessary drivers for disk hardware (SCSI or IDE) and mount the correct file system as a root file system.

so we need to create initramfs file using update-initramfs or mkinitfs tool

     update-initramfs -c -k 3.3.3
here 3.3.3 is new kernel version.

   Step 3.1.5  Update GRUB bootloader
  the last step is update the boot loader here i m using GRUB boot-loader.

this command automatically probe the kernels in /boot folder and add the entries in its configuration file, grub.cfg

now restart the system , we will see the new kernel is added in boot loader entries. then choose new kernel in boot loader.

now open the terminal and issue uname -r command, its shows the  current kernel version.

Step 3.2 : Debian specific way Way

              In Debian system's (Ubuntu, Linux Mint,..) there is one tool "make-kpkg" is available to automate the kernel compilation process.

to install the make-kpkg, use apt-get
sudo apt-get install    kernel-package    libncurses5-dev

this one line command is compile the kernel and its modules and make the binary form in deb file.
make-kpkg --initrd --append-to-version=-ramki    kernel_image     kernel_headers

this one line make the kernel and its headers with custom suffix -ramki
--initrd specify that make initramfs disk file
kernel_image option to make the kernel and its modules
kernel_headers option to make all header files of the kernel. (optional)

the final output is its create 2 DEB files.

Install the Deb files
      Install the deb file is simple. use dpkg tool like normal installation in Debian system
dpkg -i kernel_image-ramki-i386.deb

while executing this line install the kernel to /boot folder. install the modules in /lib/modules folder and create the initramfs file and update the grub like generic way. all the above operation is happen automatically.

then in optional case we can install the kernel_headers-ramki-i386.deb file.

why need the kernel headers are important?
       kernel_image is enough to run the system. but when we need to install drivers, for example we need to install the new driver in Linux  that time while compile the driver, that time need the kernel headers.

In my system its works fine. i tested in VM also.


Comments Welcomed