[HowTo] Build your very first custom kernel

Difficulty: ★★★☆☆

  1. Before you begin, grab the Linux kernel source. Go ahead and head to kernel.org, and choose the kernel you want to compile. I’ll use the mainline kernel as an example, which is 5.11-rc2 at the time of writing this post. You can grab it with git clone or wget the download link to it if you want.

  2. Extract the archive:

    bsdtar -xvf [kernel-archive-file]

    Where, obviously you have to replace [kernel-archive-file] with the name of the compressed archive of the kernel.

  3. cd into the directory it was extracted in. And run the following if you want to use the config that came with your distro. (RECOMMENDED)

     zcat /proc/config.gz >> .config
  4. If you want to configure some extra features with the stock config, run the following:

    make menuconfig
    • At this point, you’ll be greeted with a menu showing a bunch of options. If you don’t know what you’re doing, exit out and leave it.
  5. Now, it’s time to compile it!

    make -j$(nproc)

    Where $(nproc) will use all the CPU pipelines available so your build should go faster.

  6. After the compilation finises, it’s time to install the modules:

    sudo make modules_install
  7. Now, you’re ready to install the kernel file and the System.map. Run the following command to install them:

    sudo make install
  8. The following commands need a root shell for what we’re about to do, so:

    sudo su
  9. As we’re in the root shell now, we want to copy vmlinuz to vmlinuz-[kernel-version]-[arch]

    cp /boot/vmlinuz /boot/vmlinuz-[kernel-version]-[arch]
    • Replace [kernel-version]-[arch] with the kernel version (E.g: linux-5.11-rc2-x86_64)
    • If you do not perform the above command you won’t be able to reboot!
  10. Now, we’re going to generate initramfs.
    Arch Based distros use mkinitcpio, which is a bash script to automate the generation of the initramfs.
    Therefore, we want to make custom presets for the kernel. These are usually located in /etc/mkinitcpio.d, so we’ll use a template from another version of the kernel for our newly compiled version:

    # mkinitcpio preset file for the 'linux' package
    PRESETS=('default' 'fallback')
    fallback_options="-S autodetect"
  11. Edit the [kernel-version]-[arch] with the kernel version you compiled and the arch of your cpu. E.G: x86_64 and save this to a preset in /etc/mkinitcpio.d/[kernel-version].preset:

    sudo mkinitcpio -p linux[kernel-version].preset
    • If you get “no modules were added, this is probably not what you wanted”, please use the config that came with the distro, and recompile your kernel. Then follow the steps above again.

    • If everything goes well, you can reboot and try out your new kernel! If you boot successfully.

    • To verify the kernel version, execute::

      uname -a 
    • If you see the kernel version you compiled, congrats! You just successfully compiled your first Linux kernel!



Nice guide :+1::+1:. If you are using nvidia drivers can you use mhwd/manjaro settings gui to add the drivers to a custom kernel?


As far as I know you can.

And thank you!! :smiley:

Thanks to @Yochanan and @Fabby with formatting and etc!!! :smiley:

1 Like