Linux HowTo

How to compile a new Linux kernel

Step-by-step practical guide: preparation, configuration, compilation, installation, initramfs, bootloader update, and rollback if problems arise.

If you don't want to compile by hand, try my automatic build program AV kernel gui: you can find it in the Download section.
This guide is focused on Slackware. The steps show kernel compilation and initrd management with mkinitrd, with notes for LILO and GRUB.

1. Before you start

  • Work as root or use sudo where required.
  • Keep at least one working kernel in the bootloader (do not replace the only one).
  • Make sure you have free space (a few GB in /usr/src and /boot).
  • Note the current kernel:
uname -r
uname -m

2. Required packages

Install build tools and base libraries. Package names may vary.

Typical packages

  • gcc, make, binutils
  • bc, bison, flex
  • openssl-devel / libssl-dev
  • elfutils, pahole (for BTF)
  • perl, python (some targets/scripts)
  • ncurses-devel / libncurses-dev (for menuconfig)

Useful tools

  • wget or curl to download sources
  • xz / tar to extract archives
  • git if you build from kernel.org repository
  • mkinitrd (Slackware) to create initrd for the generic kernel

3. Get the kernel sources

You can use your distro sources or download the official tarball. Example with tarball:

cd /usr/src
wget https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.x.y.tar.xz
tar -xf linux-6.x.y.tar.xz
ln -sfn linux-6.x.y linux
cd linux

Replace 6.x.y with the version you want to compile.

4. Backup the current configuration

The safest way is to start from the current kernel configuration.

zcat /proc/config.gz > .config

If /proc/config.gz does not exist, try the distro file:

cp /boot/config-$(uname -r) .config
Make a safety copy before editing:
cp .config .config.backup-$(date +%F)

5. Configure the kernel

  1. Update the old configuration for new sources:
make oldconfig

If you prefer automatic defaults for new options:

make olddefconfig
  1. Open the text configurator:
make menuconfig

Check especially:

  • Disk/controller drivers (SATA/NVMe)
  • Root filesystem (ext4, xfs, btrfs, etc.)
  • EFI/UEFI support if used
  • Network (LAN/Wi-Fi)
  • CPU microarchitecture (do not overdo if you want compatibility)
If you compile as modules the drivers required to mount root, you must have a correct initramfs. If unsure, keep the distro configuration and change only a few options.

6. Build the kernel and modules

Clean (only if you are starting from a previously used tree) and build:

make -j"$(nproc)"

Build and install modules into /lib/modules/<version>:

make modules_install
Compilation can take from a few minutes to a long time depending on CPU, number of cores, and configuration.

7. Install the kernel

Install kernel, System.map, and config into /boot:

make install

On some distros make install also updates the bootloader automatically; on others it does not.

Verify what was created:

ls -lh /boot
ls -lh /lib/modules | tail

8. Create initramfs (if needed)

On Slackware, if you use a generic kernel (recommended), you must create the initrd for the new kernel.

Slackware (mkinitrd)

mkinitrd -c -k <new-kernel-version> -f ext4 -r /dev/sdXY -m ahci:ext4

Adjust filesystem, root device, and modules to your real values.

For a more precise command it is also recommended to use:

/usr/share/mkinitrd/mkinitrd_command_generator.sh -r

9. Update the bootloader

Do not remove the previous kernel entry. Add or regenerate while keeping at least one fallback entry.

GRUB

grub-mkconfig -o /boot/grub/grub.cfg

LILO (still common on Slackware)

Edit /etc/lilo.conf adding a new image and then:

lilo -v
If you use LILO and forget to run lilo after changes, the system may still point to the old kernel or not boot the new entry.

10. Reboot and verify

  1. Reboot the system.
  2. Select the new kernel from the bootloader.
  3. After login verify:
uname -r
dmesg | less
lsmod | head

Check network, audio, filesystems, video drivers, and main services.

11. Rollback procedure (if something goes wrong)

  1. Reboot.
  2. Select the previous kernel from the bootloader.
  3. Fix the configuration and recompile, or remove the new entry from the bootloader.
Best practice: keep at least 2 working kernels (current + previous) and their initramfs in /boot.

12. Practical Slackware note

On Slackware it is often better to:

  • Start from the config of the installed huge/generic kernel.
  • Use a generic kernel + initrd for daily use.
  • Update /etc/lilo.conf carefully (or GRUB if used).
  • Keep a “rescue” entry or the old kernel.

Final checklist

[ ] Sources downloaded/extracted
[ ] .config copied from current kernel
[ ] make oldconfig / menuconfig completed
[ ] make && make modules_install && make install
[ ] initramfs created (if required)
[ ] bootloader updated
[ ] old kernel kept as fallback
[ ] reboot and tests completed