Compiling the Linux kernel from source gives you a system tuned to your hardware and needs. This guide provides practical tips for a smooth build, from preparation to installation, and answers common questions. You will learn how to configure, build, and install a custom kernel without breaking your system.
Why Compile a Custom Kernel?
Most users never need to compile a kernel—distro kernels are well-tested and optimized for general use. However, there are good reasons to do it yourself:
- Hardware support: Enable a driver not included in your distro’s kernel, or disable ones you don’t need to reduce memory usage.
- Performance: Optimize for your specific CPU (e.g., enable
CONFIG_MNATIVE_INTELorCONFIG_MNATIVE_AMD) or reduce kernel size. - Security: Apply patches or enable security features not yet in your distro’s kernel.
- Learning: Understand how the kernel works and what your system needs.
- Testing: Try new features or bug fixes before they hit your distro.
Prerequisites: What You Need Before You Start
Before downloading anything, check that you have the necessary tools and space.
- Disk space: At least 20 GB free (kernel source is ~1.5 GB, build with debug info can exceed 10 GB).
- RAM: 4 GB minimum; 8 GB or more speeds up compilation significantly.
- Build tools: Install
build-essential(Debian/Ubuntu) orbase-devel(Arch), pluslibncurses-dev(for menuconfig),flex,bison,bc, andlibssl-dev(for signing). - Source code: Download the latest stable kernel from kernel.org or use your distro’s source package.
Configuration: The Most Important Step
The kernel has thousands of options. The safest approach is to start from your current kernel’s config and modify it.
- Copy the current config:
cp /boot/config-$(uname -r) .config(Debian/Ubuntu) orzcat /proc/config.gz > .config(if enabled). - Run
make menuconfig(ornconfigfor a nicer interface). This loads the existing config and lets you tweak options. - Enable new hardware: Use
/to search for specific symbols (e.g.,CONFIG_NVME). - Disable unused drivers: This reduces compile time and kernel size, but be careful—disable only what you are sure about.
- Save and exit. The config is saved as
.config.
Tip: For a first build, keep the config close to your distro’s default. You can always rebuild later.
Compilation: How Long Does It Take?
Compilation time varies wildly based on CPU cores and disk speed. A modern 8-core CPU with an SSD can build a default kernel in 10–20 minutes. Older or fewer cores may take over an hour. Use all cores with make -j$(nproc) but be aware that this can use a lot of RAM (about 1–2 GB per core). If you have 4 GB RAM, use -j2 instead.
If you want to speed up subsequent builds, enable CONFIG_CCACHE and install ccache. This caches compiled objects and can cut rebuild times by 50% or more.
Installing the Kernel: Modules and Bootloader
After compilation, you need to install modules and the kernel image.
- Install modules:
sudo make modules_install(installs to/lib/modules/<version>). - Install kernel:
sudo make install(copiesvmlinuzandSystem.mapto/bootand updates the bootloader). - Update bootloader: Most distros run
update-grubautomatically. If not, run it manually.
Critical: Always keep your previous kernel in the bootloader menu. If the new one fails to boot, you can select the old one.
Common Problems and How to Fix Them
Here are frequent issues and their solutions:
- “No rule to make target” errors: Usually missing dependencies. Install the build tools listed above.
- Out of memory during compile: Reduce
-jjobs or add swap space. - Kernel panics on boot: Often due to missing filesystem driver. Ensure you enabled your root filesystem’s driver (e.g.,
CONFIG_EXT4_FS) and that it is built-in (y), not a module (m). - Wireless or graphics not working: Missing firmware. Install
linux-firmwarepackage. - Modules not loading: Run
sudo depmod -aafter installing modules.
Alternatives to Compiling from Source
If compiling feels daunting, consider these alternatives:
- Use a distro kernel with newer backports: Ubuntu’s HWE kernels, Arch’s
linux-lts, or Fedora’s latest stable. - Use a kernel from a PPA or COPR: e.g.,
xanmodorliquorixfor performance. - Use a kernel manager like
ukkuormainline: These download and install official kernels automatically. - Use a kernel build script: Tools like
kernel-builderautomate config and build.
Frequently Asked Questions
Do I need to compile the kernel to get better performance?
Not usually. Most users see negligible performance gains from a custom kernel. The benefits are more about hardware support and reducing bloat. If you are chasing performance, first try a performance-oriented distro kernel or CPU governor tweaks.
How do I revert to my old kernel if the new one fails?
Reboot and select the previous kernel from the GRUB menu. If GRUB doesn’t show, hold Shift (BIOS) or press Esc (UEFI) during boot. Once booted, you can remove the new kernel with sudo apt purge linux-image-<version> (Debian/Ubuntu) or sudo pacman -R linux-<version> (Arch).
What is the difference between make and make modules_install?
make builds the kernel image and modules. make modules_install copies the modules to /lib/modules. You must run both—first make, then make modules_install, then make install.
Should I use make oldconfig or make menuconfig?
oldconfig asks questions for every new option since your last config—useful for upgrading. menuconfig gives a graphical interface to change any option. For a fresh build, start with oldconfig to get a working base, then fine-tune with menuconfig.
How do I know which config options to enable for my hardware?
Use lspci -k and lsusb to see your hardware. Then search for the corresponding driver symbol in menuconfig. For example, an Intel Wi-Fi card might need CONFIG_IWLWIFI. You can also run make localmodconfig to automatically enable only the modules currently loaded—this is a great starting point.
Is it safe to compile a kernel on a system I use daily?
Yes, as long as you keep your old kernel available. The build process does not affect your running system until you install the new kernel. Even then, the old kernel remains in /boot and in the bootloader menu. Just be careful not to delete it.
Related
General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.
More in this bed
- PowerPoint vs Keynote: Key Differences
- How to Sign Out of OneDrive in Windows 10
- Reduce Memory Use in Windows When RAM Is Low
- How to Set Up and Use Credential Manager on Windows 10
- Fix Microsoft Edge Battery Drain on Windows 11
- Type to Siri Instead of Talking on iPhone (iOS 11)