LOADING

WELCOME

Technical Documentation for Clean Installation of Ubuntu, CUDA, cuDNN, and PyTorch on WSL2

2025/3/23

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

  1. Open Windows Terminal, PowerShell, or Command Prompt as Administrator.

  2. List all installed WSL distributions:

    wsl --list -v
    
  3. 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)

  1. Ensure WSL2 is enabled and set as the default WSL version:

    wsl --set-default-version 2
    
  2. Install Ubuntu using WSL:

    wsl --install -d Ubuntu-24.04
    
  3. 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

  1. Open the Ubuntu terminal.

  2. Run the following commands to update and upgrade the system:

    sudo apt update && sudo apt upgrade -y
    
  3. Install essential development tools:

    sudo apt install -y build-essential git wget curl
    

Step 4: Install CUDA (Version 12.4)

  1. Visit the CUDA 12.4.1 download page:
    CUDA 12.4.1 Download Archive

  2. Follow 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, press Enter, and exit using Ctrl+X.

  3. Update package lists and install CUDA:

    sudo apt update
    sudo apt-get -y install cuda-toolkit-12-4
    
  4. Configure environment variables by editing the .bashrc file:

    nano ~/.bashrc
    
  5. 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
    
  6. Save the file using Ctrl+O, press Enter, and exit using Ctrl+X.

  7. Apply the changes:

    source ~/.bashrc
    
  8. Verify the CUDA installation:

    nvcc --version
    nvidia-smi
    

Step 5: Install cuDNN

  1. Visit the cuDNN download page: cuDNN Downloads
  2. Follow the installation instructions provided on the page.
  3. Ensure compatibility with the installed CUDA version (12.4).

Step 6: Install PyTorch

  1. Install Python and pip if not already installed:

    sudo apt install -y python3 python3-pip python3-venv
    
  2. Create a virtual environment for PyTorch:

    python3 -m venv pytorch_env
    
  3. Activate the virtual environment:

    source pytorch_env/bin/activate
    
  4. Install PyTorch with CUDA support:

    pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu124
    
  5. 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')"
    
  6. 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: