Why Compile Your Own Kernel?

Most Linux users use pre-built kernels provided by their distribution. This is convenient and perfectly sufficient. However, compiling your own kernel opens up a world of new possibilities and is a fundamental skill for anyone who wants to deeply understand how the system works.

The main reasons to do it are:

  • Optimization: You can remove unnecessary drivers and features, creating a kernel perfectly tailored to your hardware, which may (or may not) result in better performance and a faster system startup.
  • Latest Features: Access to the latest features, drivers, and security patches before they make it to your distribution’s official repositories.
  • Learning: It’s the best way to see “behind the scenes” of how an operating system is built.
  • Hardware Support: Sometimes the only way to get very new or unusual hardware to work is by enabling experimental drivers in the kernel.

This guide will walk you through the entire process, step by step.

Step 1: Preparing the Environment

To compile the kernel, we need a few essential tools.

  • For Debian-based systems (Ubuntu, Mint):
    sudo apt-get update
    sudo apt-get install build-essential libncurses-dev bison flex libssl-dev libelf-dev
    
  • For Red Hat-based systems (Fedora, CentOS):
    sudo dnf groupinstall "Development Tools"
    sudo dnf install ncurses-devel bison flex elfutils-libelf-devel openssl-devel
    

Step 2: Getting the Kernel Source

The latest kernel versions can always be found on the official kernel.org website. Download the archive with the latest stable version (mainline), and then unpack it.

# Change to the directory where you want to keep the sources
cd /usr/src/

# Download the sources (replace the version number with the current one)
wget https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.5.3.tar.xz

# Unpack the archive
tar -xf linux-6.5.3.tar.xz

# Change to the source directory
cd linux-6.5.3/

Step 3: Configuring the Kernel

This is the most important stage. We need to create a .config file that tells the compiler which features, drivers, and options to include.

The safest approach is to copy the configuration of your current kernel and modify it.

# Copies the configuration of the currently running kernel
cp /boot/config-$(uname -r) .config

Now we have several ways to edit this configuration:

  • make oldconfig: The system will only ask you about new options that have appeared in the newer kernel version compared to your old configuration. This is the fastest method. (More in our article about make oldconfig!)
  • make menuconfig: Starts a text-based graphical interface that allows you to conveniently browse and modify all options. This is the most recommended method.

Let’s run menuconfig:

make menuconfig

Navigate the menu with the arrow keys, enable (Y), disable (N), or set as a module (M) the options you are interested in. Save your changes when you are done.

Step 4: Compilation

Once the .config file is ready, we can start the compilation. This is a process that can take from a dozen minutes to several hours, depending on the power of your processor.

# Start the compilation, using all available processor cores (-j `nproc`)
make -j $(nproc)

The Role of Initrd/Initramfs

Before proceeding with the installation, it’s worth mentioning initrd (initial RAM disk) or initramfs (initial RAM filesystem). This is a small filesystem that is loaded into memory before the actual kernel and serves to prepare it. It contains essential modules (e.g., drivers for the disk controller, filesystem), tools for hardware detection, and mounting the root filesystem.

In modern Linux systems, initramfs is crucial because the kernel itself is very modular and often needs specific drivers to even access the disk where the rest of the system resides. The make install command typically automatically generates the appropriate initramfs file for your new kernel, using tools like dracut (Red Hat) or update-initramfs (Debian/Ubuntu).

Step 5: Installing Modules and the Kernel

After the compilation is finished, we need to install the compiled modules and the kernel image itself.

# Install the modules
sudo make modules_install

# Install the kernel image
sudo make install

The make install command will automatically copy vmlinuz to /boot, create an initrd, and on most systems, update the GRUB bootloader configuration by itself.

Step 6: Reboot and Verification

Everything is ready! Now all you have to do is restart your computer.

sudo reboot

During system startup, in the GRUB menu, you should see a new entry with your freshly compiled kernel. Select it.

After logging in, you can verify that the new kernel is active with the uname -r command. It should display the version of the kernel you compiled.

uname -r

Summary

You did it! You have compiled and are running your own Linux kernel. Although the process may seem complicated at first, it provides great satisfaction and control over the system. It’s a gateway to a deeper understanding of Linux and the open-source world. Don’t be afraid to experiment with the configuration options!