In this post will explore the install of docker and nvidia driver on ubuntu server with supported ubuntu versions 20, 22, 24.
Let's explore, how to install the docker with readymade commands, just need to run once.
Let's start with updating the Ubuntu repos:
sudo apt update
This will update the system repositories.
Next is to follow these commands:
sudo apt install -y docker.io

sudo usermod -aG docker $USER
sudo systemctl enable docker
sudo systemctl start docker

It will install the docker. Once completed let's move to next step. And also install the docker-composer if needed as:
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
Check docker-composer version: docker-compose version
After finishing the docker and composer install, let's move to the next step. For the NVIDIA driver install and the container support of GPU.
Step 1.
Get the recommended driver support for the ubuntu. To do so, we need to first install an ubuntu package to query the NVIDIA driver support.
sudo apt install ubuntu-drivers-common

After install of sudo apt install ubuntu-drivers-common, you can query for recommended drivers as,
ubuntu-drivers devices This will show the list of available drives with system recommendation.

Step 2
We identified the driver, which is suitable for the system, now we will make the install with the below command:
sudo apt install -y nvidia-driver-535, you can use your own identified drives nvidia-driver-xxx
After installing the NVIDIA driver, system need a reboot, so do it as sudo reboot
After reboot, check the installed driver by nvidia-smi
It will give output like:

Once you re-login to VM or system, it's time to install the container support for GPU. Let's see.
The above commands will help you installing the nvidia-container-toolkit.
The above commands has three commands,
1. Add repo
2. Do update
3. Install package.
Step 3
Restart the docker after install of nvidia-container-toolkit
sudo systemctl restart docker
Now try to run a simple GPU based docker image.
docker run --rm --gpus all nvidia/cuda:12.2.0-base-ubuntu20.04 nvidia-smi
It will give output like:

You are all set with your environment now.
Hope you find it helpful!
Checkout our DevOps services here