A Step-by-Step Guide to Compiling the Linux Kernel from Source
Jul 30, 2025 am 01:26 AMInstall required tools using sudo apt install build-essential libncurses-dev bison flex libssl-dev libelf-dev. 2. Download kernel source from kernel.org or via git clone. 3. Copy current config with cp /boot/config-$(uname -r) .config and run make olddefconfig. 4. Optionally customize with make menuconfig. 5. Compile with make -j$(nproc). 6. Install modules with sudo make modules_install. 7. Install kernel with sudo make install, which updates the bootloader. 8. Reboot and verify with uname -r. Always keep the old kernel for fallback, use LOCALVERSION to customize version string, clean builds with make clean or make mrproper, and backup .config for future use. The process is time-consuming but straightforward when following these steps carefully, and it ends with a successfully booted custom kernel.
Compiling the Linux kernel from source might sound intimidating, but it's a manageable process once you know the steps. Whether you're doing it to enable specific features, improve performance for your hardware, or just to learn how the kernel works, this guide walks you through the entire process on a typical Ubuntu-based system. While distro-specific details may vary slightly, the core steps are the same across most Linux distributions.

1. Install Required Tools and Dependencies
Before you start, make sure your system has the necessary tools to compile the kernel. You’ll need compilers, development libraries, and other build utilities.
On Ubuntu or Debian-based systems, run:

sudo apt update sudo apt install build-essential libncurses-dev bison flex libssl-dev libelf-dev
These packages provide:
build-essential
: GCC compiler and related toolslibncurses-dev
: Needed for menuconfig (text-based configuration interface)bison
andflex
: Parser and lexical analyzer generatorslibssl-dev
: Required for signing modules (if enabled)libelf-dev
: Used for ELF object file support
2. Download the Kernel Source Code
You can get the latest stable kernel from kernel.org.

Go to your home directory (or a dedicated folder) and download the latest source:
cd ~ wget https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.6.1.tar.xz tar -xf linux-6.6.1.tar.xz cd linux-6.6.1
? Tip: Replace the version number with the latest stable release available at the time.
Alternatively, use git
to clone the official repository (if you want more control or plan to contribute):
git clone https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
3. Configure the Kernel
This is one of the most important steps. You need to decide which features, drivers, and options to include in your custom kernel.
Start with Your Current Configuration
The easiest way is to base your new kernel config on your current running one:
cp /boot/config-$(uname -r) .config
Then update it for any new options in the newer kernel:
make olddefconfig
olddefconfig
uses default values for new options instead of prompting you — useful for automation.
Customize Using Menuconfig (Optional)
If you want to tweak settings (e.g., disable unused drivers or enable experimental features), use:
make menuconfig
This opens an interactive menu where you can navigate and modify kernel options. Save and exit when done.
?? Common changes: Enable BPF, disable unused filesystems, optimize for your CPU type under "Processor type and features".
4. Compile the Kernel
Now it’s time to build the kernel and modules. This step can take 20 minutes to over an hour, depending on your CPU and number of cores.
To speed things up, use multiple threads (-j$(nproc)
uses all available cores):
make -j$(nproc)
This compiles:
- The kernel image (e.g.,
arch/x86/boot/bzImage
) - Loadable kernel modules
?? If you get errors, double-check that all dependencies are installed and your
.config
is valid.
5. Install the Kernel Modules
Once compilation finishes, install the modules (drivers, filesystems, etc.) into /lib/modules
:
sudo make modules_install
This copies compiled modules to /lib/modules/<kernel-version>/
.
6. Install the Kernel Image
Next, install the actual kernel image so the bootloader can find it:
sudo make install
This command:
- Copies
bzImage
to/boot/vmlinuz-<version>
- Creates an initramfs using
dracut
ormkinitramfs
- Updates the bootloader configuration (on Debian/Ubuntu, via
update-grub
)
? On Ubuntu/Debian,
make install
automatically runsupdate-initramfs
andgrub-mkconfig
.
If you're on another distro or the boot entry isn't added, you may need to manually update GRUB:
sudo update-grub
7. Reboot into Your New Kernel
Reboot your system and select the new kernel from the GRUB menu (if multiple entries exist).
After booting, verify the kernel version:
uname -r
You should see the version number of the kernel you just compiled.
Tips and Best Practices
Keep your old kernel: Don’t remove old kernel entries until you confirm the new one works. This ensures you can boot into a working system if something goes wrong.
Version naming: To avoid confusion with distribution kernels, consider customizing the kernel version string. Edit the
LOCALVERSION
in.config
or run:make LOCALVERSION=-mykernel -j$(nproc)
Clean up builds: Use
make clean
(removes generated files but keeps config) ormake mrproper
(full cleanup) when starting fresh.Backup your config: Save your
.config
file somewhere safe for future use:cp .config ~/kernel-config-6.6.1-custom
Compiling the Linux kernel isn’t something most users need to do daily, but it’s a powerful skill for developers, system administrators, or enthusiasts. Once you’ve done it once, the process becomes straightforward. Just remember to test thoroughly before relying on it in production.
Basically, it's a bit time-consuming, but not complicated — especially when you stick to small, safe changes at first.
The above is the detailed content of A Step-by-Step Guide to Compiling the Linux Kernel from Source. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

