Setup Windows 10/11 machines for Deep Learning with Docker and GPU using WSL
Do you have a windows laptop/desktop with a decent Nvidia GPU and interested in developing Deep Learning applications like me but don’t know how to enable GPUs for training? Don’t worry, I’ve got you covered. This article guides you step-by-step to set up development environment locally from scratch.
This guide from Nvidia outlines the process but if you are feeling lazy, here is the easier way out. You must be running Windows 10 version 2004 and higher (Build 19041 and higher) or Windows 11.
Step 1: Prerequisites
Search for Turn Windows features on or off at search and enable “Virtual Machine Platform”, “Windows Subsystem for Linux” features and click OK. Do a Restart if prompted.
Step 2: Install WSL 2 and Ubuntu
Open PowerShell as Administrator and run the below commands to install WSL and Ubuntu distribution.
wsl.exe --install -d Ubuntu
wsl.exe --update
wsl.exe --set-default-version 2
Follow this guide if you face any errors while setting WSL 2.
Step 3: Download latest Nvidia Driver
Download the latest drivers for your Graphics card from Nvidia’s official site. You can find your GPU from Task manager under Performance. Choose appropriate Product Series, Product, OS and download and install the driver.
Step 4: Install CUDA Toolkit
Next step is installing CUDA Toolkit on top of WSL environment. Open Ubuntu by searching at Windows. It will open a terminal window. Create a user and set up password. Remove the outdated signing key by running below command.
sudo apt-key del 7fa2af80
The simplest way to install CUDA toolkit is from bash. At the time of writing this article, the CUDA toolkit version 12.3 is latest and the commands are given below. In order to get the latest version, follow the commands from https://developer.nvidia.com/cuda-downloads.
Operating System: Linux
Architecture: x86_64
Distribution: WSL-Ubuntu
Version: 2.0
Installer Type: deb (local)
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.3.2/local_installers/cuda-repo-wsl-ubuntu-12-3-local_12.3.2-1_amd64.deb
sudo dpkg -i cuda-repo-wsl-ubuntu-12-3-local_12.3.2-1_amd64.deb
sudo cp /var/cuda-repo-wsl-ubuntu-12-3-local/cuda-*-keyring.gpg /usr/share/keyrings/
sudo apt-get update
sudo apt-get -y install cuda-toolkit-12-3
Step 5: Setup Docker and Nvidia Container Toolkit
Download and Install Docker Desktop. Click Settings and enable Ubuntu under Resources > WSL Integration tab. Click Apply and restart at the bottom right corner.
Switch back to Ubuntu terminal and run below commands to install container runtime.
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 apt-get update
sudo apt-get install -y nvidia-container-toolkit
sudo nvidia-ctk runtime configure --runtime=docker
Now, restart Docker Desktop.
Step 6: Verify your setup
Verify your setup by running below command to check whether GPU is being utilized or not. Open command prompt and run below command.
docker run --gpus all -it --rm nvidia/cuda:12.3.1-base-ubuntu20.04 nvidia-smi
That’s it! You are good to go. You can load custom Docker images and develop your deep learning applications/projects on your own GPU. Remember to use gpus all flag to enable GPU within docker container.
Bonus (Optional)
If you want to create custom images for Tensorflow and PyTorch with Jupyter IDE, feel free to clone below Dockerfile scripts and build images.
References:
- https://docs.nvidia.com/cuda/wsl-user-guide/index.html
- https://developer.nvidia.com/cuda-downloads
- https://learn.microsoft.com/en-us/windows/wsl/install
You can contact me at my LinkedIn in case you face any issues along the journey. That’s it for today. Happy Coding! 💻🤓