Steps to build the kernel from source
As a convenient and quick reference, the following are the main, key steps required to build a Linux kernel from source. As the explanation for each of them is pretty detailed, you can refer back to this summary to see the big picture. The steps are as follows:
- Obtain a Linux kernel source tree through either of the following options:
- Downloading a specific kernel source tree as a compressed file
- Cloning a (kernel) Git tree
- Extract the kernel source tree into some location in your home directory (skip this step if you obtained a kernel by cloning a Git tree).
- Configure: Get a starting point for your kernel config (the approach varies). Then edit it, selecting the kernel support options as required for the new kernel. The recommended way of doing this is with
make menuconfig
. - Build the kernel image, the loadable modules, and any required Device Tree Blobs (DTBs) with
make [-j'n'] all
. This builds the compressed kernel image (arch/<arch>/boot/[b|z|u]{Ii}mage
), the uncompressed kernel image– vmlinux
, theSystem.map
file, the kernel module objects, and any configured DTB files. - Install the just-built kernel modules (on x86) with
sudo make [INSTALL_MOD_PATH=<prefix-dir>] modules_install
. This step installs kernel modules by default under/lib/modules/$(uname -r)/
(theINSTALL_MOD_PATH
environment variable can be leveraged to change this). - Bootloader (x86): Set up the GRUB bootloader and the
initramfs
, earlier calledinitrd
, image:sudo make [INSTALL_PATH=</new/boot/dir>] install
- This creates and installs the
initramfs
orinitrd
image under/boot
(theINSTALL_PATH
environment variable can be leveraged to change this). - It updates the bootloader configuration file to boot the new kernel (first entry).
- This creates and installs the
- Customize the GRUB bootloader menu (optional).
This chapter, being the first of two on this kernel build topic, will cover steps 1 to 3, with a lot of required background material thrown in as well. The next chapter will cover the remaining steps, 4 to 7. So, let’s begin with step 1.