How to Install the NVIDIA CUDA Toolkit 11.2 and cuDNN 8.1 for TensorFlow in WSL2 — Ubuntu 20.04 LTS with Anaconda Support

Mahmoud Bahaa
3 min readFeb 26, 2022
Dual RTX 2080 GPUs
Photo by Nana Dua on Unsplash

So, in this post I am going to quickly explain how to access GPU acceleration through Windows Subsystem for Linux. I personally needed that for Tensorflow to use as a backend for Open3D: A Modern Library for 3D Data Processing.

The problem arises when you have a Windows machine, but need to use something that was built only for Linux. Your options are very limited, either setup a dual boot or try with WSL2. As of November 2021, with the release of Windows 10 (non-insider) 2021H2, the support for CUDA was added and you can now access your GPUs on your host machine in WSL2. This works as well with Windows 11.

Installation of the needed software however is somehow tricky as you need to do some steps on both the host machine and on the WSL2 distro you are using. The standard cudatoolkit and cudnn from conda channels do not work.

1- Install CUDA drivers for your GPU only on the Windows machine:

  • Run the downloaded driver and continue with the installation of it.

2- Install WSL2:

  • Open an elevated PowerShell (As an Administrator)
  • Install WSL2 Ubuntu-20.04 with:
wsl --install -d Ubuntu-20.04
  • After installation is finished, log into the WSL2 kernel with:
wsl -d Ubuntu-20.04

3- Install Anaconda

  • Download Anaconda (for Linux) in WSL2 machine from: Anaconda official website
  • Install Anaconda (You can check this guide if you don’t know how).
  • Once you installed Anaconda and you are in the base environment, you can proceed with installing CUDA Toolkit and CuDNN.

4- Install CUDA Toolkit and CuDNN:

  • Since recent Tensorflow versions require CUDA Toolkit 11.2 and CuDNN 8.1, we will install those versions. Simply run this command in the Ubuntu machine.
sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/7fa2af80.pub && sudo sh -c 'echo "deb http://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64 /" > /etc/apt/sources.list.d/cuda.list' && sudo apt-get update && sudo apt-get --yes install cuda-toolkit-11-2 && sudo sh -c 'echo "deb http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu2004/x86_64 /" > /etc/apt/sources.list.d/nvidia-machine-learning.list' && sudo apt-get update && sudo apt-get install --yes --no-install-recommends cuda-11-2 libcudnn8=8.1.0.77-1+cuda11.2 libcudnn8-dev=8.1.0.77-1+cuda11.2

For better readability, the previous command can be run into separate commands (Don’t run this if you already ran the previous block):

sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/7fa2af80.pubsudo sh -c 'echo "deb http://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64 /" > /etc/apt/sources.list.d/cuda.list'sudo apt-get updatesudo apt-get --yes install cuda-toolkit-11-2sudo sh -c 'echo "deb http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu2004/x86_64 /" > /etc/apt/sources.list.d/nvidia-machine-learning.list'sudo apt-get updatesudo apt-get install --yes --no-install-recommends cuda-11-2 libcudnn8=8.1.0.77-1+cuda11.2 libcudnn8-dev=8.1.0.77-1+cuda11.2
  • Once finished, confirm that everything is working by running the BlackScholes sample:
cd /usr/local/cuda-11.2/samples/4_Finance/BlackScholessudo make./BlackScholes
  • If everything is alright, you should see something like this

5- Create a new Anaconda environment and start developing

conda create -n tf2
conda activate tf2
conda install tensorflow
python -c "import tensorflow"
  • If you see nothing strange about missing CUDA libraries then you are good to go!
  • You can further confirm by running the below python script. It should return the GPUs found by Tensorflow on WSL2.
import tensorflow as tf
print(tf.config.list_physical_devices('GPU'))

And that’s it for this guide. Happy Coding!

--

--