DEV Community

wx-yz
wx-yz

Posted on • Edited on

HOWTO: Compile Linux kernel (on Ubuntu, applies to any distro)

Happy CPUs when compiling the kernel

Happy CPUs when compiling the kernel

Ubuntu 20.04 comes with Linux kernel 5.4.0-37. I had to reboot every time my machine wake up from sleep to fix screen flickering issues. Latest stable kernel (5.7.2) fixed this issue for me.

Notes,

  • It's easy to start with a working config and test the kernel first
  • If it's working, move on to remove things you don't need. This will result in a smaller kernel
  • Kernel modules built against the running kernel have to be recompiled. Notably Nvidia drivers and VirtualBox

Steps to compile the kernel,

  1. Download latest stable from kernel.org

  2. Unzip
    ➜ tar xf linux-5.7.2.tar.xz && cd linux-5.7.2

  3. Copy existing config (make things easy)
    ➜ cp /boot/config-$(uname -r) .config

  4. Use terminal based configuration tool
    ➜ make menuconfig
    If you have relatively recent hardware, this step will take most of your time. Add/remove stuff based on your hardware

  5. Compile the kernel with Clang (Remove CC for compiling with gcc - the default C compiler)
    make CC=clang -j12

  6. Install the kernel
    sudo make install

  7. Update grub
    sudo update-grub

  8. Reboot
    sudo reboot

Top comments (2)

Collapse
 
geoeduca profile image
GeoEduca

according to your experience, because it is better to compile,
like this: make CC=clang -j4
and not, like this: make -j4
Or is it that there are factors that determine when to use one or the other ?

Collapse
 
mccurcio profile image
Matt Curcio

These look like notes I make for my own benefit. ;))