How to install Docker Compose, CUDA Toolkit, NVIDIA-Docker, NVIDIA Container Toolkit on WSL2 Ubuntu
Install Windows Terminal on Microsoft Store
Multiple tabs are supported in Windows Terminal, so it is recommend to install it from the Microsoft Store.
Change Settings -> Startup -> Default terminal application to “Windows Terminal” instead of “Windows Console Host”
Install WSL2
Skip to next if your environment is not on WSL2.
Open a PowerShell as a administrator
PS C:\tmp> wsl --install
Installing: Virtual Machine Platform
Virtual Machine Platform has been installed.
Installing: Windows Subsystem for Linux
Windows Subsystem for Linux has been installed.
Installing: Windows Subsystem for Linux
Windows Subsystem for Linux has been installed.
Installing: Ubuntu
Ubuntu has been installed.
The requested operation is successful. Changes will not be effective until the system is rebooted.
Reboot your Windows PC, and open an Ubuntu terminal, then installation will start.
Installing, this may take a few minutes...
Please create a default UNIX user account. The username does not need to match your Windows username.
For more information visit: https://aka.ms/wslusers
Enter new UNIX username:
Enter a username and password.
Install Docker
Open a WSL2 prompt and execute,
sudo apt update && sudo apt upgrade -y && sudo apt autoremove
sudo apt install -y curl
curl -s https://raw.githubusercontent.com/karaage0703/ubuntu-setup/master/install-docker.sh | /bin/bash
Log out and login (or restart PC)
To confirm
docker run hello-world
...
docker: Got permission denied while trying to connect to the Docker daemon socket…
If got permission denied, give permission to the user.
sudo gpasswd -a $(whoami) docker
sudo chgrp docker /var/run/docker.sock
sudo service docker restart
Logout from ubuntu, restart wsl (wsl –shutdown), and loging again.
docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
c1ec31eb5944: Pull complete
Digest: sha256:53cc4d415d839c98be39335170ad2ca8eb36951288f81b75
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
Docker Compose
Configure Docker Compose
Setup Docker’s apt repository.
# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
# Add the repository to Apt sources:
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
Ref: https://docs.docker.com/engine/install/ubuntu/#install-using-the-repository
Install docker compose
sudo apt install docker-compose-plugin
Confirm
test@ubuntu:~$ docker compose version
Docker Compose version v2.29.2
Install CUDA Toolkit
Open the page, and get the latest download instructions.
Current instructions is here.
sudo apt-key del 7fa2af80
wget https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/cuda-wsl-ubuntu.pin
sudo mv cuda-wsl-ubuntu.pin /etc/apt/preferences.d/cuda-repository-pin-600
wget https://developer.download.nvidia.com/compute/cuda/12.6.1/local_installers/cuda-repo-wsl-ubuntu-12-6-local_12.6.1-1_amd64.deb
sudo dpkg -i cuda-repo-wsl-ubuntu-12-6-local_12.6.1-1_amd64.deb
sudo cp /var/cuda-repo-wsl-ubuntu-12-6-local/cuda-*-keyring.gpg /usr/share/keyrings/
sudo apt-get update
sudo apt-get -y install cuda-toolkit-12-6
NVIDIA-Docker
curl -s https://raw.githubusercontent.com/karaage0703/ubuntu-setup/master/install-nvidia-docker.sh | /bin/bash
Run Jupyter Notebook to confirm GPU detection.
nvidia-docker run -it --rm --name tensorflow-gpu -p 8888:8888 tensorflow/tensorflow:latest-gpu-py3-jupyter
You will see like this,
________ _______________
___ __/__________________________________ ____/__ /________ __
__ / _ _ \_ __ \_ ___/ __ \_ ___/_ /_ __ /_ __ \_ | /| / /
_ / / __/ / / /(__ )/ /_/ / / _ __/ _ / / /_/ /_ |/ |/ /
/_/ \___//_/ /_//____/ \____//_/ /_/ /_/ \____/____/|__/
WARNING: You are running this container as root, which can cause new files in
mounted volumes to be created as the root user on your host machine.
To avoid this, run the container by specifying your user's userid:
$ docker run -u $(id -u):$(id -g) args...
[I 09:01:27.622 NotebookApp] Writing notebook server cookie secret to /root/.local/share/jupyter/runtime/notebook_cookie_secret
jupyter_http_over_ws extension initialized. Listening on /http_over_websocket
[I 09:01:27.730 NotebookApp] Serving notebooks from local directory: /tf
[I 09:01:27.730 NotebookApp] The Jupyter Notebook is running at:
[I 09:01:27.730 NotebookApp] http://5fd7ff9ed8ac:8888/?token=e908442a4362ccb88ecd3a09c3dff4918a806548639cb0d3
[I 09:01:27.730 NotebookApp] or http://127.0.0.1:8888/?token=e908442a4362ccb88ecd3a09c3dff4918a806548639cb0d3
[I 09:01:27.731 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 09:01:27.733 NotebookApp]
To access the notebook, open this file in a browser:
file:///root/.local/share/jupyter/runtime/nbserver-1-open.html
Or copy and paste one of these URLs:
http://5fd7ff9ed8ac:8888/?token=e908442a4362ccb88ecd3a09c3dff4918a806548639cb0d3
or http://127.0.0.1:8888/?token=e908442a4362ccb88ecd3a09c3dff4918a806548639cb0d3
[W 09:01:30.828 NotebookApp] Clearing invalid/expired login cookie username-127-0-0-1-8888
Open a browser with the last link http://127.0.0.1:8888/?token=xxxxxxxx Run this command to confirm your GPU. Click “New”, select “Python 3” and type these lines and run.

from tensorflow.python.client import device_lib
device_lib.list_local_devices()

Ctrl+c to stop.
Shutdown this notebook server (y/[n])? y
[C 09:08:26.484 NotebookApp] Shutdown confirmed
[I 09:08:26.484 NotebookApp] Shutting down 1 kernel
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
File "/usr/lib/python3.6/logging/__init__.py", line 1945, in shutdown
h.flush()
File "/usr/local/lib/python3.6/dist-packages/absl/logging/__init__.py", line 892, in flush
self._current_handler.flush()
File "/usr/local/lib/python3.6/dist-packages/absl/logging/__init__.py", line 785, in flush
self.stream.flush()
File "/usr/local/lib/python3.6/dist-packages/ipykernel/iostream.py", line 341, in flush
if self.pub_thread.thread.is_alive():
AttributeError: 'NoneType' object has no attribute 'thread'
[I 09:08:26.886 NotebookApp] Kernel shutdown: e9a9f3e0-d0fd-4571-861a-471aacf28dea
NVIDIA Container Toolkit
curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg \
&& curl -s -L https://nvidia.github.io/libnvidia-container/stable/deb/nvidia-container-toolkit.list | \
sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' | \
sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list
sudo sed -i -e '/experimental/ s/^#//g' /etc/apt/sources.list.d/nvidia-container-toolkit.list
sudo apt update
sudo apt install -y nvidia-container-toolkit
runtime configuration, and restart docker
sudo nvidia-ctk runtime configure --runtime=docker
sudo systemctl restart docker