Building the Linux Kernel

This is a short post describing how to build and install the Linux kernel from source. There’s plenty of documentation elsewhere online that goes into more detail than I will here, so this is mainly as a reference for myself.

I’m using Fedora 34, but you can follow similar steps for other distributions.

First install the necessary packages needed for building the kernel.

$ sudo dnf group install "C Development Tools and Libraries"
$ sudo dnf install gcc make git ctags ncurses-devel kernel-devel

It’s likely that the group install for the C development tools installs most of what you need, but various places around the web have suggested the other packages specifically, so might as well include them just to be sure.

During compiling, my build failed a couple of times requiring me to install openssl as well as dwarves.

$ sudo dnf install dwarves openssl openssl-devel

Download the kernel source. You can download it to wherever you want, I usually just put it in my home folder.

$ git clone https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
$ cd linux

Once the kernel source has finished downloading, change into the linux directory.

First we need to copy our existing config file into the kernel source directory and then make the config.

$ cp /boot/config-`uname -r`* .config
$ make oldconfig

make oldconfig will require you to answer a bunch of questions about the various modules you want the kernel to load. Selecting the default for most should be ok unless you have a reason to do otherwise.

Next we build the kernel.

$ make bzImage

Depending on how fast your computer is this might take a while.

Once the kernel has finished compiling we need to build the modules that have been configured in the earlier steps.

$ make modules

On my machine compiling took a couple of hours, but I did walk away for a bit while it was running and came back to it finished, so it might be less.

Finally, if everything went according to plan you should now be able to install your fresh kernel.

$ sudo make modules_install install

As you can see by the following screenshot, I’m currently running on the 5.14.12-200 kernel build that was installed by Fedora.

Give your computer a reboot and you should boot into the new kernel.

A couple of times when I did this I tried booting into the new kernel and all I got was a black screen. I couldn’t work out what I was doing wrong and took a few times to get it right. Turns out I wasn’t configuring the kernel correctly in the first few steps, so while running make and make install showed no errors and the new kernel was showing in the grub boot menu, it wasn’t actually built properly.

Now that it’s working and I can boot into the new kernel, I just want a quick test to make sure things are working ok. Particularly VirtualBox, and USB.

Turning on a VM works as expected, and plugging in a USB device works as well. Wifi also works. One last test, I’ll close the laptop lid so the machine goes to sleep, and then open back up to ensure suspend and resume is all ok, which it is.

If you get stuck, the documentation is hugely useful. I recommend Building a custom kernel from the Fedora project and Kernel Build on the Kernel Newbies site.