JRehkemper.de

Build the Linux Kernel from Source

Since the Linux Kernel is Open Souce you can download it for free and compile your own version of it.
This can have advantages even if you do not change the code, since you can decide which drivers will be implemented in the kernel and which will compile as modules which are loaded at runtime. You can also decide to not compile some drivers at all. This can make your system more lightweight, since Mainstream Distributions have to include a prefferer of drivers to support most hardware. You on the other hand know what hardware you are using and can only included required drivers.
Since the Kernel is quite a big project the compile time on a decent machine will lie between 20 minutes to an hour. So take your time.

Dependencies

First of all we need to install the dependencies to compile the Kernel

sudo yum install gcc make git ctags ncurses-devel openssl-devel
# or
sudo apt-get install libncurses5-dev gcc make git exuberant-ctags bc libssl-dev
# or
sudo zypper in git gcc ncurses-devel libopenssl-devel ctags cscope

Download the source code

The source code is hosted on kernel.org. Here you will find many different branches. Go ahead and pick one and either clone the git or download the tar-ball.

Configuration

Next we need to create a configuration. This is the part where you can customize your kernel.

A good starting point is to use the configuration your distibution used. Therefore copy the configuration file into the directory of your downloaded source code.

cp /boot/config-`uname -r`* .config

If you want to start from scratch you can also generate a new config file.

make defconfig

Now you can customize the config to your liking. You can use the tool menuconfig for that. It will open a tui which will let you select and deselect differnt parts of the kernel.

menuconfig

Compiling

Now that we have configured the kernel, we can start to compile. If you have a multicore processor you should use multiple threads while compiling to speed things up.

make -j <number of threads>

Now we just have to wait.

Installing the Kernel

Once the Kernel is compiled, we need to install it, so that we can select it the grub menu.

sudo make modules_install install

And then update the grub config. The command differs depending on your distribution, but it should be one of the two below.

sudo update-grub2
# or
sudo grub2-mkconfig -o /boot/grub2/grub.cfg

If you now reboot your system you should be able to select your new kernel.

profile picture of the author

Jannik Rehkemper

I'm an professional Linux Administrator and Hobby Programmer. My training as an IT-Professional started in 2019 and ended in 2022. Since 2023 I'm working as an Linux Administrator.