How to Build a Custom Kernel on Ubuntu

The thought of compiling your own kernels strikes fear in the hearts of new Linux users. It sounds terrifying, building the most crucial part of your system from scratch. The truth is, though, building the Linux kernel is really easy.

Building Linux kernels does not require programming. Some of the best programmers in the world have already written all of the code. You just have to pick the features that you want and put it all together.

Before you even touch a kernel, you need the right tools to build it. Use Apt to download them from Ubuntu’s repositories.

sudo apt install install git build-essential kernel-package fakeroot libncurses5-dev libssl-dev ccache

Now you can grab the source that you want to build. All of the kernels are available from the official Linux repositories. You can take a look at the latest stable releases in their git repository. At the time of this article the latest is 4.11. That branch is listed as linux-4.11.y, and that’s the one that will be cloned with the command below.

cd ~
mkdir kernelbuild
cd kernelbuild
git clone -b linux-4.11.y git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git

Clone The Linux Kernel Source From Git

It will take a while to clone the kernel, so be patient.

Begin your setup by changing directories into the newly cloned directory. Then, copy the configuration of your existing kernel into it.

cd linux-stable
cp /boot/config-`uname -r` .config

Now you have to adapt the old configuration to the new kernel.

yes '' | make oldconfig

Normally the script will ask you what to do with every new feature. This way accepts the defaults. If you want to be asked, just use make oldconfig.

You can leave your configuration the way it is, and you’d probably be alright. There’s no point in building a custom kernel if you’re not going to customize it, though.

To customize your configuration, open make menuconfig.

Linux Kernel Configuration Menu

A blue menu will open up with a listing of categories. Those categories contain features that you can select to build into your kernel.

Linux Kernel Filesystems Configuration

For example, if you really want to build support for the BTRFS file system directly into the kernel and enable other features, you’d go to “File systems ->.” Then, scroll down to where you see “Btrfs filesystem support.” Select the option that you want and hit the Space bar. The Space bar cycles between “M,” “*,” and empty. “M” signifies that the feature will be built as a module that will be loaded if needed when Ubuntu starts. “*” means that the feature will be built into the kernel and always loaded. The script does not include blank options in the final product.

When you’re done setting things up, clean the directory.

make clean

Now your kernel is ready to build.

There is a method used by Ubuntu to build their kernels, but it forces you to use scripts written for older versions. Sometimes that’s alright; others it breaks horribly. So, it’s usually better to just use the more generic Linux method with GNU make.

make -j `getconf _NPROCESSORS_ONLN` deb-pkg LOCALVERSION=-custom

All that line does is compile the kernel into .deb packages using the amount of CPU cores on your system plus one. It also adds on “custom” to the end of the package version to differentiate your custom kernel from others.

Note: it can take hour(s) to compile a kernel. Be patient.

You’ll find your new kernel packages one directory up. They’ll be easily identifiable by their version number. You can use dpkg to install them.

cd ..
sudo dpkg -i linux-firmware-image-4.11.1-custom_4.11.1-custom-1_amd64.deb
sudo dpkg -i linux-libc-dev_4.11.1-custom-1_amd64.deb
sudo dpkg -i linux-headers-4.11.1-custom_4.11.1-custom-1_amd64.deb
sudo dpkg -i linux-image-4.11.1-custom-dbg_4.11.1-custom-1_amd64.deb
sudo dpkg -i linux-image-4.11.1-custom_4.11.1-custom-1_amd64.deb

When the installation finishes, restart your computer. Ubuntu will automatically boot into your new kernel. You can double-check that it did by runing uname -r in a terminal when it starts up. If you see your version, congratulations! You’re running your own custom kernel.

Leave a Reply

Yeah! You've decided to leave a comment. That's fantastic! Check out our comment policy here. Let's have a personal and meaningful conversation.