How to change Linux-kernel-version

Are you tired of reinstalling the nvidia driver as a deep learning engineer? It may be related to the Linux kernel update…

Zukhriddin
4 min readFeb 23, 2023

Working on deep learning projects in Ubuntu, I come across some challenges, particularly when it comes to issues with Nvidia drivers, CUDA, and cuDNN. To keep my system up-to-date and install necessary packages, I frequently use the sudo apt update and sudo apt upgrade commands. However, sudo apt upgrade can update the Linux kernel, which may cause problems with the Nvidia driver upon reboot. If you encounter this issue, you may need to reinstall the Nvidia driver or downgrade your Linux kernel version. Personally, I would prefer to downgrade the kernel version, and here we’ll explore how to manage Linux kernel versions and see some time-saving tips. Let’s get started!

Part 1. Install or downgrade the Linux kernel version:

Check the currently installed kernel version on the terminal by running the command:

uname -r

Check the available kernel versions to install:

apt-cache search linux-image- | grep generic

This command will search all the packages available in the package manager’s cache that match the string linux-image and pipe the output to grep, which filters out any packages that contain the string generic in their name.

You can also use the following command to see the available versions in the repository:

apt-cache policy linux-image-generic

To see a list of all the available kernel versions on your system, you can run the command:

dpkg --list | grep linux-image

To list the kernel versions that are installed and available to boot, you can use the command:

dpkg --list | grep linux-image | awk '{ print $2 }'

Once you have determined the version of the kernel that you want to downgrade to or install, you can use the following command to install it:

sudo apt-get install linux-image-<version>-generic

Where “version” is the version number of the kernel that you want to install, and “generic” is the type of kernel.

After installing the kernel, you must set the desired kernel version on the grub menu that your Linux distro uses to boot which is explained in part 2.

Part 2. Change the kernel version that your Linux system is booting to:

We will use the grub bootloader to do this:

Open a terminal window and run the following command. This will open the GRUB configuration file in the nano text editor:

sudo nano /etc/default/grub

In the GRUB configuration file make the following changes:

GRUB_TIMEOUT_STYLE=menu  # it was "hidden" by default
GRUB_TIMEOUT=10 # it was "0" by default

The above changes make grub menu to be shown automatically(you don't need to press ESC or Shift keys) while booting and it waits 10 seconds.

Then, update the GRUB bootloader configuration:

sudo update-grub

When you run the command sudo update-grub, it scans the system for installed operating systems and kernels, generates a new GRUB configuration file, and updates the menu entries in the GRUB bootloader. This ensures that the boot menu shows the correct options and that the system can boot to the correct operating system or kernel version.

Then reboot the system:

While rebooting, grub menu appears on the screen automatically.
Press Enter on **Advanced options for Ubuntu section, and then choose desired kernel version you want to boot. After that, your system will be loaded with the chosen kernel, and if you run uname -r it shows the selected kernel version that was chosen while rebooting.
Great, That's done!!!...

Extra:

You can make the specific kernel version default while booting. If the kernel version you want to make default is already running on your system (as shown by the output of uname -r) and you would like to prevent it from being upgraded when using sudo apt update && sudo apt upgrade, you can use the apt-mark command to mark the package as hold:

sudo apt-mark hold linux-image-<version>-generic

You can make sure it was held by running this:

apt-mark showhold

This will prevent the package from being automatically upgraded or removed. To make it default during boot, you can use sudo update-grub command to update the grub bootloader configuration file and set the desired kernel as the default.

sudo update-grub

To unhold(release):

sudo apt-mark unhold linux-image-<version>-generic

If the kernel version you want to make default is not running on your system (as shown by the output of uname -r) then read this gist from the start...

Removing kernels:

You can remove older kernels that are not in use by running the following command:

sudo apt-get purge <kernel-version>

This will free up disk space and make sure that the current kernel version is the only one available to boot.

📌 Gist version of the blog post: here…

--

--