How to set up WSL2 + CUDA + Docker

tutorial
docker
cuda
Published

September 24, 2022

Below are the instructions on how to set up Windows Subsystem for Linux (WSL2), CUDA, and Docker, so that you can run GPU-enabled docker containers inside a linux environment on windows.

Pre-requisites

  1. You must be running Windows 10 version 2004 and higher (Build 19041 and higher) or Windows 11.
    • Check your windows version by pressing Windows Key + R and typing winver.
  2. You must have a NVIDIA GPU installed in your computer.
  3. You must have the latest drivers installed for your GPU (download them here.)

Installing WSL2

Run the following commands in windows command prompt in administrator mode.

Step 1 - Install WSL

wsl.exe --install

Step 2 - Restart PC

Restart your PC.

Step 3 - Ensure you have latest WSL kernel

wsl.exe --update

Step 4 - Restart WSL

wsl --shutdown

Step 5 - Set the default WSL version to 2

wsl.exe --set-default-version 2

Step 6 - Install Ubuntu

wsl --install -d Ubuntu

Step 7 - Set up Linux

You should see a new terminal prompting you to enter a UNIX username. Do this now. (This doesn’t have to match your windows username.) You will also be prompted to create a password. Note that the letters are invisible for security reasons.

Step 8 - Close Ubuntu terminal & make sure your Ubuntu installation is running version 2 of WSL.

Close the Ubuntu terminal, and in your windows command prompt in administrator mode, run wsl.exe --set-version Ubuntu 2

Step 9 - Restart WSL again

wsl --shutdown

You should now have WSL2 with Ubuntu properly installed. Type wsl in windows command prompt to enter into the linux environment. You can check the Ubuntu version with the command lsb_release -a inside your linux environment.

Fixing DNS issues

Sometimes there can be problems with DNS within WSL. If you have DNS issues, these steps should fix them. Run the following commands in the WSL environment (unless otherwise specified.)

Step 1 - Remove resolv.conf file

sudo rm -Rf /etc/resolv.conf

Step 2 - Check your DNS servers

In windows command prompt, run ipconfig /all and find the line ‘DNS Servers’ under the network adapter you are using. In my case, this was ‘Wireless Lan adapter Wi-Fi’. Take note of these DNS Servers.

Step 3 - Edit resolv.conf

Run sudo nano /etc/resolv.conf. Write the following into the file:

namespace xxx.xxx.xxx.xxx
namespace xxx.xxx.xxx.xxx

where xxx.xxx.xxx.xxx is replaced by the DNS servers from the previous step (you can list as many as you like here.) Press CTRL + o, then Enter to save the file name, then CTRL + x to edit the text editor.

Step 4 - Make resolv.conf immutable

sudo chattr -f +i /etc/resolv.conf

Note: If you want to undo this later, you can do sudo chattr -f -i /etc/resolv.conf

Step 5 - Edit wsl.conf

sudo bash -c 'echo "[network]" > /etc/wsl.conf' sudo bash -c 'echo "generateResolvConf = false" >> /etc/wsl.conf'

Step 6 - Restart WSL

wsl --shutdown

Step 7 - Relaunch WSL + check resolv.conf hasn’t changed

Relaunch WSL with wsl, and run cat /etc/resolv.conf. This should print the contents of resolv.conf to the terminal. This should match what you wrote to the file in step 3. If not, something has gone wrong.

Your DNS server should now be configured properly. Note: If you change connection you may need to re-do these steps.

Installing CUDA Toolkit

You should be able to run CUDA applications within WSL now. However, if you want to build CUDA applications, you will need the CUDA Toolkit.

Follow steps here inside the WSL environment.

Installing Docker

Install Docker Desktop by following the instructions here.

In the Docker menu, go to Settings > General and select ‘Use WSL 2 based engine.’ Click Apply & restart.

Test everything works

Make sure Docker Desktop is open, and within the WSL environment run sudo docker run --rm --gpus all nvidia/cuda:11.0.3-base-ubuntu20.04 nvidia-smi to test the latest official CUDA image.

References