How to Build a Custom Kernel on Ubuntu

Build a Custom Linux 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.

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.

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.

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.

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.

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.

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.

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.

9 comments

    • It can be. If you know your system very well, you can strip out all of the unnecessary modules and create a bare-bones kernel. That can minimally increase the speed of some things.

      Stripping a kernel down is more useful for machines that you’re using for a specific purpose. If you’re building a custom router, for example, you wouldn’t want or need extras. Extra functionality can create the potential for security breaches, and it also creates bloat. Routers tend to have limited resources, so bloat is a very bad thing.

      The other main reason you’d want to build your own kernel is for new functionality. Last summer when the new open source AMDGPU drivers were first launched, only the newest kernels supported them. If you were running Ubuntu, you had to build your own kernel from the latest stable release and enable the drivers.

  1. “building the Linux kernel is really easy.”
    So is brain surgery or designing a working rocket, once you’ve done it a few dozen times. :-)

    I assume that the procedure as you have outlined in this article will work for most of Debian-based distros, not just Ubuntu. I also assume that by changing to the proper package manager commands, this procedure will work for pretty much most Linux distros.

    Another assumption on my part is that it helps to have recent hardware (CPU, RAM, m/b). A P4 or a K7 based system, with 1 GB of RAM or less, might have a bit of a problem with compiling a kernel.

    “A blue menu will open up with a listing of categories. ”
    Where does one find the descriptions of these categories and their purpose? For example, what is ‘OCFS2 expensive checks’ option and what does it do?

    • The procedure does work with most Debian distributions. It’s pretty close to other distributions as well. A lot depends on the format that you want your kernel in after it’s compiled and how you plan on installing it.

      I tested this out on a single-core VM. RAM is where I found limitations. I wouldn’t try it with less than 4GB. You might be able to get it to work, but it will take forever.

      I didn’t provide descriptions of each of the categories because it would have taken forever to go into each one. To answer your question specifically, OCFS2 is a file system. That’s why it’s under the file system category. It’s not nearly as common as something like EXT4 or XFS, though.

      Truthfully, you’re probably only ever going to use the self-explanatory categories. If you do find yourself needing something more complex, you’re probably going to be told exactly what to enable from a developer’s guide or documentation, at least that’s been my experience.

      • Thanks.
        At the present time I do not foresee myself compiling a custom kernel. If anything, I probably would be more likely to build a custom distro from Gentoo, Arch or Linux From Scratch.

  2. Hi Nick!
    It’s pretty interesting topic you open here.
    I would like to make custom kernel for main reason – AMD GPU video driver, but it still not clear how to do it.(link)
    Could you please clarify?
    ps. I already read several topics in askubuntu.com

    • Which version of Ubuntu are you running? If you’re on 17.04, AMDGPU should be working out of the box.

      If you’re talking about getting the latest kernel to try new features before they hit Ubuntu, then you can follow the process detailed in the guide. Graphics drivers are under the “Device drivers” section. There’s a subsection for graphics there. AMDGPU is a module there with its features below it. You can go and enable what you want.

      Keep in mind that you should also find the latest kernel firmware for Ubuntu and a build of the latest version of Mesa. You can build Mesa yourself from Git, if you need to. The kernel firmware will probably be available from a couple of developers.

      I hope that helps.

      • Nick,

        Thank you for sharing this.
        However, I need your assistance in completing a project – involving real time tuning of TCP Congestion control parameters using POX controller.

        Is there any help or recommendation you can give.

Comments are closed.

Sponsored Stories