How to compile the Linux kernel on Debian or Ubuntu

Reasons why you want to build a custom Linux kernel are numerous. For example, you may want to add driver support for devices which are not recognized by the generic kernel shipped with Linux distros. You may want to optimize the kernel for your hardware, tweak performance tuning options, or turn on or off specific kernel options that cannot modified at run time. If you would like to compile the Linux kernel for any of these reasons, follow these instructions on compiling the Linux kernel on Debian or Ubuntu.

First of all, make sure that you have enough space left in the partition where the kernel source will reside. You need at least 10GB available space in the partition. Once you verify that there is enough space, then proceed as the root as follows.

$ sudo apt-get update
$ sudo apt-get install kernel-package libncurses5-dev fakeroot wget bzip2
$ sudo apt-get install build-essential udev
$ sudo apt-get install linux-source-$(uname -r)
$ cd /usr/src
$ sudo tar xvfvj linux-source-xxxxxx.tar.bz2
$ sudo ln -s linux-source-xxxxxxx ./linux
$ cd /usr/src/linux
$ sudo make clean && make mrproper
$ sudo cp /boot/config-`uname -r` ./.config
$ sudo make menuconfig
(modify kernel configurations as you wish)
(or you could just do: $ make oldconfig)
$ sudo make-kpkg clean
$ sudo fakeroot make deb-pkg

The last step build is going to take 30 minutes to 1 hour depending on your system. If the build is successful, it will generate linux-image-xxxxxx.deb in /usr/src. You can install the newly built custom kernel image as follows.

$ sudo dpkg -i /usr/src/linux-image-xxxxx.deb

Finally, update the grub configuration file (/boot/grub/grub.cfg) to change the default kernel image to load during booting.

Subscribe to Xmodulo

Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.

Support Xmodulo

Did you find this tutorial helpful? Then please be generous and support Xmodulo!

The following two tabs change content below.
Dan Nanni is the founder and also a regular contributor of He is a Linux/FOSS enthusiast who loves to get his hands dirty with his Linux box. He likes to procrastinate when he is supposed to be busy and productive. When he is otherwise free, he likes to watch movies and shop for the coolest gadgets.

Leave a comment

Your email address will not be published. Required fields are marked *