Follow @Fahim_FBA on Twitter to learn more about it.
This is a complete guideline that I use to set up my computer for CUDA specific workload on my Windows machine via WSL2.
Make sure to ⭐ this repository if you liked it!
This repo is hosted at fahimfba.github.io/CUDA-WSL2-Ubuntu
↪️ If you are also interested about my current specification that I use for this task.
- Processor: Ryzen 5 3500X 6 Core and 6 Threads
- RAM: 32GB DDR4 3200MHz (16 GB + 8 GB + 8 GB)
- GPU: Zotac RTX 3050 8GB GDDR6
- Motherboard: Gigabyte B450M Aorus Elite
- Monitor: MSI Optix G24 Gaming Curved 75Hz
Fun fact! I hate RGB!
For this guideline, I will be using the Windows 11 Pro (Version 22H2) and WSL2 (of course!).
This whole processes can take a lot of time. Therefore, make sure to ensure proper connectivity of the internet and the electricity. For me, it took almost 7 hours in total. You would also need to download some pretty huge packages along the way. Also, make sure that you have installed the latest Nvidia driver after downloading the official driver from the official website of Nvidia. Make sure that you have installed all the updates of your Windows 11.
I will be using the latest PowerShell. You can download that from the Microsoft store, but I will download from the official website as the store might create some problems later. Go to the official website. This normally redirects you to the latest version of PowerShell available on that moment. For me, the latest version is 7.3 (24 May 2023). For you, it might be the updated version. Don't worry about that. Simply download the latest stable version.
- Click on Download PowerShell button.
- Find the latest PowerShell of
win-x64.msi
. Download that.
- The installation process is pretty straightforward. However, I will be guiding you throughout the entire process. Double click on the downloaded file. Then click
Next
.
- Keep everything as it is and click
Next
.
- I still prefer to keep everything as it is and simply click
Next
.
- I still prefer to keep everything as it is and simply click
Next
.
- Click
Install
.
- Now click
Finish
.
I really like the Windows Terminal as I can simply switch to any other WSL OSes (Ubuntu, Kali, Git Bash, etc.) whenever I want. But before proceeding further, I have to make sure that my current Windows Terminal is the updated one.
- Open the Microsoft Store and search for
Windows Terminal
.
- Click on
Update
if it needs an update. - Make sure that you are on the latest updated Windows Terminal already.
- Now open the Windows Terminal, and we have to change some settings first. Click on
Open Settings
.
- If you do not see the
Open Settings
prompt, then simply click the drop-down arrow, and click onSettings
.
- From
Startup
, make sure that the Default profile is set onPowerShell
(The new PowerShell we installed in Step 2). TheDefault terminal application
should be set onWindows Terminal
.
- Then click
Save
and exit the terminal.
For using the WSL, we have to make sure that our CPU virtualization is enabled. You can check the status through your task manager. If it is disabled, then make sure to enable the virtualization through BIOS. You will find a lot of YouTube videos, but make sure that you are following the correct one that matches your motherboard brand and model.
Now we need to install the WSL2 and Ubuntu LTS.
- Open the Windows Terminal as an Administrator.
- For installing the WSL, use the command
wsl --install
.
- Then, it would automatically install the latest LTS version of Ubuntu.
- After the tasks get finished, it would prompt you for rebooting your PC. Save other works and simply restart your computer.
- After restarting the PC, it would automatically open the terminal and ask you for the username and password for your Linux OS.
- Give the Username and Password. Make sure to use the same Password on Retype Password!
- After a while, it will install the necessary components.
- Now, make sure that WSL2 becomes the default WSL in this terminal. Apply the command,
wsl --set-default-version 2
.
- If you want to check the WSL OSes status (How many OSes are available, how many of them are running or stopped), use the command,
wsl --list --verbose
.
Here, it is telling me that I have Ubuntu installed on my WSL version 2 and it is currently stopped. - However, after working on a WSL OS, if you want to shut down the OS, then you can use the command
wsl -t distro_name
. For me, it is Ubuntu, therefore, I usedwsl -t Ubuntu
.t
represents the termination command here. - If you have multiple OSes in WSL, and if you want to run any specific distribution, then use the command
wsl --distribution distribution_name
. For example, if I want to run Ubuntu specifically, the command would bewsl --distribution Ubuntu
. If you only have one distribution, then you do not necessarily need to worry about this at all.
- You can simply use
exit
to exit a distro from the terminal. It might not necessarily shut down the distribution. You can specifically use the termination command for that. However, some regularly used commands are here.
- After installing a distribution, you would also be able to see and go there by using the drop-down menu from the Windows Terminal.
We need to update and install some apps now.
- Open Ubuntu by using any method inside the Windows Terminal. You can obviously use the dedicated Ubuntu App. But I always prefer the Terminal as I can use multiple different distributions and command line applications here altogether.
- Update the system by using the command
sudo apt update
.
- If you get errors in updating/upgrading saying it can't reach to the server then change the nameserver with the command
echo "nameserver 8.8.8.8" | sudo tee /etc/resolv.conf > /dev/null
. - After updating, upgrade the system using
sudo apt upgrade
.
You can clear the terminal by using theclear
command. - CUDA works with C. Thus, we need to install the gcc compiler first. Use the command
sudo apt install gcc --fix-missing
.
Now it is time to install CUDA from the official website of Nvidia.
Make sure to select the following:
Operating System: Linux
Architecture: x86_64
Distribution: WSL-Ubuntu
Version: 2.0
Installer Type: deb(local)
This will provide the necessary commands.
Now your task would be to apply each command serially in the WSL Ubuntu terminal. Make sure to use the first command twice. It normally resolves the problem of keyring later.
Also, keep in mind that these commands might get changed later. Therefore, always follow the official website. For this guideline, I will be using the exact command I used to set up the CUDA in my machine.
wget https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/cuda-wsl-ubuntu.pin
I used the same command again after finishing the previous transactions.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.1.1/local_installers/cuda-repo-wsl-ubuntu-12-1-local_12.1.1-1_amd64.deb
This normally takes a lot of time as it downloads a large file (above 2GB file size).
sudo dpkg -i cuda-repo-wsl-ubuntu-12-1-local_12.1.1-1_amd64.deb
sudo cp /var/cuda-repo-wsl-ubuntu-12-1-local/cuda-*-keyring.gpg /usr/share/keyrings/
- Then update the system using
sudo apt-get update
.
sudo apt-get -y install cuda
.
The official CUDA installation guide from Nvidia tells us to add export PATH=/usr/local/cuda-12.1/bin${PATH:+:${PATH}}
to the PATH variable.
I have changed the CUDA version cuda-12.1
according to my installed CUDA version. Make sure to do the same for your updated CUDA version.
Do the following to do that:
- Open Ubuntu in the Windows Terminal.
- Go to root directory using
cd ~
. Then open thebashrc
in nano usingnano .bashrc
- Go to the end of the file and copy-paste the path there. For me, the path is
export PATH=/usr/local/cuda-12.1/bin${PATH:+:${PATH}}
.
Then useCtrl
+X
to close. Make sure to useY
to save in the same file. - To apply the changes, use
source ~/.bashrc
. You can check the path usingecho $PATH
.
Install the Nvidia Cuda Toolkit using sudo apt install nvidia-cuda-toolkit
.
You can check the Driver and CUDA versions using nvidia-smi
.
Also make sure to check whether the Nvidia Cuda Compiler Driver has been installed or not by using nvcc -V
.
- Ensure that you have python3 installed in your system. You can check the version using
python3 --version
. If that says that "python3 is not found" or something like that, then install python. - Install PIP using
sudo apt-get install python3-pip
.
For installing the PyTorch, go to the official website of PyTorch. Then make sure to select the relevant sections. After that, it will provide you with a command. You have to use the command in your Ubuntu terminal.
For me, the selections were:
- PyTorch Build: Stable (2.0.1) - Make sure to select the latest stable version always
- Your OS: Linux
- Package: Pip
- Language: Python
- Computer Platform: CUDA 11.8 - Make sure to select the latest available CUDA version
After that, I got the command pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
.
I simply used that exact command in my Ubuntu terminal.
It also downloads a large file that can take a lot of time if you have a slower internet connection like me!
You can directly check whether your CUDA has been installed or not by running two lines of Python code in the terminal.
- Run Python in terminal using
python3
. - Import torch using
import torch
. - Check the CUDA availability using
torch.cuda.is_available()
.
If it returns True
, then you have successfully finished installing CUDA on your system!
You have to enable the Nvidia Developer Settings for using CUDA via WSL. Simply follow the procedures:
- Open the Nvidia Control Panel.
- Click "Agree and Continue".
. - In the Nvidia Control Panel, Click Desktop > Enable Developer Settings. If the "Enable Developer Settings" does not have any check mark, then click on that to enable that feature.
- The Developer section will be visible. Click on Manage GPU Performance Counters.
- Check the radio button on "Allow access to the GPU performance counters to all users".
- Click "Apply" and "Yes" to approve the changes permanently.
- At the end, it should look like this.
- You can again check the CUDA availability like earlier.
I normally prefer the Jupyter Notebook. It is true that you can install that in various ways, like pip install notebook
.
But I prefer the command pip install jupyter notebook
.
To open a Jypyter Notebook, you can simply use jupyter notebook
in the terminal.
The notebook will open instantly and you can use the given URL to open that in your web browser like me!
I ran two codes to check the performance of my CUDA.
- Open a Python3 script in notebook.
- I used the following code to check whether it is using my CPU or CUDA from GPU.
import torch
if torch.cuda.is_available():
device = torch.device("cuda")
else:
device = torch.device("cpu")
print("using", device, "device")
- For the performance comparison between my CPU and GPU (CUDA), I used the following code.
import time
matrix_size = 32*512
x = torch.randn(matrix_size, matrix_size)
y = torch.randn(matrix_size, matrix_size)
print("************* CPU SPEED *******************")
start = time.time()
result = torch.matmul(x, y)
print(time.time() - start)
print("verify device:", result.device)
x_gpu = x.to(device)
y_gpu = y.to(device)
torch.cuda.synchronize()
for i in range(3):
print("************* GPU SPEED *******************")
start = time.time()
result_gpu = torch.matmul(x_gpu, y_gpu)
torch.cuda.synchronize()
print(time.time() - start)
print("verify device:", result_gpu.device)
I also made side by side comparisons between the Google Colab and my computer. You can check them as well!
Try | Google Colab | My Computer |
---|---|---|
1 | Google Colab | My PC |
2 | Google Colab | My PC |
The result already states that my PC is working better than the Google Colab!
Note If you are facing an error in step 13, then check the issue 2 created by Willfried.
If you think that you do not need the CUDA deb file anymore, then you can remove that using the following command.
rm filename
For me it was,
rm cuda-repo-wsl-ubuntu-12-1-local_12.1.1-1_amd64.deb
It does not remove the CUDA from your system. It just removes the deb file from your system.
I hope that you have successfully installed CUDA on your Windows 11 system using WSL2. If you have any questions, feel free to reach out to me, or create issues. I will try my best to answer them.