Skip to content

Instantly share code, notes, and snippets.

@alexandreteles
Created December 27, 2021 02:24
Show Gist options
  • Save alexandreteles/634006078874ee5e3225b3d9ff64d4df to your computer and use it in GitHub Desktop.
Save alexandreteles/634006078874ee5e3225b3d9ff64d4df to your computer and use it in GitHub Desktop.
Enable CUDA support for podman on WSL2 (Ubuntu)

Introduction

Running Docker on WSL2 without Docker Desktop can be a bit of a pain because of its daemonized nature, especially if you're running applications inside the container on your command line instead of just letting them run in the background.

As podman doesn't require a daemon, it makes running docker containers on WSL2 much easier and cleaner and you can just alias docker=podman as the APIs are 100% compatible. Setting up the nvidia support should be pretty straightforward as well, but as it took some time for me to figure it out (as the process isn't very well documented yet) I'm putting this Gist together for me and for everyone that might need it.

Steps

  1. Run the setup.sh script bellow. It'll install both podman and the required nvidia packages. This script is based on the information found at https://docs.nvidia.com/cuda/wsl-user-guide/index.html and https://podman.io/getting-started/installation;

  2. Copy and paste or download the oci-nvidia-hook.json bellow and save it to the /usr/share/containers/oci/hooks.d/. The source for this file can be found at https://github.com/NVIDIA/nvidia-container-toolkit/blob/master/oci-nvidia-hook.json

  3. You should be ready to go! To test if it's working just run: podman run --rm nvidia/cuda:11.0-base nvidia-smi and your output should look like this:

Mon Dec 27 02:18:10 2021
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 495.53       Driver Version: 497.29       CUDA Version: 11.5     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  NVIDIA GeForce ...  On   | 00000000:01:00.0  On |                  N/A |
|  0%   47C    P0    23W / 100W |   1781MiB /  4096MiB |     N/A      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                                  |
|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
|        ID   ID                                                   Usage      |
|=============================================================================|
|  No running processes found                                                 |
+-----------------------------------------------------------------------------+
{
"version": "1.0.0",
"hook": {
"path": "/usr/bin/nvidia-container-toolkit",
"args": ["nvidia-container-toolkit", "prestart"],
"env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
]
},
"when": {
"always": true,
"commands": [".*"]
},
"stages": ["prestart"]
}
#/bin/bash
. /etc/os-release
sudo sh -c "echo 'deb http://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/x${NAME}_${VERSION_ID}/ /' > /etc/apt/sources.list.d/devel:kubic:libcontainers:stable.list"
wget -nv https://download.opensuse.org/repositories/devel:kubic:libcontainers:stable/x${NAME}_${VERSION_ID}/Release.key -O Release.key
sudo apt-key add - < Release.key
sudo apt-get update
sudo apt-get -y install podman
sudo mkdir -p /etc/containers
podman info
echo -e "[registries.search]\nregistries = ['docker.io', 'quay.io']" | sudo tee /etc/containers/registries.conf
distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | sudo apt-key add -
curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | sudo tee /etc/apt/sources.list.d/nvidia-docker.list
sudo apt-get update
sudo apt-get install -y libnvidia-container-tools libnvidia-container1 nvidia-container-toolkit pigz
sudo mkdir -p /usr/share/containers/oci/hooks.d/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment