How to Build a Real-Time Linux Kernel (PREEMPT_RT) Step by Step-Best Linux Device Drivers Training Online

« Previous Lecture | Next Lecture »

How to Build a Real-Time Linux Kernel (PREEMPT_RT) Step by Step

A hands-on lecture from our free Linux kernel development course — configure, compile, install, and verify a PREEMPT_RT real-time kernel on a modern Linux system, with no outdated steps.

Level
Intermediate
Series
Part 2 of 3
Cost
100% Free

In this practical lecture of our free Linux kernel development course, you will build a real-time Linux kernel with PREEMPT_RT from source. If you learned kernel building from older books, you were taught to download a separate real-time patch first. On kernel 6.12 and newer, that step is gone for x86, ARM64, and RISC-V — the real-time option lives directly in the standard configuration menu. This tutorial teaches the modern workflow first, and then shows the legacy patch method separately, because many production embedded devices still run older long-term kernels where the patch is required. Everything here uses only free, open-source tools, making this a perfect companion to any free embedded systems course.

What You Will Learn

  • How to prepare your build environment and fetch modern kernel sources
  • How to trim the configuration to your hardware with localmodconfig
  • How to enable CONFIG_PREEMPT_RT (“Fully Preemptible Kernel”) via menuconfig and via scripts
  • Which companion config options matter for a real-time Linux kernel build
  • How to compile, install, and boot your RT kernel
  • How to verify the running kernel is truly real-time
  • Legacy path: applying the RT patch queue to an older LTS kernel

Prerequisites

  • Part 1 of this series (real-time concepts and preemption models)
  • A Linux machine or VM with at least 20 GB free disk space
  • Comfort with the terminal and sudo
  • Our earlier free Linux kernel development course lectures on kernel configuration are helpful but not mandatory

Step 1: Prepare the Build Environment

Install the standard kernel build toolchain. On Debian/Ubuntu-family systems:

sudo apt update
sudo apt install build-essential flex bison libssl-dev libelf-dev \
     libncurses-dev bc dwarves rsync git fakeroot

On Fedora-family systems:

sudo dnf install gcc make flex bison openssl-devel elfutils-libelf-devel \
     ncurses-devel bc dwarves git

Step 2: Download a Modern Kernel Source

Choose a kernel that already contains mainline PREEMPT_RT support — that means 6.12 or newer. The 6.12 long-term series is an excellent choice for embedded products because it combines built-in real-time support with long-term maintenance. Download and extract:

cd ~/rt-build
wget https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.12.30.tar.xz
tar xf linux-6.12.30.tar.xz
cd linux-6.12.30

Replace the version number with the latest point release shown on kernel.org — the steps do not change.

Which Path Do You Need?
Your Situation External RT Patch Needed? What To Do
Kernel 6.12+ on x86 / x86_64 / ARM64 / RISC-V No Enable CONFIG_PREEMPT_RT directly (this tutorial, Steps 3–7)
Kernel 7.1+ on 32-bit ARM No Same as above — ARM support landed in mainline 7.1
Older LTS kernel (5.10, 5.15, 6.1, 6.6) required by your BSP Yes Apply matching RT patch (see Legacy Path section below)
Need bleeding-edge RT optimizations not yet merged Optional Apply the current development patch queue on top of mainline

Step 3: Trim the Configuration to Your Machine

Building every driver in the kernel tree wastes hours. The localmodconfig target reads the modules currently loaded on your system and disables everything you are not using:

lsmod > /tmp/current-modules
make LSMOD=/tmp/current-modules localmodconfig

Tip: Before running lsmod, plug in every USB device and enable every interface (Wi-Fi, Bluetooth, camera) you plan to use, so their modules are loaded and therefore kept in the config.

Step 4: Enable the Fully Preemptible Kernel (CONFIG_PREEMPT_RT)

Now the key moment of this real-time Linux kernel build. Open the configuration UI:

make menuconfig

Navigate as follows:

  1. Enter General setup.
  2. Scroll to Preemption Model. On a fresh config it typically shows a desktop-oriented default.
  3. Press Enter, and choose Fully Preemptible Kernel (Real-Time) — the option corresponding to CONFIG_PREEMPT_RT.
  4. Save and exit.
Menuconfig Navigation Path
General setup ▶ Preemption Model ▶ Fully Preemptible Kernel (Real-Time)

Cannot see the Real-Time option? On some kernel versions the RT choice is hidden behind expert mode. Enable Configure standard kernel features (expert users) under General setup (CONFIG_EXPERT) and the option appears. Also confirm your architecture is one of the supported ones from the table above.

Alternative: Enable RT Without the Menu

For scripted or CI builds, skip the UI entirely:

scripts/config --enable EXPERT
scripts/config --enable PREEMPT_RT
scripts/config --set-val HZ 1000
scripts/config --enable HZ_1000
make olddefconfig

Verify Before Building

grep -E "PREEMPT_RT|PREEMPT=" .config

You want to see CONFIG_PREEMPT_RT=y. If it shows # CONFIG_PREEMPT_RT is not set, revisit the menu.

Step 5: Recommended Companion Options

Useful Config Options for RT Builds
Option Purpose Recommendation
CONFIG_HZ_1000 1 ms timer tick for finer scheduling granularity Enable
CONFIG_NO_HZ_FULL Stops the periodic tick on isolated busy CPUs, cutting jitter Enable for CPU-isolation setups
CONFIG_RCU_BOOST Prevents preempted RCU readers from stalling the system Enable
CONFIG_DEBUG_PREEMPT, lock debugging Catches locking bugs during development Enable in dev, disable in production (adds latency)

Step 6: Compile and Install

Use all your CPU cores and give the kernel a recognizable local version tag:

