Machine Learning with WSL2, Cuda GPU Acceleration & Docker

Before October 2020 you either struggled through setting up tools on Windows natively or dual-booted to linux for doing machine learning tasks using the GPU’s.

In a recent Windows Insider update, along with new drivers from Nvidia, support for running GPU based tasks with Tensorflow pretty simple.

You can follow the documentation, or watch a video outlining it here. There were a few things that were out of date in that guide, so check out the notes below.

I’m here to cover pros, cons, pitfalls and just log the issues I faced and how I solved them in hopes to help others and prevent myself from wasting time in the future.

Once you have the base setup working I highly suggest exporting your WSL instance so you can reset to that point later.

What we’re gonna do:

  1. Follow the Nvidia docs to set up Cuda support inside WSL2. This includes updating Windows, drivers, installing Cuda toolkit and Docker.
  2. Install python & pip.
  3. Install anaconda3 or miniconda3.

Install python & pip

For Ubuntu 20.04 Python3 is the default. If you are using Ubunto 18.04 or older you can use these instructions to update to Python3.

In my case I only need to install pip using the following:

sudo apt update
sudo apt install python3-pip

Tip: copy the following to your ~/.bashrc file to use python and pip instead of python3 and pip3.

# Enter `which python3` & `which pip3` into the command line to get the paths.
alias python='/usr/bin/python3' 
alias pip='/usr/bin/pip3'

Setup Issues & Guide Corrections

The following issues were things I either ran into or had previously. I have provided solutions and corrections where available.

Commands differ for Ubuntu 20.04

Specifically when adding apt-keys under the Setting up CUDA Toolkit section, be sure to change the ubuntu1804 text to ubuntu2004 to match your version.

Cuda Toolkit is now v11.2

Install Toolkit using apt-get install -y cuda-toolkit-11-2 instead.

Docker Installer Confusion

When running the docker installer it detects the /mnt/c/ProgramFiles copy of docker. It seems like its not gonna work, but if you just wait for the 20-40s sleep timers it does finish properly.

Disable Docker for Windows

If you already use Docker for Windows, be sure to go into its settings โ†’ Resources โ†’ WSL Integration and uncheck your Machine Learning WSL instance. This is just a precaution so its not trying to constantly install itself.

Outdated Jupyter Example

The Nvidia setup docs show an example for Jupyter Notebooks. Unfortunately it took me hours to realize it was very out of date. Over a year old in fact. This became apparent when some of the tutorial notebooks wouldn’t complete the first few steps due to missing functions such as subsplit.

Instead use the following:

docker run -it --gpus all -p 8888:8888 tensorflow/tensorflow:latest-gpu-jupyter

Docker doesn’t autostart

After playing for a while you will quickly become annoyed having to run sudo service docker start ever time. Follow these 2 guides to fix this:

  1. Configure it to start automatically.
  2. How to automatically start the Docker daemon on WSL2

I am a founder & CEO of Code Atlantic and I've been working with WordPress for over 15 years creating plugins to help WP site owners grow for more than 10 of those. We have developed and maintained popular plugins including the best wordpress popup plugin, Popup Maker with over 4k 5 star reviews, as well as Content Control, Ahoy & User Menus.

Follow on:

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.