Building a Real-time Linux Kernel in Ubuntu with PREEMPT_RT

In order to achieve real-time computing performance in Linux, effectively turning Linux into an RTOS, most distributions can be patched with the real-time kernel patch known as PREEMPT_RT.

Mar 15, 2023 8:00am

Introduction

We previously showed how easy it is to enable a real-time Linux kernel in Ubuntu 22.04 now that Canonical is including this kernel as an option, but it is also not very difficult to patch the default Linux kernel in order to build your own real-time Linux kernel to use with your distribution. The basic steps will be to download the kernel sources and PREEMPT_RT patch, patch the kernel, build the kernel, restart the system and select the new real-time kernel. For more information about the Real Time Linux project, including the latest stable version of the RT_PREEMPT patch, please check the project page from the Linux Foundation.

Prerequisites

Instructions

We start with the default Linux kernel included in Ubuntu 22.04 which is version 5.15.0-43-generic.

  1. Create a working directory.

mkdir ~/kernel
cd ~/kernel

  1. Download the kernel source files and real-time patch files for your specific Linux kernel version from kernel.org.

wget https://mirrors.edge.kernel.org/pub/linux/kernel/v5.x/linux-5.15.96.tar.gz
wget https://mirrors.edge.kernel.org/pub/linux/kernel/projects/rt/5.15/patch-5.15.96-rt61.patch.xz

  1. Unpack the source files.

tar -xzf linux-5.15.96.tar.gz
xz -d patch-5.15.96-rt61.patch.xz
cd linux-5.15.96
patch -p1 <../patch-5.15.96-rt61.patch

  1. Configure the kernel build options and install package dependencies.

cp /boot/config-5.15.0-43-generic .config
sudo apt update
sudo apt install make gcc libncurses-dev libssl-dev flex libelf-dev bison
make menuconfig

    1. Activate “Fully Preemptible Kernel (Real-Time)” option from “General setup” / “Preemption Model” then SAVE and EXIT.

  1. Build the kernel (note: this can take some time). For the build process there are likely several dependencies that must be installed (some covered in the previous step), so when encountering build errors please check for other packages that might be necessary specific to your system.

sudo make

  1. Install the kernel modules.

sudo make modules_install

  1. Install the kernel.

sudo make install

  1. Reboot and select the newly built PREEMPT_RT kernel instead of the default. You may need to edit your Grub configuration in order to access the boot loader screen to select the new real-time kernel. Please refer to https://help.ubuntu.com/community/Grub2 for additional information and help with the Grub 2 boot loader.

  1. Check to confirm the new real-time kernel is in use. You should see "-rt" in the kernel name.

Conclusions

As you can see after following the steps above it is not difficult to patch, build, and install a real-time Linux kernel into Ubuntu. The process is the same for other Linux distributions. For better real-time performance we highly recommend to setup the firmware and BIOS according to our recommendations in our Developer Center: https://developer.acontis.com/rt-faq/items/FAQ1013.html and https://developer.acontis.com/pcob.html.

Now you are on your way to developing real-time applications in Linux.