How to build the Ubuntu Linux Kernel¶
If you have patches you need to apply to the Ubuntu Linux kernel, or you want to change some kernel configs, you may need to build your kernel from source. Follow these steps to build the Ubuntu Linux kernel.
Prerequisites¶
It is recommended to have 8GB of RAM or more to build the Linux kernel. This guide supports Xenial and newer.
If this is the first time you are building a kernel on your system, you will need to set up your build environment and install the required packages.
Otherwise, skip ahead to Obtain the source for an Ubuntu release.
Set up build environment¶
To build an Ubuntu kernel, you will need enable the necessary source
repositories in the sources.list
or ubuntu.sources
file.
Add "deb-src" to the Types:
line in the
/etc/apt/sources.list.d/ubuntu.sources
file.
Types: deb deb-src
URIs: http://archive.ubuntu.com/ubuntu
Suites: noble noble-updates noble-backports
Components: main universe restricted multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
Check that you have the following entries in the
/etc/apt/sources.list
file. If not, add or uncomment these lines
for your Ubuntu release.
deb-src http://archive.ubuntu.com/ubuntu jammy main
deb-src http://archive.ubuntu.com/ubuntu jammy-updates main
Install required packages¶
To install the required packages and build dependencies, run:
sudo apt update
sudo apt build-dep -y linux linux-image-unsigned-$(uname -r)
sudo apt install -y fakeroot llvm libncurses-dev dwarves
Obtain the source for an Ubuntu release¶
There are different ways to get the kernel sources, depending on the kernel version you want to make changes to.
Get kernel source for version installed on build machine¶
Use the apt source
command to get the source code for the kernel version
currently running on your build machine.
apt source linux-image-unsigned-$(uname -r)
This will download and unpack the kernel source files to your current working directory.
Get kernel source for other versions¶
Use git to get the source code for other kernel versions. See Obtaining the kernel sources for an Ubuntu release using git for more information.
Prepare the kernel source¶
Once you have the kernel source, run the following commands to ensure you have a clean build environment and the necessary scripts have execute permissions:
cd <kernel source working directory>
chmod a+x debian/rules
chmod a+x debian/scripts/*
chmod a+x debian/scripts/misc/*
fakeroot debian/rules clean
When you build the kernel, you should modify the version number so you can
differentiate this kernel from the kernel provided by Canonical and
avoid conflicts. Modify the ABI number (the number after the dash in the
version) to “999” in the
<kernel source working directory>/debian.master/changelog
file. If
you’re building something other than the generic linux kernel, use the
<kernel source working directory>/debian.<kernel derivative>/changelog
file.
Modify the configuration¶
Sometimes you may need to enable or disable a kernel feature using a kernel config. If you don’t need to enable any new features or disable a feature provided by the Ubuntu Linux kernel, you may skip this section.
The following commands will run menuconfig
to allow you to edit the
configurations. If editing the configs, you will need to respond Y or N to
each. Otherwise you might get an error later in the build process.
cd <kernel source working directory>
fakeroot debian/rules editconfigs
Build the kernel¶
Run the following commands to build the kernel:
cd <kernel source working directory>
fakeroot debian/rules clean
fakeroot debian/rules binary
If the build is successful, several .deb binary package files will be produced in the directory above the working directory.
For example, after building a kernel with version “6.8.0-999.36” on an amd64 system, these three (or more) .deb packages will be produced:
linux-headers-6.8.0-999_6.8.0-999.36_all.deb
linux-headers-6.8.0-999-generic_6.8.0-999.36_amd64.deb
linux-image-unsigned-6.8.0-999-generic_6.8.0-999.36_amd64.deb
linux-modules-6.8.0-999-generic_6.8.0-999.36_amd64.deb
Install the new kernel¶
Install the debian packages (on your build system, or a different target
system) with dpkg -i
and then reboot:
cd <kernel source working directory>/../
sudo dpkg -i linux-headers-<your kernel version>*_all.deb
sudo dpkg -i linux-headers-<your kernel version>-<generic or your kernel derivative>*.deb
sudo dpkg -i linux-image-unsigned-<your kernel version>-<generic or your kernel derivative>*.deb
sudo dpkg -i linux-modules-<your kernel version>-<generic or your kernel derivative>*.deb
sudo reboot
Test the new kernel¶
Run any necessary testing to confirm that your changes have taken effect.
You should also
confirm that your kernel version matches what is in your
<kernel source working directory>/debian.master/changelog
file by using the following
command:
uname -r