This guide provides step-by-step instructions for setting up a clean installation of Ubuntu on Windows Subsystem for Linux 2 (WSL2), followed by installing CUDA, cuDNN, and PyTorch for machine learning development.
Prerequisites
- Windows 10 version 2004 or higher (Build 19041 or higher) or Windows 11
- A system with an NVIDIA GPU compatible with CUDA 12.4
- Administrative privileges on your Windows machine
- At least 20GB of free disk space
Step 1: Remove Old or Unused Linux Distributions
Open Windows Terminal, PowerShell, or Command Prompt as Administrator.
List all installed WSL distributions:
wsl --list -v
Unregister any unwanted distributions:
wsl --unregister DistroName
Replace
DistroName
with the name of the distribution you want to remove (e.g.,Ubuntu-20.04
).
Step 2: Install Ubuntu(Specifically Ubuntu 24.04)
Ensure WSL2 is enabled and set as the default WSL version:
wsl --set-default-version 2
Install Ubuntu using WSL:
wsl --install -d Ubuntu-24.04
Once the installation is complete, you will be prompted to reboot your system. Once the system has been rebooted, open the ubuntu terminal and set your username and password.
Step 3: Update and Upgrade Ubuntu
Open the Ubuntu terminal.
Run the following commands to update and upgrade the system:
sudo apt update && sudo apt upgrade -y
Install essential development tools:
sudo apt install -y build-essential git wget curl
Step 4: Install CUDA (Version 12.4)
Visit the CUDA 12.4.1 download page:
CUDA 12.4.1 Download ArchiveFollow the installation instructions provided on the installer page. Just before <sudo apt-get -y install cuda-toolkit-12-4>, make sure to follow this step:
# Open a new file for storing the sources list sudo nano /etc/apt/sources.list.d/ubuntu.sources # Paste the following at the end of the file: Types: deb URIs: http://old-releases.ubuntu.com/ubuntu/ Suites: lunar Components: universe Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
Save the file using
Ctrl+O
, pressEnter
, and exit usingCtrl+X
.Update package lists and install CUDA:
sudo apt update sudo apt-get -y install cuda-toolkit-12-4
Configure environment variables by editing the
.bashrc
file:nano ~/.bashrc
Add the following lines at the end of the file:
# Add local bin to PATH if [ -d $HOME/.local/bin ]; then export PATH=$HOME/.local/bin:$PATH fi # CUDA configuration export CUDA_HOME=/usr/local/cuda-12.4 if [ -d $CUDA_HOME/bin ]; then export PATH=$CUDA_HOME/bin:$PATH fi if [ -d $CUDA_HOME/lib64 ]; then export LD_LIBRARY_PATH=$CUDA_HOME/lib64:$LD_LIBRARY_PATH fi
Save the file using
Ctrl+O
, pressEnter
, and exit usingCtrl+X
.Apply the changes:
source ~/.bashrc
Verify the CUDA installation:
nvcc --version nvidia-smi
Step 5: Install cuDNN
- Visit the cuDNN download page: cuDNN Downloads
- Follow the installation instructions provided on the page.
- Ensure compatibility with the installed CUDA version (12.4).
Step 6: Install PyTorch
Install Python and pip if not already installed:
sudo apt install -y python3 python3-pip python3-venv
Create a virtual environment for PyTorch:
python3 -m venv pytorch_env
Activate the virtual environment:
source pytorch_env/bin/activate
Install PyTorch with CUDA support:
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu124
Verify the PyTorch installation and CUDA compatibility:
python3 -c "import torch; print('PyTorch version:', torch.__version__); print('CUDA available:', torch.cuda.is_available()); print('CUDA version:', torch.version.cuda if torch.cuda.is_available() else 'Not available')"
Deactivate the virtual environment when done:
deactivate
Common Troubleshooting
CUDA Not Found
If CUDA is not found after installation, verify that:
- The environment variables are correctly set in your
.bashrc
file - You’ve sourced the
.bashrc
file after modifications - The CUDA installation path matches the path in your environment variables
PyTorch CUDA Issues
If PyTorch doesn’t detect CUDA, try:
- Reinstalling PyTorch with the specific CUDA version you installed
- Checking compatibility between your PyTorch and CUDA versions
- Verifying that both CUDA and cuDNN are correctly installed
WSL2 GPU Access Issues
If WSL2 cannot access your GPU, ensure that:
- You have the latest NVIDIA drivers installed on Windows
- WSL2 GPU support is enabled in your system
- You’ve restarted your computer after driver installations
Conclusion
You now have a clean installation of Ubuntu on WSL2 with CUDA 12.4, cuDNN, and PyTorch properly configured. This setup provides a robust environment for machine learning and deep learning development.
For more information, consult the official documentation: