Skip to content

Latest commit

 

History

History
290 lines (192 loc) · 17.6 KB

README.md

File metadata and controls

290 lines (192 loc) · 17.6 KB

JavaScript With Fahim Banner

Setup Windows Machine for ML/DL Using Nvidia Graphics Card (CUDA)


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!



↪️ 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!).

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. Make sure that you have installed all the updates of your Windows 11.
Windows 11 Updates

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. 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.
    Download Powershell button
  2. Find the latest PowerShell of win-x64.msi. Download that.
    latest powershell msi file
  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.
    msi software
  4. Keep everything as it is and click Next.
    Next step
  5. I still prefer to keep everything as it is and simply click Next.
    Next step
  6. I still prefer to keep everything as it is and simply click Next.
    Next step
  7. Click Install.
    Install
  8. Now click Finish.
    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.
    Windows Terminal on Microsoft Store
  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.
    Windows Terminal Settings Customization
  5. If you do not see the Open Settings prompt, then simply click the drop-down arrow, and click on Settings.
    Settings
  6. From Startup, make sure that the Default profile is set on PowerShell (The new PowerShell we installed in Step 2). The Default terminal application should be set on Windows Terminal.
    Default startup config
  7. Then click Save and exit the terminal.
    Save

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

Step 5: WSL & Ubuntu LTS

Now we need to install the WSL2 and Ubuntu LTS.

  1. Open the Windows Terminal as an Administrator.
    Open terminal as an administrator
  2. For installing the WSL, use the command wsl --install.
    VMP install
    WSL install
  3. Then, it would automatically install the latest LTS version of Ubuntu.
    Ubuntu install
  4. After the tasks get finished, it would prompt you for rebooting your PC. Save other works and simply restart your computer.
    Reboot PC
  5. After restarting the PC, it would automatically open the terminal and ask you for the username and password for your Linux OS.
    After rebooting PC
  6. Give the Username and Password. Make sure to use the same Password on Retype Password!
    username and password
  7. After a while, it will install the necessary components.
    Complete Ubuntu
  8. Now, make sure that WSL2 becomes the default WSL in this terminal. Apply the command, wsl --set-default-version 2.
    WSL 2 default
  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.
    OSes status
    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. Terminate command
  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.
    run specific distro
  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.
    regularly used command
  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.
    drop-down menu for other distributions

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.
    start Ubuntu
  2. Update the system by using the command sudo apt update.
    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.
    Upgrade
    Upgrade system
    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.
    gcc
    gcc installing
    finishing installation

Step 7: CUDA

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)

Nvidia - CUDA


This will provide the necessary commands.
commands CUDA
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
    1st command
    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
    1st command
  3. sudo mv cuda-wsl-ubuntu.pin /etc/apt/preferences.d/cuda-repository-pin-600
    2nd Command
  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
    3rd command
    This normally takes a lot of time as it downloads a large file (above 2GB file size).
    large file
  5. sudo dpkg -i cuda-repo-wsl-ubuntu-12-1-local_12.1.1-1_amd64.deb
    4th command
  6. sudo cp /var/cuda-repo-wsl-ubuntu-12-1-local/cuda-*-keyring.gpg /usr/share/keyrings/
    5th command
  7. Then update the system using sudo apt-get update.
    update system
    updating
  8. sudo apt-get -y install cuda.
    install CUDA
    finish CUDA installation

Step 8: Post installation

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.
path
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
    root
  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}}.
    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.
    path check

Step 9: Nvidia CUDA Toolkit

Install the Nvidia Cuda Toolkit using sudo apt install nvidia-cuda-toolkit.
toolkit 1
toolkit 2

You can check the Driver and CUDA versions using nvidia-smi.
smi

Also make sure to check whether the Nvidia Cuda Compiler Driver has been installed or not by using nvcc -V.
nvcc

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.
    pip install

Step 10: PyTorch

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.
PyTorch command

I simply used that exact command in my Ubuntu terminal.
PyTorch command

It also downloads a large file that can take a lot of time if you have a slower internet connection like me!
PyTorch download

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!
cuda yes

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.
    nvidia control panel
  2. Click "Agree and Continue".
    agree.
  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.
    not check mark
    check mark
  4. The Developer section will be visible. Click on Manage GPU Performance Counters.
    GPU counters
  5. Check the radio button on "Allow access to the GPU performance counters to all users".
    check
  6. Click "Apply" and "Yes" to approve the changes permanently.
    approve changes
  7. At the end, it should look like this.
    final
  8. You can again check the CUDA availability like earlier.
    cuda availability

Step 12: Jupyter Notebook

I normally prefer the Jupyter Notebook. It is true that you can install that in various ways, like pip install notebook.
notebook 1
notebook 2

But I prefer the command pip install jupyter notebook.
jupyter notebook

To open a Jypyter Notebook, you can simply use jupyter notebook in the terminal.
notebook cli

The notebook will open instantly and you can use the given URL to open that in your web browser like me!
open notebook in browser

Step 13: Test

I ran two codes to check the performance of my CUDA.

  1. Open a Python3 script in notebook.
    open script
  2. 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") 


cuda

  1. 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)

CPU vs GPU


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.

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.

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.


remove deb

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.