Compiling the Kernel from Source - linux-surface/linux-surface GitHub Wiki
The following provides a short overview of how you can compile the linux-surface kernel for yourself. Compiling the kernel is necessary if you want to make changes to the kernel itself, e.g. remove patches that you don't need, include new patches, or test other changes. In general, we recommend you use one of the prebuilt packages if they are available for your distribution.
While preparing the sources and building the kernel itself works similarly on every distribution, the packaging steps are different. As always, you should look at the documentation/wiki of your distribution for more information. It is recommended that you obtain the kernel sources from https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git. Example kernel configurations for Debian and Arch Linux can be found at https://github.com/linux-surface/kernel-configs.
-
Install the required build-dependencies:
sudo apt install build-essential binutils-dev libncurses5-dev libssl-dev ccache bison flex libelf-dev
-
Clone the kernel repository:
git clone git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
and change into its directory via
cd linux
.If you already have the sources checked out from a previous build and want to re-compile the kernel for an updated version, you can run
git fetch
inside the repository to update it and then follow the steps below. -
Check out your desired version, e.g. via
git checkout v5.3.14
You usually want the latest version supported by the patches, so change this accordingly. It is recommended that you then create a new branch, e.g. via
git switch -c v5.3.14-surface
-
Assuming you have cloned this repo to
~/linux-surface
, apply the patches viafor i in ~/linux-surface/patches/[VERSION]/*.patch; do patch -p1 < $i; done
-
Configure your kernel.
You can get a base config from any up-to-date kernel package on the release page by extracting it (e.g
linux-image-<version>
for Debian/Ubuntu based distributions). For Debian based distributions, you can also find a config at/boot/config-<version>
. In both cases, you should choose a version that is close to the version you intend to build. To ensure that all Surface related options are set, you should merge this base config with one of the surface config fragments inlinux-surface/configs/
. For example, for kernel 5.10 run./scripts/kconfig/merge_config.sh <base-config> ~/linux-surface/configs/surface-5.10.config
from inside the kernel source directory, with<base-config>
being a path to the base configuration you chose.If you want more control, have a look at the specific configuration targets e.g.
make menuconfig
ormake olddefconfig
.By completing this step, you have set up the kernel sources and the configuration. The steps until here should be fairly similar on non-Debian based distributions.
-
Build the kernel
.deb
packages. For non-Debian-based distributions, please refer to the documentation of your distribution on how to build and package the kernel. For Debian-based distributions simply runmake -j `getconf _NPROCESSORS_ONLN` deb-pkg LOCALVERSION=-linux-surface
If you intend to apply changes and re-build the kernel later on (or in case of errors), you may instead want to run
make -j `getconf _NPROCESSORS_ONLN` bindeb-pkg LOCALVERSION=-linux-surface
This skips creation of the source package, which you most likely won't need. Feel free to adjust the
LOCALVERSION
to your liking. This is essentially the name/flavor of the kernel and should not collide with any other kernel that you intend to keep (it will replace the kernel with the sameLOCALVERSION
and version, e.g.5.10.5-linux-surface
will be the full suffix for a v5.10.5 source). -
Install the kernel packages, e.g. via
sudo dpkg -i linux-headers-[VERSION].deb linux-image-[VERSION].deb linux-libc-dev-[VERSION].deb
Have a look at the PKGBUILDs we use for building the binary package releases.