https://github.com/fahimfba/cuda-wsl2-ubuntu
Install CUDA on Windows11 using WSL2
https://github.com/fahimfba/cuda-wsl2-ubuntu
cuda cuda-programming cuda-support cuda-toolkit cuda-wsl deep-learning deep-reinforcement-learning deeplearning deeplearning-ai machine-learning machinelearning machinelearning-python wsl wsl-environment wsl-ubuntu wsl2
Last synced: about 2 months ago
JSON representation
Install CUDA on Windows11 using WSL2
- Host: GitHub
- URL: https://github.com/fahimfba/cuda-wsl2-ubuntu
- Owner: FahimFBA
- License: mit
- Created: 2023-05-23T17:46:53.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2023-08-02T13:56:38.000Z (almost 2 years ago)
- Last Synced: 2025-03-27T14:54:58.725Z (2 months ago)
- Topics: cuda, cuda-programming, cuda-support, cuda-toolkit, cuda-wsl, deep-learning, deep-reinforcement-learning, deeplearning, deeplearning-ai, machine-learning, machinelearning, machinelearning-python, wsl, wsl-environment, wsl-ubuntu, wsl2
- Language: Jupyter Notebook
- Homepage: https://fahimfba.github.io/CUDA-WSL2-Ubuntu/
- Size: 10.4 MB
- Stars: 63
- Watchers: 2
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
- Security: SECURITY.md
Awesome Lists containing this project
README
Setup Windows Machine for ML/DL Using Nvidia Graphics Card (CUDA)
> **Follow [@Fahim_FBA](https://twitter.com/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 :star: this repository if you liked it!
This repo is hosted at fahimfba.github.io/CUDA-WSL2-Ubuntu
:arrow_right_hook: 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 75HzFun fact! I hate RGB!
For this guideline, I will be using the Windows 11 Pro (Version 22H2) and WSL2 (of course!).
## Step 1: Internet & Electricity
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](https://www.nvidia.com/download/index.aspx). Make sure that you have installed all the updates of your Windows 11.
## Step 2: Latest PowerShell
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](https://learn.microsoft.com/en-us/powershell/scripting/install/installing-powershell-on-windows). 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.1. Click on **Download PowerShell** button.

2. Find the latest PowerShell of `win-x64.msi`. Download that.

3. 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`.

4. Keep everything as it is and click `Next`.

5. I still prefer to keep everything as it is and simply click `Next`.

6. I still prefer to keep everything as it is and simply click `Next`.

7. Click `Install`.

8. Now click `Finish`.
## Step 3: Windows Terminal
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.
1. Open the **Microsoft Store** and search for `Windows Terminal`.

2. Click on `Update` if it needs an update.
3. Make sure that you are on the latest updated Windows Terminal already.
4. Now open the Windows Terminal, and we have to change some settings first. Click on `Open Settings`.

5. If you do not see the `Open Settings` prompt, then simply click the drop-down arrow, and click on `Settings`.

6. From `Startup`, make sure that the **Default profile** is set on `PowerShell` (The new PowerShell we installed in [Step 2](#step-2-latest-powershell)). The `Default terminal application` should be set on `Windows Terminal`.

7. Then click `Save` and exit the terminal.
## Step 4: Hardware Virtualization
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.
## Step 5: WSL & Ubuntu LTS
Now we need to install the WSL2 and Ubuntu LTS.
1. Open the Windows Terminal as an **Administrator**.

2. For installing the **WSL**, use the command `wsl --install`.


3. Then, it would automatically install the latest LTS version of Ubuntu.

4. After the tasks get finished, it would prompt you for rebooting your PC. Save other works and simply restart your computer.

5. After restarting the PC, it would automatically open the terminal and ask you for the username and password for your Linux OS.

6. Give the Username and Password. Make sure to use the same Password on Retype Password!

7. After a while, it will install the necessary components.

8. Now, make sure that **WSL2** becomes the default WSL in this terminal. Apply the command, `wsl --set-default-version 2`.

9. 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.
10. 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 used `wsl -t Ubuntu`. `t` represents the **termination** command here. 
11. 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 be `wsl --distribution Ubuntu`. If you only have one distribution, then you do not necessarily need to worry about this at all.

12. 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.

13. After installing a distribution, you would also be able to see and go there by using the drop-down menu from the Windows Terminal.
## Step 6: Configure Ubuntu LTS
We need to update and install some apps now.
1. 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.

2. Update the system by using the command `sudo apt update`.

3. 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`.
4. After updating, upgrade the system using `sudo apt upgrade`.


You can clear the terminal by using the `clear` command.
5. CUDA works with C. Thus, we need to install the gcc compiler first. Use the command `sudo apt install gcc --fix-missing`.


## Step 7: CUDA
Now it is time to install CUDA from the [official website of Nvidia](https://developer.nvidia.com/cuda-downloads).
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.
1. `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.
2. `wget https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/cuda-wsl-ubuntu.pin`

3. `sudo mv cuda-wsl-ubuntu.pin /etc/apt/preferences.d/cuda-repository-pin-600`

4. `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).

5. `sudo dpkg -i cuda-repo-wsl-ubuntu-12-1-local_12.1.1-1_amd64.deb`

6. `sudo cp /var/cuda-repo-wsl-ubuntu-12-1-local/cuda-*-keyring.gpg /usr/share/keyrings/`

7. Then update the system using `sudo apt-get update`.


8. `sudo apt-get -y install cuda`.

## Step 8: Post installation
The [official CUDA installation guide from Nvidia](https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html) 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:
1. Open Ubuntu in the Windows Terminal.
2. Go to root directory using `cd ~`. Then open the `bashrc` in nano using `nano .bashrc`

3. 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 use `Ctrl` + `X` to close. Make sure to use `Y` to save in the same file.
4. To apply the changes, use `source ~/.bashrc`. You can check the path using `echo $PATH`.
## Step 9: Nvidia CUDA Toolkit
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`.
## Step 9: PIP
* 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`.
## Step 10: PyTorch
For installing the PyTorch, go to the [official website of PyTorch](https://pytorch.org/get-started/locally/). 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 versionAfter 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!
## Step 11: CUDA availability
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!
## Step 12: Nvidia Developer Settings
You have to enable the Nvidia Developer Settings for using CUDA via WSL. Simply follow the procedures:
1. Open the **Nvidia Control Panel**.

2. Click "Agree and Continue".
.
3. 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.


4. The **Developer** section will be visible. Click on **Manage GPU Performance Counters**.

5. Check the radio button on "Allow access to the GPU performance counters to all users".

6. Click "Apply" and "Yes" to approve the changes permanently.

7. At the end, it should look like this.

8. You can again check the CUDA availability like earlier.

## Step 12: Jupyter NotebookI 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!
## Step 13: Test
I ran two codes to check the performance of my CUDA.
1. Open a Python3 script in notebook.

2. I used the following code to check whether it is using my CPU or CUDA from GPU.```python
import torchif torch.cuda.is_available():
device = torch.device("cuda")
else:
device = torch.device("cpu")
print("using", device, "device")
```
3. For the performance comparison between my CPU and GPU (CUDA), I used the following code.
```python
import timematrix_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](https://colab.research.google.com/) and my computer. You can check them as well!| Try | Google Colab | My Computer |
| --- | ------------------------------------------------------------------------------------------ | ----------------------------------------------------------------------------------------- |
| 1 | [Google Colab](https://github.com/FahimFBA/CUDA-WSL2-Ubuntu/blob/main/GoogleCollab1.ipynb) | [My PC](https://github.com/FahimFBA/CUDA-WSL2-Ubuntu/blob/main/CUDA%20_TEST_Fahim1.ipynb) |
| 2 | [Google Colab](https://github.com/FahimFBA/CUDA-WSL2-Ubuntu/blob/main/GoogleCollab2.ipynb) | [My PC](https://github.com/FahimFBA/CUDA-WSL2-Ubuntu/blob/main/CUDA%20_TEST_Fahim2.ipynb) |The result already states that my PC is working better than the Google Colab!
> **Note**
If you are facing an error in [step 13](#step-13-test), then check the [issue 2 created by Willfried](https://github.com/FahimFBA/CUDA-WSL2-Ubuntu/issues/2).## Step 14: Remove CUDA Deb file
If you think that you do not need the CUDA deb file anymore, then you can remove that using the following command.
```bash
rm filename
```For me it was,
```bash
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.
## Conclusion
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.