How to Build and Install a Custom Kernel on Ubuntu

Customize your Linux to the max by compiling your own kernel

Ubuntu Custom Kernel Featured Image

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 in Ubuntu 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. Here we will show you how to build and install a custom kernel on Ubuntu.

Getting the Dependencies

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

sudo apt install wget build-essential bison flex libncurses-dev libssl-dev libelf-dev

Getting the Kernel Source for Ubuntu

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 releases in their cdn repository. At the time of this article, the latest is 5.16. You can download using the commands:

cd /home/$USER/
mkdir kernelbuild
cd kernelbuild
wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.16.19.tar.xz
Ubuntu Custom Kernel Download Kernel

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

Setting Up for Your Build

Begin your setup by unzipping the kernel tar file. Then, copy the configuration of your existing kernel into it.

tar xavf linux-5.16.19.tar.xz && cd ./linux-5.16.19
cp /boot/config-`uname -r` .config

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

make oldconfig

This script will ask you what to do with every new feature. If you want to use the defaults, use make olddefconfig instead.

Configuring the Ubuntu 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, run

make menuconfig
Ubuntu Custom Kernel Makeconfig Sample 1

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

Ubuntu Custom Kernel Btrfs Sample

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.

Building Kernel Packages for Ubuntu

There is a method in 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` 
make 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.

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.

cd ./../
sudo dpkg -i linux-firmware-image-5.16.19-custom_5.16.19-custom-1_amd64.deb
sudo dpkg -i linux-libc-dev_5.16.19-custom-1_amd64.deb
sudo dpkg -i linux-headers-5.16.19-custom_5.16.19-custom-1_amd64.deb
sudo dpkg -i linux-image-5.16.19-custom-dbg_5.16.19-custom-1_amd64.deb
sudo dpkg -i linux-image-5.16.19-custom_5.16.19-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 running uname -r in a terminal when it starts up.

Installing Custom Binary Kernels

Another way of using custom Linux kernels in Ubuntu is by installing a pre-compiled binary version of it. Unlike the source-based version, these are copies of the kernel that have already been compiled for the system and architecture that you are running.

Ubuntu Custom Kernel Kernel List

Further, custom binary kernels can also provide you with the flexibility of being able to jump across kernel versions for your Ubuntu installation. This can be especially helpful if you are running a machine with hardware compatibility issues and you do not want to deal with compiling your own kernel.

Knowing that, the most popular way of installing custom binary kernels is through Mainline. This is a GUI program developed by bkw777 that aims to streamline the process of installing a Linux kernel.

Ubuntu Custom Kernel Kernel Mainline

In that, this program combines all of the steps that you need to take to properly download and configure the kernel. For example, Mainline allows you to easily pick a kernel binary and header that you want to install. It will then hold this version in a cache where you can easily load from whenever you need it.

Installing Mainline in Ubuntu

With that, installing Mainline is relatively easy. First, you need to tell apt to include Mainline’s package repository. To do that, you can run the following command from your Terminal:

sudo add-apt-repository ppa:cappelikan/ppa
Ubuntu Custom Kernel Add New Repo

From here, you need to then update Ubuntu’s package directory. This will allow apt to read from the repository that you have just added and, in turn, allow you to download Mainline through it:

sudo apt update

With that, the only thing left to do is to install Mainline itself:

sudo apt install mainline
Ubuntu Custom Kernel Installing Mainline

Using Mainline

Once done, you can now load Mainline by going to your Application Menu and searching for it. The Mainline program will then load and display all of the kernel packages that you can install for your system.

Ubuntu Custom Kernel Search Mainline

From there, you can then select and install the kernel version that you want to use for your system. For example, I can select “Linux 5.16.19” and click Install on the right sidebar to fully load this kernel version to my machine.

Ubuntu Custom Kernel Install New Kernel 1

On the other hand, I can also use Mainline to remove the kernels that I do not want to use anymore. For example, I can select “Linux 5.15” and click Remove on the same right sidebar to remove this kernel from my system.

Ubuntu Custom Kernel Uninstall Old Kernel

Doing it this way allows you to cleanly add or remove any files and binaries that are linked with that kernel version. This is because a kernel binary also includes the libraries, headers and firmware which, in turn, allows it to function properly.

Frequently Asked Questions

What are the issues of running a custom kernel?

One of the biggest issues of running a custom kernel is that you are taking a vital piece of your system out of regular updates. This means that you need to manually install all kernel updates from this point on. These include additional hardware features as well as critical security updates.

This can be a problem if you need a system that runs and maintains itself with minimal user intervention. As such, running a custom kernel not only requires you to know what kernel features you want but also be aware of critical updates.

Is there any disadvantages to using a kernel binary?

Yes. One of the main disadvantages of using a kernel binary is that you do not have any control with its built-in features. This means that, unlike a source-based kernel, a kernel binary will not contain any hardware-specific optimizations and support.

This means that if you intend on using exotic pieces of hardware with Ubuntu. You will need compile your kernel from scratch.

Is it better to compile a kernel feature as a module or a built-in?

This largely depends on what you want to use your kernel for. For example, compiling a feature as a built-in will make sure that it will immediately load once the kernel is loaded into memory. However, doing it this way will increase the overall size of the final kernel.

Meanwhile, compiling a feature as a module will remove it from the base kernel. Instead, this module then loads once the kernel is already in memory. This reduces the file size of the final kernel which, in turn, makes it fast and secure.

Knowing that, it is good practice to compile kernel features as a built-in as it reduces the chances of any issues with compatibility in your system. However, you should also be wary on including too much features as doing it will significantly increase your kernel’s overall size.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox