Install RT Linux patch for Ubuntu

Here’s for Ubuntu 19.10 and above and I patched Linux 5.4.5 rt kernel patch because Linux 5.3 -- the base of Ubuntu 19.10 -- has no rt kernel patch.

0. Make a working directory

# Make dir and move to working directory
$ mkdir ~/kernel && cd ~/kernel

1. Download kernel and patch

Download kernel and rt patch from https://www.kernel.org/. You can get these below:

(kernel) https://www.kernel.org/pub/linux/kernel/
(rt patch) https://www.kernel.org/pub/linux/kernel/projects/rt/

Note that the version of rt patch and kernel should be same. I used linux-5.4.5.tar.gz and patch-5.4.5-rt3.patch.gz.

If you download these on the ~/kernel, skip below and move on to step 2.

# Move these zip file to ~/kernel
$ cd ~/Download
$ mv linux-5.4.5.tar.gz patch-5.4.5-rt3.patch.gz ~/kernel/.

2. Extract kernel sources and patch rt kernel

# Extract kernel sources
$ cd ~/kernel
$ tar xvzf linux-5.4.5.tar.gz

# Patch rt kernel
$ cd linux-5.4.5
$ gzip -cd ../patch-5.4.5-rt3.patch.gz | patch -p1 --verbose {}

3. Install required packages

For using menuconfig GUI, libncurses-dev is required. flex and bison will be needed when you compile the kernel.

# For using gui
$ sudo apt install libncurses-dev libssl-dev

# For compiling kernel
$ sudo apt install flex bison

4. Configure kernel for RT

$ make menuconfig

and enter the menuconfig GUI.

# Make preemptible kernel setup
General setup ---> [Enter]
Preemption Model (Voluntary Kernel Preemption (Desktop)) [Enter]
Fully Preemptible Kernel (RT) [Enter] #Select

# Select <SAVE> and <EXIT>
# Check .config file is made properly

Note that there’s no Check for stack overflows option on GUI configuration anymore. You can check it by searching “overflow”. Type / and overflow on Graphical Menu.

5. Compile the kernel

$ make -j20
$ sudo make modules_install -j20
$ sudo make install -j20

6. Make kernel images lighter

As @mrRo8o7 said earlier, big initrd image can occur kernel panic. So you can resolve this problem by:

# Strip unneeded symbols of object files
$ cd /lib/modules/5.4.5-rt3  # or your new kernel
$ sudo find . -name *.ko -exec strip --strip-unneeded {} +

# Change the compression format
$ sudo vi /etc/initramfs-tools/initramfs.conf
# Modify COMPRESS=lz4 to COMPRESS=xz (line 53)

COMPRESS=xz 

[:wq]

then update initramfs

$ sudo update-initramfs -u

7. Verify and update grub

Verify that directory and update the grub.

# Make sure that initrd.img-5.4.5-rt3, vmlinuz-5.4.5-rt3, and config-5.4.5-rt3 are generated in /boot
$ cd /boot
$ ls

# Update grub
$ sudo update-grub

8. Reboot and verify

$ sudo reboot

# After the reboot
$ uname -a

then you can check your new kernel version

Linux [PROMPT] 5.4.5-rt3 …

Step 0 - Make a working directory

Make a working directory

#Move to working directory
mkdir ~/kernel && cd ~/kernel

Step 1 - Download kernel and patch

Go to https://www.kernel.org/pub/linux/kernel/ and download a desired version of kernel to ~/kernel. Similarly, go to https://www.kernel.org/pub/linux/kernel/projects/rt/ and download the RT patch with same version as the downloaded kernel version. The kernel and patch I used were linux-4.9.115.tar.gz and patch-4.9.155-rt93.patch.gz.

Step 2 - Unzip the kernel

tar -xzvf linux-4.9.115.tar.gz

Step 3 - Patch the kernel

#Move to kernel source directory
cd linux-4.9.115
gzip -cd ../patch-4.9.115-rt93.patch.gz | patch -p1 --verbose

Step 4 - Enable realtime processing This step requires libncurses-dev

sudo apt-get install libncurses-dev libssl-dev

The next command launches a graphical menu in the terminal to generate the config file.

make menuconfig

Go to the location and make the changes accordingly

##Graphical Menu##

Processor type and features ---> [Enter]
Preemption Model (Voluntary Kernel Preemption (Desktop)) [Enter]
Fully Preemptible Kernel (RT) [Enter] #Select

[Esc][Esc]

Kernel hacking --> [Enter]
Memory Debugging [Enter]
Check for stack overflows #Already deselected - do not select


[Esc][Esc]


[Right Arrow][Right Arrow]

<Save> [Enter]

.config

<Okay> [Enter]

<Exit> [Enter]


[Esc][Esc]


[Right Arrow]
<Exit> [Enter]

Step 5 - Compile the kernel

make -j20
sudo make modules_install -j20
sudo make install -j20

Step 6 - Verify and update Verify that initrd.img-4.9.115-rt93, vmlinuz-4.9.115-rt93, and config-4.9.115-rt93 are generated in /boot directory and update the grub.

cd /boot
ls
sudo update-grub

Verify that there is a menuentry containing the text "menuentry 'Ubuntu, with Linux 4.9.115-rt93'" in /boot/grub/grub.cfg file

To change default kernel in grub, edit the GRUB_DEFAULT value in /etc/default/grub to your desired kernel.

NOTE: 0 is the 1st menuentry

7 - Reboot and verify

sudo reboot

Once the system reboots, open the terminal and use uname -a to check the kernel version, it should look like the following

Linux abhay-home 4.9.115-rt93 #1 SMP PREEMPT RT Mon May 13 03:32:57 EDT 2019 x86_64 x86_64 x86_64 GNU/Linux

Note: "SMP PREEMPT RT" validates that your system is running real time kernel.


After installing the new kernel (like @Abhay Nayak posted), I got into a kernel panic. The problem was that the initrd image was too big. I solved that with:

Step 1 - Strip the kernel modules

cd /lib/modules/<new_kernel>
find . -name *.ko -exec strip --strip-unneeded {} +

Step 2 - Change the initramfs compression

Edit file /etc/initramfs-tools/initramfs.conf:

COMPRESS=xz

Step 3 - Update initramfs

sudo update-initramfs -u
sudo update-grub2