How to compile your first Linux kernel (and 3 reasons why you should)

12 hours ago 5
BOOK THIS SPACE FOR AD
ARTICLE AD
penguin parent and chick
Andrew Peacock/Getty Images

I remember the first time I compiled a Linux kernel. I was nervous. It was something I'd never done before, and it seemed like an incredibly difficult task. After all, isn't the kernel the stuff of elite programmers? If so, why would I even bother?

Because I could.

Also: These Linux distributions are best for developers

And because all of the cool open-source, alternakids were doing it.

If I wanted to fit in, I had to make (see what I did there?) it happen.

So, I downloaded the kernel source, took a deep breath, and dove in.

This would have been around 2000 or 2001, and when I successfully pulled it off, I'm sure I did a happy dance or two.

But how did I do it?

Believe it or not, the process of compiling the Linux kernel isn't nearly as hard as you might think. It is time-consuming, but it's a challenge you can certainly overcome.

Why would you want to compile the kernel?

There are three reasons why you might want to do this:

You want to use a newer or different kernel than your distribution provides (such as a real-time or low-latency kernel).You need a kernel with very specific configurations and only specific modules.You want to brag to your friends that you compiled a Linux kernel.

Also: This is the best-looking Linux desktop of 2025 (so far)

Let me show you how it's done.

How to compile the Linux kernel from source

What you'll need: To compile a kernel, you'll need to download the kernel source, a user with sudo privileges, and plenty of time. I'm going to demonstrate this on an instance of Ubuntu Desktop 24.04. If you're using a distribution not based on Debian or Ubuntu, you'll need to make sure to install the necessary dependencies with your distribution's package manager.

Before you embark on this journey, find a spare machine or deploy a virtual machine. You do not want to compile your first Linux kernel on a production machine.

The first thing you're going to do is download the source of the kernel you want to use. I would suggest going straight to The Linux Kernel Archives and downloading the tarball file of the kernel you want. I'm going to download the 6.13 source for demonstration purposes.

Show more

Open a terminal window and issue the following command to install the dependencies:

Show more

sudo apt-get install build-essential libncurses-dev git bison flex libssl-dev -y

Open a terminal window and change into the directory housing the downloaded file and extract it with the command:

Show more

unxz --keep linux-XXX.tar.xz

Where XXX is the release number.

We can now extract the tar file with the command:

Show more

tar -xf linux-XXX.tar

Where XXX is the release number.

At this point, you can either use the generic configuration or you can manually edit the .conf file within the newly created directory (which will be named linux-XXX – where XXX is the release number). Another method you can use (which I would suggest for your first go-round) is copying your distribution's configuration file. 

Before you do this, change into the newly created folder with the command:

Show more

cd linux-XXX

Where XXX is the release number.

Next, copy the configuration file with the command:

cp /boot/config-"$(uname -r)" .config

Alternatively, you could manually configure the kernel by using the command:

make menuconfig

The above command opens the menuconfig utility, where you can browse and enable/disable various features. This is how I've always compiled kernels. This method does take some time (because there are a lot of modules to go through). If you go that route, make sure to save it to a file with:

make savedconfig

The above command will create a new directory housing your .config file.

Also: The first 5 Linux commands every new user should learn

After copying the configuration, you need to update the configuration file (because the one shipped with your distribution is most likely out of date). To do this, issue the command:

Show more

make oldconfig

The .config file is now updated for the kernel source you downloaded.

For users of Debian-based distributions, you'll need to disable the module that is used to sign kernel modules, because the certificate used for this is not included. To do this, issue the commands:

Show more

scripts/config --disable SYSTEM_TRUSTED_KEYS
scripts/config --disable SYSTEM_REVOCATION_KEYS

If you do not do this, the build will fail later in the process, which means you will have wasted a lot of time (been there, done that...too many times).

It's now time to build the kernel with the command:

Show more

make -j$(nproc)

This will take quite a long time to complete.

You don’t want every Linux kernel module loading at bootup. Instead, we build loadable modules. This process not only installs the kernel modules but also signs them, which means the process will (once again) take some time. The command for this is:

Show more

sudo make modules_install -j$(nproc)

There are some instances when you might need the kernel header files (such as if you were going to write your own modules or install certain server software). For that, issue the command:

Show more

sudo make headers_install

It's now time to install the kernel, which is done with the command:

Show more

sudo make install

The ramdisk (initial RAM filesystem) is required for booting and can be created with:

Show more

sudo dracut --force /boot/initrd.img-XXX-generic XXX-generic

Where XXX is the release number of the kernel you're installing.

Finally, update the grub bootloader (so it's aware of your new kernel) with:

Show more

sudo update-grub2

You can now reboot your machine and select the newly compiled kernel.

Also: The best Linux laptops you can buy

Remember, do this on a virtual machine or a test machine because you don't want to monkey around with your daily driver and wind up with a machine that won't boot.

Get the morning's top stories in your inbox each day with our Tech Today newsletter.

Read Entire Article