Building and Installing a Kernel

This post is about how to install a new kernel from source.

The latest source code for the Linux kernel is kept on You can either download the full source code as a tar ball (not recommended and will take forever to download), or you can check out the code from the read-only git repositories.

First let’s install some packages using:

sudo apt-get install vim libncurses5-dev gcc make git exuberant-ctags

I was working on the staging-next, so this post will take that as an example.

git clone -b staging-next git://

Above command clones staging-next branch of Greg’s tree. Obviously as the size is big so this clone is going to take some time.

Now let’s set up the configuration.

When you download the source tree, it doesn’t come with a .config file. You have several options on generating a .config file. The easiest is to duplicate your current config.

Duplicate your current config using:

cp /boot/config-`uname -r`* .config

If you want to make some change in your configuration, run the following command, to to silently update any new configuration values to their default:

make olddefconfig

If you want to make changes, you can run

make menuconfig

This will popup something like this:

make menuconfig

If you simply want to use the default just save and exit out of the above menu.

Now let’s build the kernel:



or if you have a multi-core processor (run nproc), run

make -jX

Now let’s install new kernel

Save the following script in a file where your code is present.

echo " " &&
echo "This script will attempt to install Linux Kernel 3.9 on this machine." &&
echo "Typically, your current version will be kept, and you will be able to ustilise it again later if Kernel 3.9 does not work." &&
echo " " &&
read -p "Press Enter to continue, or abort by pressing CTRL+C" nothing &&
echo " " &&
echo "Downloading Kernel 3.9 Packages" &&
echo "3 Files, 55 MB to Download" &&
echo " " &&
echo "Creating Kernel Directory in Home folder" &&
echo " " &&
mkdir -p $HOME/kernel-htu &&
cd $HOME/kernel-htu &&
echo " " &&
echo "Downloading File 1 of 3, 12 MB" &&
echo " " &&
wget &&
echo " " &&
if [ $(getconf LONG_BIT) = "64" ]
echo "64bit Detected" &&
echo " " &&
echo "Downloading File 2 of 3, 1 MB" &&
echo " " &&
wget &&
echo " " &&
echo "Downloading File 3 of 3, 12 MB" &&
echo " " &&
echo "32bit Detected" &&
echo " " &&
echo "Downloading File 2 of 3, 1 MB" &&
echo " " &&
wget &&
echo " " &&
echo "Downloading File 3 of 3, 12 MB" &&
fi &&
echo " " &&
echo "Installing Kernel" &&
echo "This step will require you password." &&
echo "This is the last step you can safely cancel at." &&
echo "Use Ctrl+C to cancel." &&
echo " " &&
sudo dpkg -i *.deb &&
echo " " &&
echo "Installation Complete" &&
echo " " &&
read -p "Press Enter to Delete the Downloads, or CTRL+C to keep them." nothing &&
echo " " &&
sudo rm -rf $HOME/kernel-htu

Set execute permissions:

sudo chmod +x

Execute the script :

sudo ./

Running your kernel

You need the grub menu at the time of boot, to make it always appear at the time of boot, do the following

#Open grub file using vim, you can use any editor

vim /etc/default/grub

#Either comment or remove the following line



#Change GRUB_TIMEOUT to something > 0

This portion of my grub file looks something like this:




GRUB_TIMEOUT=10 #how long grub shows the menu before it choses the kernel at the top of the list

Now after editing the file, update it using

sudo update-grub2

Now you are done. You just need to reboot.

sudo reboot

load the kernel which you installed.

9 Replies to “Building and Installing a Kernel”

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s