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.
Getting the Dependencies
Before you even touch a kernel, you need the right tools to build it. Use Apt to download them from Ubuntu’s repositories.
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.
git clone-b linux-4.11.y git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git
It will take a while to clone the kernel, so be patient.
Setting Up for Your Build
Begin your setup by changing directories into the newly cloned directory. Then, copy the configuration of your existing kernel into it.
cp/boot/config-`uname -r` .config
Now you have to adapt the old configuration to the new kernel.
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.
Configuring the Kernel
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.
A blue menu will open up with a listing of categories. Those categories contain features that you can select to build into your kernel.
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.
Now your kernel is ready to build.
Building Kernel Packages
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.
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.
Installing the Kernel
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.
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.