Build custom kernel on Debian / Ubuntu

The Linux kernel sits at the core of all Linux systems, including thousands of GNU/Linux distributions, the Android mobile operating system, and tons of embedded systems, networking devices, etc. Its popuarity and ubiquity can be credited to the fact that it is free and open source. Anyone can download the Linux kernel, make changes to it if they wish, and compile it for their own commercial or private use.

With nearly 30 million lines of code and a massive number of contributers from all over the world, the Linux kernel sounds like a complicated project that an ordinary user would never bother themselves with, right? You may think that the core of all these operating systems should be left to scientists, programmers, and IT professionals. But, in reality, downloading the Linux kernel source code and compiling it is very straightforward, and any Linux user can do it.

In this tutorial, we will take Debian and Ubuntu users through the step by step instructions of downloading the Linux kernel source code, installing the prerequisite packages that allow us to compile it, and finally compile the kernel with our own custom options. At the end, you will also see how to install the kernel into your own system, so your computer can run the very kernel you made yourself.

In this tutorial you will learn:

  • How to download Linux kernel source code
  • How to install prerequisite packages needed to compile kernel
  • How to customize the Linux kernel
  • How to compile the Linux kernel
  • How to install the Linux kernel on our own system
Build custom kernel on Debian / Ubuntu
Build custom kernel on Debian / Ubuntu
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Debian Linux based system
Software N/A
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Build custom Linux kernel step by step instructions



Twitter icon Follow LinuxConfig.org on Twitter for the latest tips and tricks about Linux!

  1. We will start by downloading the Linux kernel source code. Navigate to kernel.org to download the version you want. Most likely you will just want to download the latest version tarball.
    Download the lastest version tarball of the Linux kernel
    Download the lastest version tarball of the Linux kernel
  2. Before we tinker with the file we just downloaded, open a command line terminal and install the following prerequisite packages. Some are most likely already installed.
    $ sudo apt update
    $ sudo apt install fakeroot build-essential libncurses-dev xz-utils libssl-dev flex libelf-dev bison
    
  3. Next, extract the Linux kernel tarball.
    $ tar xvf linux-5.18.tar.xz
    $ cd linux-5.18
    

    Replace the file name above with that of your own download.

  4. This is where we can begin to customize things. The next steps will just be a recommendation, but feel free to make your own configurations as you see fit. First, we will copy our system’s current kernel configuration file and use it for our new kernel.
    $ cp /boot/config-$(uname -r) .config
    
  5. The following make command allows us to edit the configuration file we just copied and choose which features we want to include in our compiled Linux kernel. Make your desired changes here. When done, you can save and exit from this menu. Note that it is not necessary to make any changes at all if you just want to stick with the settings of your current system.


    $ make menuconfig
    
    Configuring our Linux kernel
    Configuring our Linux kernel
  6. Next, it is time to build the Linux kernel with the following commands. You will see a lot of output in your terminal and the compile process could take a while. The first two commands make a necessary change to the certificate in order to let us compile.
    $ scripts/config --disable SYSTEM_TRUSTED_KEYS
    $ scripts/config --disable SYSTEM_REVOCATION_KEYS
    $ sudo make
    
  7. Once that is complete, we can install the necessary kernel modules with the following command.
    $ sudo make modules_install
    
  8. Then, to install the kernel onto your own system, finish up with this command:
    $ sudo make install
    
  9. Lastly, reboot the system to load into the new kernel you just compiled and installed.
    $ reboot
    
  10. After rebooting, you should see that your system is utilizing the new kernel you just compiled. You can verify this with the uname command and seeing that the version numbers match.
    $ uname -r
    

Closing Thoughts




In this tutorial, we saw how to download and compile the Linux kernel from source code on a Debian or Ubuntu based system. You also saw how to configure the Linux kernel to your own needs, as well as how to install the kernel on your own system. With these steps, you can always download and install the latest kernel on your computer. You can also experiment with adding or shedding features as you see fit.