WSL2 + GPU + Docker

Dec 07, 2020

Full details for this post at docs.nvidia.com/cuda/wsl-user-guide, this post expands links' instructions and distills just the essential commands without their about text.

Switch to Windows Dev Channel

[Details]

  1. Register for Windows Insider Program
  2. Settings > Update & Security > Windows Insider Program > switch from Beta Channel (Recommended) to Dev Channel
  3. Settings > Update & Security > Advanced Options > turn on Receive updates for other Microsoft products when you update Windows
  4. Settings > Update & Security > Check for updates, download, restart.

Install Nvidia WSL2-compatibile driver

[Details]

  1. Nvidia link > Get CUDA Driver > download, install. Do this before below!

Install WSL2

[Details]

  1. Run Powershell as Administrator
  2. dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
  3. dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
  4. Restart computer
  5. (back in Powershell) wsl --set-default-version 2

Install Ubuntu

  1. Install Ubuntu 18.04 from MS Store. I recommend 18.04 over 20.04, easier Nvidia setup without modification.
  2. Install this thing. You'd get an Ubuntu error with that link as a fix anyway.

Install Docker + Nvidia stuff

[Details]

  1. Setup Docker, Nvidia
    curl https://get.docker.com | sh
    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
    curl -s -L https://nvidia.github.io/libnvidia-container/experimental/$distribution/libnvidia-container-experimental.list | sudo tee /etc/apt/sources.list.d/libnvidia-container-experimental.list
    sudo apt-get update
    sudo apt-get install -y nvidia-docker2
    sudo usermod -aG docker $USER
    
  2. Edit /etc/docker/daemon.json, add "default-runtime"
    { "runtimes": { "nvidia": { "path": "nvidia-container-runtime", "runtimeArgs": [] } }, "default-runtime": "nvidia" }
    
  3. Restart Docker
    sudo service docker stop
    sudo service docker start
    

Test

[Details]

  1. docker run --gpus all nvcr.io/nvidia/k8s/cuda-sample:nbody nbody -gpu -benchmark

If it fails, eg Error: only 0 Devices available, 1 requested. Exiting.:

  1. Restart PC, try again
  2. If you're on a laptop, open Device Manager > Display Adapters
    1. Right-click Nvidia GPU > Disable > Yes
    2. Right-click > Enable
    3. This is an Nvidia bug. "NVIDIA is aware of a specific installation issue reported on mobile platforms with the WIP driver 465.12 posted on 11/16/2020. A known workaround will be to disable and reenable the GPU adapter from device manager at system start. We are working on a fix for this issue and will have an updated driver soon."
  3. Try adding "node-generic-resources": ["NVIDIA-GPU=0"] to /etc/docker/daemon.json | Details

Further tweaks

[Details]. Click for additional setup tweaks like making all RAM available, docker-compose with GPU, etc.

Try a walking desk

Long coding sessions lead to physical fatigue and mental fog. A walking desk keeps you alert and focused, preventing costly bugs and burnout.Stay focused and healthy during long coding sessions.Get the factsGet the facts

Comments temporarily disabled because Disqus started showing ads (and rough ones). I'll have to migrate the commenting system.