Magic bash script to install Nvidia and Cuda drivers (and Docker) on Ubuntu 22.04 and 24.04

On the one hand, installing drivers for Nvidia video cards does not cause as many problems as it did 10-15 years ago, on the other hand, while people can still manage drivers somehow, not everyone can get a working CUDA. In our HOSTKEY, we have created special scriptwhich the user can run on their own and get a working environment for running everything from PyTorch to Stable Diffusion and Ollama with GPU support.

What does this script do?

  1. It updates the system by detecting the type of video card by its DeviceID and installing the Linux kernel version 6, which is required for the A100/H100 series on Ubuntu 22.04;

  2. Installs Ubuntu recommended drivers for your video card;

  3. Connects Nvidia repositories and installs CUDA;

  4. It registers the necessary environment variables for its operation, simultaneously restoring the paths to /bin and /usr/bin, which you are guaranteed to break if you follow the official instructions;

  5. If you have Docker installed, adds GPU support to containers.

The script should work correctly on any video cards older than version 10xx and up to the latest models. We tested it on our GTX 1080, A4000, A5000, RTX 4090 and H100.

The script itself
#!/bin/bash

# Update and upgrade the system using apt
sudo apt update
sudo apt upgrade -y

#Check Ubuntu 22.04 and update kernel
lsb_release=$(lsb_release -a | grep "22.04")
if [[ -n "$lsb_release" ]]; then

    # Check if there's a video card with Nvidia (10de) H100 model (23xx)
    lspci_output=$(lspci -nnk | awk '/\[10de:23[0-9a-f]{2}\]/ {print $0}')
    if [[ -n "$lspci_output" ]]; then
        echo "A100 detected"
        # If yes install the necessary kernel package
        sudo apt install -y linux-generic-hwe-22.04
    fi

    # Check if there's a video card with Nvidia (10de) A100 model (20xx)
    lspci_output=$(lspci -nnk | awk '/\[10de:20[0-9a-f]{2}\]/ {print $0}')
    if [[ -n "$lspci_output" ]]; then
        echo "A100 detected"
        # If yes install the necessary kernel package
        sudo apt install -y linux-generic-hwe-22.04
    fi
fi

# Install Ubuntu drivers common package
sudo apt install ubuntu-drivers-common -y

recommended_driver=$(ubuntu-drivers devices | grep 'nvidia' | cut -d ',' -f 1 | grep 'recommended')
package_name=$(echo $recommended_driver | awk '{print $3}')
sudo apt install $package_name -y

# Install GCC compiler for CUDA install
sudo apt install gcc -y

# Get the release version of Ubuntu
RELEASE_VERSION=$(lsb_release -rs | sed 's/\([0-9]\+\)\.\([0-9]\+\)/\1\2/')

# Download and install CUDA package for Ubuntu
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu${RELEASE_VERSION}/x86_64/cuda-keyring_1.1-1_all.deb
sudo dpkg -i cuda-keyring_1.1-1_all.deb

# Update and upgrade the system again to ensure all packages are installed correctly
sudo apt update
sudo apt install cuda -y
sudo apt install nvidia-cuda-toolkit -y

# Add PATH and LD_LIBRARY_PATH environment variables for CUDA in .bashrc file
echo 'export PATH="/usr/bin:/bin:$PATH/usr/local/cuda/bin\${PATH:+:\${PATH}}"' >> ~/.bashrc
echo 'export LD_LIBRARY_PATH=/usr/local/cuda-12.2/lib64\${LD_LIBRARY_PATH:+:\${LD_LIBRARY_PATH}}' >> ~/.bashrc
source ~/.bashrc

#Installing Docker binding for Nvidia

if command -v docker &> /dev/null; then
  echo "Docker is installed."
  sudo apt install -y nvidia-docker2
  sudo systemctl restart docker
else
  echo "Docker is not installed."
fi

#Reboot the system for enable kernel modules
reboot

Save it in your home directory as nvidia_install.shdon't forget to ask via chmod +x nvidia_install.sh rights to run and run via sudo nvidia_install.sh. In 5-10 minutes (depending on your internet speed) you will receive a working system on which you can continue experimenting with the same neural networks.


Order virtual or highlighted server with pre-installed corporate messenger Rocket.Chat! You can choose a server in a data center in Russia or in data centers in Europe, the USA or Turkey. Payment in rubles by invoice from a Russian company.

Similar Posts

Leave a Reply

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