make -j$(nproc) LOCALVERSION=-eprt

On Debian/Ubuntu systems, building installable packages is cleaner:

make -j$(nproc) LOCALVERSION=-eprt bindeb-pkg
sudo dpkg -i ../linux-image-6.12.30-eprt_*.deb ../linux-headers-6.12.30-eprt_*.deb
sudo update-grub

Or install the traditional way:

sudo make modules_install
sudo make install
sudo update-grub

Reboot and pick your new kernel from the boot menu:

sudo reboot

Step 7: Verify the Running Real-Time Kernel

Three quick checks confirm success. First, the kernel banner must contain PREEMPT_RT:

$ uname -a
Linux epbox 6.12.30-eprt #1 SMP PREEMPT_RT Wed Jul  8 10:15:02 IST 2026 x86_64 GNU/Linux

Second, the config of the running kernel:

$ grep PREEMPT_RT /boot/config-$(uname -r)
CONFIG_PREEMPT_RT=y

Third, the kernel exposes a dedicated flag on RT systems:

$ cat /sys/kernel/realtime
1

If all three checks pass, congratulations — you have built and booted a real-time Linux kernel entirely with free tools, as part of this free Linux kernel development course.

Legacy Path: Older LTS Kernels That Still Need the RT Patch

Many shipping embedded products are locked to vendor BSP kernels such as 5.10, 5.15, 6.1, or 6.6. These predate the mainline merge, so the real-time option only appears after applying the matching patch queue from kernel.org’s projects/rt area. The workflow:

  1. Download the kernel source tarball and the RT patch whose version exactly matches it (for example, a 6.6.x kernel needs the 6.6.x-rtNN patch).
  2. Extract both, keeping the patch one directory above the source tree.
  3. Always dry-run first, then apply:
cd linux-6.6.90
patch -p1 --dry-run < ../patch-6.6.90-rt55.patch
echo $?        # must print 0 before you proceed
patch -p1 < ../patch-6.6.90-rt55.patch

A compressed patch can also be streamed directly without extracting it first:

xzcat ../patch-6.6.90-rt55.patch.xz | patch -p1

After patching, the “Fully Preemptible Kernel (Real-Time)” choice appears in the Preemption Model menu, and every remaining step (configure, build, install, verify) is identical to the modern path above. Never feed the still-compressed .xz file to patch as if it were plain text — it is a classic beginner mistake.

Shortcut Options: Prebuilt RT Kernels

Building from source is the best way to learn, but for quick experiments you can install a ready-made real-time kernel:

  • Debian: prebuilt RT kernel packages exist for x86 and ARM64 in the standard repositories.
  • Ubuntu Pro: a supported real-time kernel can be enabled with the pro enable realtime-kernel subscription feature (free for personal use on a few machines).
  • Yocto: use the linux-yocto-rt kernel recipe and the core-image-rt image for embedded targets — ideal for production board bring-up.

Common Mistakes and Troubleshooting

  • RT option missing in menuconfig: Kernel too old (needs 6.12+ mainline), unsupported architecture, or CONFIG_EXPERT not enabled.
  • Patch fails with rejects (legacy path): Patch version does not exactly match the kernel version. Even a point-release mismatch causes failures — always dry-run.
  • Build fails on certificates: Distro configs reference vendor signing keys. Clear them: scripts/config --disable SYSTEM_TRUSTED_KEYS --disable SYSTEM_REVOCATION_KEYS
  • Booted kernel shows PREEMPT but not PREEMPT_RT: You selected “Preemptible Kernel (Low-Latency Desktop)” instead of “Fully Preemptible Kernel (Real-Time)”. They are different options.
  • Debug options left on in production: Lock debugging and preempt debugging inflate latencies dramatically. Disable them for latency measurements and deployment.

Key Takeaways

  • On kernel 6.12+, you build a real-time Linux kernel by simply selecting CONFIG_PREEMPT_RT — no external patch on x86, ARM64, or RISC-V.
  • localmodconfig keeps build times sane by trimming unused drivers.
  • Verify with uname -a (look for PREEMPT_RT), the boot config, and /sys/kernel/realtime.
  • Older LTS kernels still require the exactly-matching RT patch, applied with a dry run first.
  • Disable debug options before measuring latency or shipping.

Frequently Asked Questions

Which kernel version should I pick for a new RT project?

Kernel 6.12 LTS or newer. It is the first long-term series with built-in PREEMPT_RT, giving you mainline real-time support plus years of maintenance updates.

Do I need special hardware to build a real-time Linux kernel?

No. Any x86_64 PC, ARM64 board (like popular SBCs), or RISC-V platform works. More CPU cores just mean faster compilation.

How long does the build take?

With localmodconfig on a modern 8-core machine, typically 10–25 minutes. A full distro-style config can take hours — another reason to trim first.

Can I run the RT kernel in a virtual machine?

Yes, and it is fine for learning this free Linux kernel development course material. But latency numbers inside a VM are not meaningful — the host steals time unpredictably. Measure on bare metal.

Does the RT kernel change how I write device drivers?

Mostly no — correct drivers work unchanged. But driver authors should know that spinlocks may sleep under RT and interrupt handlers run as threads, which our free linux device drivers course lectures cover in depth.

My embedded vendor kernel is 5.15. Am I stuck?

No — use the legacy path: apply the matching 5.15-rt patch from the kernel.org RT project, then follow the identical configure-build-verify steps.

Your Real-Time Kernel Is Running — Now Prove It

In Part 3 of this free embedded systems course, you will measure worst-case latency with cyclictest and the modern rtla tooling, and tune your system like a professional.

Next Lecture: Measure & Tune Latency » Course Index

« Previous Lecture | Next Lecture »

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *