How to Compile a Linux Kernel

Hello Everyone as in last post i just gave the basic of programming a kernel with some theoretical and graphical description that i am sure helps you out to at least basic understanding of kernel and its programming. If you missed that article then don’t worry click here to read that post.

Lets move ahead, Before moving to programming section i just want to focus on point that we are doing our kernel programming practices on Linux kernel because it is open source and easy to code so the programmer won’t face any difficulties while coding and debugging the kernel.

First of all you need to install a Linux and i prefer install a latest version of Ubuntu or there is many distribution around over net but for beginners and home user Ubuntu is best. If you already have Linux then move ahead.

Note: If you don’t want a DualBoot system and want to use only your primary then i prefer to use the Virtual Machine like virtual box, Vmware and if you are not familiar with them the click here for “installation and setup of virtual machines“.

If you find any difficulty while downloading and installing Linux i.e. Ubuntu Linux then click here to read the “installation and setup of Ubuntu” post.

Now As if you have Linux and i think you are little familiar with “terminal” Then start the programming steps:

Step1: Download the Latest Version of source code [Free]

To edit and compile the kernel we need a source code of kernel and to get the latest version directly please click here or do it manually given below in steps.

Remember this file is come with extension .tar.bz so you have to extract it, Alternative method for this is to open terminal from System->Terminal and type following commands one by one.

$ cd /tmp
$ wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-3.1.0.tar.bz2

Note: If you want the copy paste the do it one by one not the whole at once 🙂 because both are different commands.

Step2:Extract the source file.

As i told we get the package in .tar.bz format so have to extract it, we can do it as simple by clicking right button of mouse on the package and selecting the extract option else by commands…..your choice………!!!

# tar -xjvf linux-3.1.0.tar.bz2 -C /usr/src
# cd /usr/src

Step3: Configuring Kernel and compiling

Well now we are on programming step so before this make sure you have following things on your system.

  • Gcc latest version,
  • ncurses development package.

If you don’t have them then download it and install it.

Type following for the Gcc:

$ sudo apt-get install gcc

Type this for ncurses development tool:

$ sudo apt-get install libncurses5-dev

To configure a kernel there is three basic commands for general modifications:

  • $ make menuconfig
  • $ make xconfig
  • $ make gconfig
menuconfig is used to edit the text based version of linux and in this post we are doing only this cause i believe the large amount of food will make you ill…:)
xconfig is used to edit the windows and other GUI tools in KDE system and gconfig is used to edit same but in gnome system. These both are the advance and skilled commands.
Lets move for the menu config:
type following commands:
$ sudo make menuconfig
Once you type and hit enter ,you will get a pop up window with the list of menus and you can select the items for the new configuration. If your unfamiliar with the configuration just check for the file systems menu and check whether ext4 is chosen or not, if not select it and save the configuration. Check the screen shot below for menuconfig.
Here you can edit the text or caption of menus and once you done you have to compile it:
To compile the changes type following commands:
$ sudo make
As you type the above command it will compile your modified your code so be patients cause it take a time around 40 minutes.!
Step4: Install the modified Kernel:
Now to see the output of your changes you have to install your own kernel in your system [ That’s why i prefer virtual machine].
To install and check type following commands [sorry now GUI shortcuts here]
$ sudo make modules_install install
The above command is used to install the kernel and it creates a boot file and image file as well. After installation to make sure whether it is done or not check for the following files:
  • System.map-3.1.0
  • vmlinuz-3.1.0
  • initrd.img-3.1.0
  • config-3.1.0
If you won’t find the initrd.img-3.1.0 [as in case of mine] the type the following commands to get it:
$ sudo update-initramfs -u -k 3.1.0
Now you have successfully installed the Linux kernel in your system reboot it and you will see the latest version of kernel as well as the changes of menus that you made.
Hope this is helpful for you..!!!
Show your response in form of comments.
Good luck and happy programming.
About Shahid

Shahid is a Engineering student and addicted to Technology. He is Working For iTechCode(Editor-In-Chief) very passionate about blogging and technology. His area of interest includes programming, tech gadgets, gaming, internet marketing, blogging etc.

Comments

  1. Sathishkumar says

    Thanks for this article. Your blog is looking good and i like the contents in it 🙂

Speak Your Mind

*