Build your own real-time kernel

If your distribution isn't any help, you can compile and install a real-time kernel manually. The examples assume building a RT kernel based on the 4.8 kernel version but they should generally apply for other kernel versions too provided that there is a RT patchset available for the used kernel.

Install the necessary packages, on Debian and Ubuntu this is:

sudo apt-get install kernel-package fakeroot build-essential flex bison

Download the kernel sources and the RT patchset (this example uses the 4.8 branch, you're free to use another kernel branch for which there is a RT patch set available):

mkdir -p ~/tmp/linux-rt
cd ~/tmp/linux-rt
wget -c
wget -c

Extract the kernel sources and patch them with the RT patchset:

tar xJvf linux-4.8.15.tar.xz
cd linux-4.8.15
xzcat ../patch-4.8.15-rt10.patch.xz | patch -p1

Now you need to configure the kernel. The easiest way is to use an existing kernel config, those configs can be found in the /boot/ directory of your system. You can copy an appropriate kernel config to your current working directory and use that config as a starting point:

cp /boot/config-`uname -r` .config

This will copy the config of the kernel you're currently using. You might want to consider using a config of a kernel already optimized for audio work, like the Ubuntu -lowlatency kernel config. The next step is to create a new config with full preemption enabled from the copied config:

make oldconfig

You will get a lot of prompts but you can leave everything at its default value except for the prompt which Preemption Model you'd like to use. Select option 5 there (Fully Preemptible Kernel):

Preemption Model
> 1. No Forced Preemption (Server) (PREEMPT_NONE)
  2. Voluntary Kernel Preemption (Desktop) (PREEMPT_VOLUNTARY)
  3. Preemptible Kernel (Low-Latency Desktop) (PREEMPT__LL) (NEW)
  4. Preemptible Kernel (Basic RT) (PREEMPT_RTB) (NEW)
  5. Fully Preemptible Kernel (RT) (PREEMPT_RT_FULL) (NEW)
choice[1-5]: 5 <Enter>

You can use one of these commands to edit the configuration:

make config

if you want a text interface, or

make menuconfig

for a nice ncurses text interface (you need to install libncurses5-dev if you're on Debian or Ubuntu), or

make nconfig

for another ncurses interface with better keybindings, or

make xconfig

for a qt GUI interface (you need to install libqt4-dev if you're on Debian or Ubuntu).

Make sure to check the “Timer Frequency: 1000 Hz” options under the “Processor type and features” group.

When your configuration is done, you can build and install the kernel and its modules (distro-agnostic way):

make install
make modules_install

On Debian or Ubuntu the building step can be done like this:

make -j `nproc` LOCALVERSION= deb-pkg

Or if you prefer make-kpkg:

CONCURRENCY_LEVEL=`nproc` LOCALVERSION= fakeroot make-kpkg --initrd kernel_image kernel_headers

You can also use `getconf _NPROCESSORS_ONLN` instead of `nproc`.

When building is complete and the packages have been created you can install them (this will also update the boot loader menu):

cd ..
sudo dpkg -i linux-{headers,image}-4.8.15-rt10_*.deb

If you built and installed the kernel the distro-agnostic way you will have to update the bootloader yourself:

sudo update-grub

Optional bits

You may want to disable the debug features; this will save compile space. You can do it in the config interface, or from the shell with this command:

scripts/config --disable DEBUG_INFO

You may want to set the “Local version” (in the “General setup” group) to your name or -custom-rt or whatever: it will be appended to the kernel name so you'll easily recognize it as a custom build, and you won't risk overwriting some working kernel with the same name. You can achieve the same result by editing the file localversion-rt and append a local version name to the version number of the RT patch set.

You can do without an initrd if you build into the kernel (not as a module, i.e. checked in the config as “*” and not “m”) everything you need to boot (e.g. ext4, chipset drivers, keyboard drivers, etc.). If you need it, build it with:

mkinitrd -k kernel-<kernel version> -i initrd-<kernel version>

or follow your distro guide.

If you need to know which drivers you need for your hardware, use the form at (it's distro-agnostic, even if it says “Debian” everywhere).

Build your own real-time kernel on Debian Wheezy or later

Since Debian Wheezy, the linux-source-3.2 package already installs the RT patch (you don't need to download it from upstream), but you'll have to apply it manually.

(Instructions taken from

Install the kernel source package:

sudo aptitude install linux-source

This installs a linux tarball and a bzipped RT patch in /usr/src/ . Copy them to wherever you want to build (you'll need some GB of free space), untar/bunzip them and cd into the linux source directory.

Apply the RT patch:

patch -p1 -i ../linux-patch-3.x-rt.patch

Make sure to install the needed tools for the build:

sudo apt-get build-dep linux

Now you must configure the kernel. (This step is distro-agnostic, you can follow the istructions in the previous paragraph).

Then you can compile.

make clean
make deb-pkg

… and install the new kernel

sudo dpkg -i ../linux-image-3.x.yy_foo.deb
wiki/build_your_own_real-time_kernel.txt · Last modified: 2022/05/04 10:26 by autostatic