LXD is described as the next-generation container and virtual machine manager that offers an immersive for Linux systems running inside containers or as virtual machines. It provides images for an inordinate number of Linux distributions with support

Clear Linux OS is the ideal operating system for people – ahem system admins – who want to have a minimal, secure, and reliable Linux distribution. It is optimized for the Intel architecture, which means that running Clear Linux OS on AMD sys

The key steps for creating a self-signed SSL certificate are as follows: 1. Generate the private key, use the command opensslgenrsa-outselfsigned.key2048 to generate a 2048-bit RSA private key file, optional parameter -aes256 to achieve password protection; 2. Create a certificate request (CSR), run opensslreq-new-keyselfsigned.key-outselfsigned.csr and fill in the relevant information, especially the "CommonName" field; 3. Generate the certificate by self-signed, and use opensslx509-req-days365-inselfsigned.csr-signk

Firefox browser is the default browser for most modern Linux distributions such as Ubuntu, Mint, and Fedora. Initially, its performance might be impressive, however, with the passage of time, you might notice that your browser is not as fast and resp

Decompress the .zip file on Windows, you can right-click to select "Extract All", while the .tar.gz file needs to use tools such as 7-Zip or WinRAR; on macOS and Linux, the .zip file can be double-clicked or unzip commanded, and the .tar.gz file can be decompressed by tar command or double-clicked directly. The specific steps are: 1. Windows processing.zip file: right-click → "Extract All"; 2. Windows processing.tar.gz file: Install third-party tools → right-click to decompress; 3. macOS/Linux processing.zip file: double-click or run unzipfilename.zip; 4. macOS/Linux processing.tar

When encountering DNS problems, first check the /etc/resolv.conf file to see if the correct nameserver is configured; secondly, you can manually add public DNS such as 8.8.8.8 for testing; then use nslookup and dig commands to verify whether DNS resolution is normal. If these tools are not installed, you can first install the dnsutils or bind-utils package; then check the systemd-resolved service status and configuration file /etc/systemd/resolved.conf, and set DNS and FallbackDNS as needed and restart the service; finally check the network interface status and firewall rules, confirm that port 53 is not

As a system administrator, you may find yourself (today or in the future) working in an environment where Windows and Linux coexist. It is no secret that some big companies prefer (or have to) run some of their production services in Windows boxes an

If you find that the server is running slowly or the memory usage is too high, you should check the cause before operating. First, you need to check the system resource usage, use top, htop, free-h, iostat, ss-antp and other commands to check CPU, memory, disk I/O and network connections; secondly, analyze specific process problems, and track the behavior of high-occupancy processes through tools such as ps, jstack, strace; then check logs and monitoring data, view OOM records, exception requests, slow queries and other clues; finally, targeted processing is carried out based on common reasons such as memory leaks, connection pool exhaustion, cache failure storms, and timing task conflicts, optimize code logic, set up a timeout retry mechanism, add current limit fuses, and regularly pressure measurement and evaluation resources.
