In this guide, we will walk through the steps to download, configure, compile, and install a custom Linux kernel on an Ubuntu system. We’ll cover everything from installing necessary tools to building the kernel and updating the bootloader to use the new kernel. This process allows you to customize the kernel for specific hardware, performance, or feature requirements.
Prerequisites
Before you start, you’ll need to have a few tools and libraries installed on your Ubuntu system. These tools will allow you to compile and configure the kernel source.
-
Install Required Packages
Open a terminal and run the following command to install the essential development tools:sudo apt update sudo apt install build-essential libncurses-dev bison flex libssl-dev bc libelf-dev
These packages provide the necessary tools for kernel compilation, including compilers, libraries, and configuration utilities.
-
Install Git (Optional but recommended) If you plan to clone the latest kernel from Git, you’ll need
git
installed:sudo apt install git
Step 1: Download the Latest Kernel Source
The easiest way to get the latest stable kernel source is by using Git to clone the official kernel repository from GitHub. You can do this by running the following commands:
git clone https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
cd linux
This will download the latest kernel source code into the linux
directory.
Alternatively, if you prefer, you can download the latest stable kernel tarball from kernel.org. However, we will focus on using Git for simplicity, as it ensures you're working with the latest code.
Step 2: Configure the Kernel
Now that you have the kernel source, you need to configure it. The configuration step determines which features and drivers will be enabled in your custom kernel.
-
Copy the Current Kernel Configuration
To avoid starting from scratch, you can copy your current running kernel configuration. This ensures that your new kernel will support your hardware. Run the following command to copy your current kernel's config:cp /boot/config-$(uname -r) .config
-
Run
make menuconfig
This command launches a text-based menu to customize your kernel configuration. If you just want to tweak certain options (like enabling/disabling specific drivers or features), you can usemake menuconfig
.make menuconfig
The
menuconfig
interface will allow you to select various options. However, if you're happy with the default configuration (after copying your existing config), you can skip this step. -
Optionally, Update Configuration for New Options
If you want to ensure your configuration is up to date with new kernel options, you can run:make oldconfig
This will prompt you for any new configuration options that have been added since the last kernel version you were using. You can simply accept the defaults for most options by pressing Enter.
Step 3: Compile the Kernel
Once your configuration is ready, it’s time to compile the kernel. This step may take a while, depending on your system’s performance, as it involves building both the kernel and kernel modules.
-
Compile the Kernel
Use themake
command to start the build process. The-j$(nproc)
flag tellsmake
to use all available CPU cores, speeding up the compilation process:make -j$(nproc)
This will build the kernel as well as all the required modules. The
nproc
command returns the number of CPU cores on your system, ensuring the compile process uses all available resources. -
Compile Kernel Modules
Once the kernel itself is compiled, you need to compile and install the kernel modules (if you have any).make modules sudo make modules_install
Step 4: Install the New Kernel
After successfully compiling the kernel and modules, you can install them onto your system.
-
Install the Kernel
Use the following command to install the newly compiled kernel into the/boot
directory:sudo make install
This will copy the kernel image (
vmlinuz
), the kernel configuration file, and the System.map file to the/boot
directory. It will also update the bootloader configuration (GRUB) so the new kernel can be selected at boot. -
Install Kernel Modules
The previously compiled kernel modules need to be installed as well:sudo make modules_install
This will place the kernel modules into the appropriate directories under
/lib/modules
.
Step 5: Update GRUB Bootloader
Once the new kernel is installed, you need to update GRUB, the bootloader, so that it recognizes the new kernel.
-
Update GRUB
Run the following command to regenerate the GRUB configuration file, which includes the newly installed kernel:sudo update-grub
This command scans for all available kernels and updates the GRUB configuration accordingly.
Step 6: Reboot and Test the New Kernel
With everything installed, you can now reboot your system. During boot, GRUB should present you with the option to boot into the new kernel.
-
Reboot the System
Simply reboot your machine:sudo reboot
-
Select the New Kernel
If GRUB doesn’t automatically boot into the new kernel, you can manually select it by pressingEsc
during boot to access the GRUB menu. From there, select the new kernel. -
Verify the New Kernel
After rebooting, verify that you are running the newly compiled kernel by running the following command:uname -r
This will display the kernel version, and you should see the version you just compiled.
Troubleshooting
-
Missing Libraries or Dependencies: If you encounter errors about missing libraries (such as
libelf.h
orgelf.h
), make sure you have the necessary development packages installed. You can installlibelf-dev
(for Ubuntu) as follows:sudo apt install libelf-dev
-
Boot Issues: If the system doesn’t boot into the new kernel, you can always revert to the previous kernel using GRUB. Simply select an older kernel from the GRUB menu during boot.
Conclusion
By following these steps, you can compile and install a custom Linux kernel on your Ubuntu system. This process is useful for optimizing your system for specific hardware or enabling/disable kernel features that are not available in the default Ubuntu kernel. Whether you're a developer, system administrator, or power user, compiling your own kernel is an excellent way to fine-tune your system for performance, security, and custom functionality.