Installing Ubuntu with WSL2

This tutorial was made in April 2023, and can be subjective to changes. More info can be found on the Microsoft WSL2 documentation: https://learn.microsoft.com/en-us/windows/wsl/

1. Enable WSL2 in Windows features

Select Start, type features and you should see Turn Windows features on or off.

Activate Virtual Machine Platform and Windows Subsystem for Linux. Reboot! image

These 2 Administrator commands replace the clicking above:

PS C:\users\demo> dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
PS C:\users\demo> dism.exe /online /enable-feature /featurename:WindowsSubsystemforLinux /all /restart

2. Install Ubuntu

Select Start, type PowerShell, right-click Windows PowerShell, and then select Run as administrator.

Enter the WSL update command:

PS C:\users\demo> wsl --install

The system will then ask you to type in a username and password for the Ubuntu system and enter the Ubuntu bash shell.

If you open Windows Terminal and click the dropdown menu, you will find the Ubuntu shell (Run as administrator is not necessary). image

3. Install Cuda

If you have an NVIDIA card installed, you can install Cuda as well. It works like a normal Linux installation.

demo@DESKTOP-J47JBBS:~$ sudo apt update
demo@DESKTOP-J47JBBS:~$ sudo apt install nvidia-driver-530

If you enter the nvidia-smi command you will see something like this:

image

You can install your Cuda applications e.g. pytorch explained on this topic: https://telin.ugent.be/telin-docs/windows/pytorch/#3-install-pytorch-with-cuda.

4. Memory limit

Windows sets a memory limit to the virtual PC of 12 the installed Windows memory. You can overwrite the default setting. Suppose your installed windows memory is 24 GB ram, the Ubuntu available ram, will then be 12GB. To allocate 16GB, issue the following (replace the user demo with your username):

demo@DESKTOP-J47JBBS:~$ echo -e "[wsl2]\nmemory=16GB" >/mnt/c/Users/demo/.wslconfig

The next time you start an Ubuntu shell it will have 16GB. image

You may consider it could take up all of your windows memory if you allocated too much.

5. Accessing files between OS’s

In Windows you can access the Ubuntu system through the Windows Explorer. Just type \\wsl$ in the location bar.

In Ubuntu you can access the C-drive through /mnt/c.