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
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.
- Ubuntu 22.04 LTS Desktop - for instructions on how to install see: https://ubuntu.com/tutorials/install-ubuntu-desktop#1-overview
- Enough free disk space, around 40GB depending on kernel configuration
- Internet connection
- Some basic knowledge about building a Linux kernel: https://www.linux.com/topic/desktop/how-compile-linux-kernel-0
We start with the default Linux kernel included in Ubuntu 22.04 which is version 5.15.0-43-generic.
- Create a working directory.
- Download the kernel source files and real-time patch files for your specific Linux kernel version from kernel.org.
- Unpack the source files
tar -xzf linux-5.15.96.tar.gz
xz -d patch-5.15.96-rt61.patch.xz
patch -p1 <../patch-5.15.96-rt61.patch
- Configure the kernel build options.
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
- Activate “Fully Preemptible Kernel (Real-Time)” option from “General setup” / “Preemption Model” then SAVE and EXIT.
- 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.
- Install the kernel modules.
sudo make modules_install
- Install the kernel.
sudo make install
- 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.
- Check to confirm the new real-time kernel is in use. You should see "-rt" in the kernel name.
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.