Install CUDA on WSL2 with Ubuntu
By: Yoshio Yamauchi, Sparkle
Tags: AI, Wsl, TensorFlow, Cuda, Machine Learning

Yoshio Yamauchi / SPARKLE / @sprkle_twtt

I post stories about machine learning, image recognition, and python.

1 Introduction — What is WSL?

WSL is short of “Windows Subsystem of Linux”. This is a sort of software you can install on your Windows 10 machine to use Linux over Windows. Before WSL, you had been able to use Linux operating system on a Windows machine through virtualization software like Virtual-Box. But these virtualizations are sometimes slow or you may not be able to use GPU to train AI models.

WSL or WSL2 solved these kind of problem. Because it’s not virtualization, you can access data stored on your Widows from Linux, and moreover, you can access data on USB drives. CUDA, which is a tool to train AI models, is also prepared for WSL2. In short, you don’t have to switch to Linux just to train AI models. Almost everything can be done on Windows with WSL2!

In this story I’m going to show you how to install WSL2 and CUDA.

2 Install WSL2 and Ubuntu on Windows 10

2.1 Requirements

Latest version of Windows 10 Home / Pro (inactivated one is also OK)

Basic Linux command line experience (such as navigating directories)

More than 20GB of storage space

2.2 Register the Windows Insider Program

You can register the program through the registration website. Just Follow the instructions on the website. Although the website tells that you need to activate Widows 10 Home or Pro, you actually don’t need to activate Windows. I tested with inactivated version of Windows 10 Home and it worked. After registering the program, you can install the insider program in the settings menu “Windows Update”. You can see that the program is already started to download and install. After the installation is complete, restart your computer. A Windows update will start. It will take a little bit long.

Make sure the following things such that you won’t encounter any issue:

Your Window 10 Home / Pro is updated to the latest version before registering the program. If not, you can update it in the settings menu “Windows Update”.

There’s no WSL installed before. If WSL1 or an older version of WSL2 exist on your computer, you may have to uninstall it. You can uninstall them as the you uninstall other programs.

There’s no Ubuntu installed on your computer before. If not, uninstall it.

Activate all feedbacks of your personal data. You may have asked that the first time when you install the Windows. If you rejected all of them when installation, you can turn them on in the settings menu “Diagnostics & feedback”.

Make sure your settings window of “Windows Insider Program” will look like this

2.3 Install WSL and update it to WSL2

Run the following commands as administrator to install WSL:

$ dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

The installed WSL is still the older version of WSL, so you now have to update it to WSL2:

$ dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

$ wsl — set-default-version 2

2.4 Install Ubuntu 18

The WSL2 itself does not have any operating system in it. It’s just an environment to run Linux operating systems. Thus the next step is downloading and installing a Linux OS on your Windows 10. You can do this as the same way you install software through the “Microsoft Store”. Just search “Ubuntu” and start installation of Ubuntu 18. The reason why I recommend Ubuntu 18 rather than the newer versions of Ubuntu is it’s not sure if CUDA would work on Ubuntu later than 19. The official document of CUDA for WSL2 is saying you should use 18 instead of 19 or 20.

Once you installed your Ubuntu 18, run the program. A command prompt will open with a message “Installing, this may take a few minutes…”. After that, you are required to set your username and password.

3 Install Nvidia Driver on Windows 10

You need a Nvidia driver to use your GPU. You can download and install an appropriate driver through this website. Just follow the installation process of the installer. A GPU managing software called “GeForce Experience” will be installed at the same time. One thing you need to note here is you must not install any GPU driver on the installed Ubuntu. This could make issues.

4 Install CUDA on Ubuntu

You can now install CUDA on Ubuntu. Run the command “wsl” to start Ubuntu on the windows command prompt. Then on the Ubuntu terminal run the following commands one by one with root.

$ sudo apt-key adv — fetch-keys http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub

$ sudo sh -c ‘echo “deb http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 /” > /etc/apt/sources.list.d/cuda.li st’

$ sudo apt-get update

$ sudo apt-get install -y cuda-toolkit-11–0

Once you installed CUDA I recommend you restart your computer. After reboot check if a folder named “/usr/lib/wsl”. If you can find the folder, the whole installation process is done successfully. You can check if CUDA and your GPU works on Ubuntu with a sample program. Navigate to /usr/local/cuda/samples/4_Finance/BlackScholes and then run the following commands:

$ sudo make

$ ./BlackSchooles

A message indicating CUDA is working should be shown

5 References

This document is available also in PDF:

https://drive.google.com/drive/folders/15LrqlxwGZJETPYZ1VFPpBI5dlWkbnPUu?usp=sharing