This guide outlines the process of building and installing a custom kernel from source code on Ubuntu 18.04, using kernel version 5.15.7 as an example. The steps are generally applicable to other kernel versions and similar Debian-based distributions.
- Obtain the Kernel Source Code
You can download the official kernel source either via HTTP or through Git.
To fetch the stable release archive:
wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.15.7.tar.xz
Alternatively, clone the mainline repository using Git:
git clone https://github.com/torvalds/linux.git
- Extract the Archive
If using the tarball, decompress it with:
tar -xf linux-5.15.7.tar.xz
Then navigate into the source directory:
cd linux-5.15.7
- Install Build Dependencies
Ensure all required tools and libraries are installed for a successful compilation:
sudo apt update
sudo apt install build-essential libncurses-dev bison flex libssl-dev
- build-essential: Includes GCC, make, and other core build tools.
- libncurses-dev: Required for the menuconfig interface.
- bison and flex: Needed for parsing grammar during compilation.
- libssl-dev: Used for signing modules if enabled.
- Configure Kernel Options
Prepare a clean build environment and launch the configuration interface:
make distclean
make menuconfig
The make menuconfig command opens an interactive text-based menu where you can enable or disable kernel features. For first-time builds, consider loading the current system's configuration as a base:
cp /boot/config-$(uname -r) .config
make olddefconfig
make menuconfig
After making selections, save the configuration and exit.
- Compile the Kernel
Start the compilation process. Use the -j flag to speed up the build by utilizing multiple CPU cores:
make -j$(nproc)
This dynamically sets the number of parallel jobs based on available processors.
- Enstall Kernel Modules and Image
Once compilation completes, install the generated modules and kernel image:
sudo make modules_install
sudo make install
The make install step automatically copies the kernel image (vmlinuz) and related files to /boot, and creates appropriate initramfs entries.
- Update GRUB Bootloader
Refresh the GRUB configuration to include the newly installed kernel:
sudo update-grub
- Reboot and Select New Kernel
Restart the system:
sudo reboot
During boot, hold Shift (for BIOS systems) or press Esc (for UEFI with GRUB timeout set) to access the GRUB menu. Choose "Advanced options for Ubuntu" and select the compiled kernel entry.
- Verify Kernel Version
After logging in, confirm the active kernel version:
uname -r
If the output matches your built version (e.g., 5.15.7), the installation was successful.