Thursday, October 3, 2024

How to Setup TensorFlow on Windows with WSL2 and Visual Studio Code IDE

This comprehensive guide walks you through setting up a robust development environment for TensorFlow 2.17, utilizing the power of Linux tools within the comfort of your Windows environment. We'll use WSL2 (Windows Subsystem for Linux) to seamlessly integrate TensorFlow's native Linux functionality with Visual Studio Code's user-friendly interface.

Why Choose WSL2?

TensorFlow, being natively designed for Linux, requires an emulation layer for Windows users. WSL2 provides a lightweight and efficient solution by running a Linux system directly inside Windows. This allows you to leverage the extensive Linux-based tools and libraries, including TensorFlow, while maintaining your familiar Windows desktop experience.

Prerequisites:

  1. Windows 10 or 11: Your Windows operating system should be version 10 or 11 with administrative access.

  2. CUDA-compatible GPU: While optional, a CUDA-enabled GPU is highly recommended for significantly accelerating TensorFlow's performance. You can check your GPU's compatibility on NVIDIA's website.

  3. Basic Programming Knowledge: Familiarity with fundamental programming concepts and command-line interfaces is helpful.

  4. Python & VS Code Fundamentals: A basic understanding of Python and the basics of Visual Studio Code will streamline your setup process.

  5. NVIDIA Drivers: Ensure you have the latest NVIDIA drivers installed on your Windows system for GPU support (if applicable).

Key Requirements for TensorFlow 2.17:

  • Python Version: 3.9–3.12

  • cuDNN: 8.9

  • CUDA: 12.3

Step 1: Installing WSL2

  1. PowerShell (Command-Line):

    • Open PowerShell as administrator (search "PowerShell" and right-click, choose "Run as administrator").

    • Run the command wsl --install to install WSL2.

    • Reboot your system when prompted.

    • Verify installation by running wsl --version.

  2. Windows Settings (GUI):

    • Go to Settings > Update & Security > For Developers.

    • Enable "Windows Subsystem for Linux".

    • Reboot your system.

Step 2: Enabling Necessary Windows Features

  1. Search "Windows Features" and click on "Turn Windows features on or off".

  2. Check both "Virtual Machine Platform" and "Windows Subsystem for Linux".

  3. Reboot your machine if you enabled any features.

Step 3: Installing Ubuntu

  1. Open the Microsoft Store and search for "Ubuntu 20.04.6 LTS" (or the latest version available).

  2. Click "Get" to download and install the distribution.

  3. Open Ubuntu from the Start Menu. The terminal will initialize the Ubuntu environment.

  4. Create a username and password when prompted.

Step 4: Diagnosing the Environment

  1. CUDA Compatibility: Verify your GPU's CUDA compatibility using the list of supported GPUs on NVIDIA's documentation. Ensure you've installed the latest NVIDIA drivers.

  2. Diagnostic Script (Optional):

    • Copy and paste the following script into a text file named diagnostics.sh and place it in your Linux home directory (accessible by pasting \\wsl$ into your Windows File Explorer).

          #!/bin/bash
    
    echo "=== CUDA, GCC, and System Diagnostics ==="
    
    # CUDA Version Check
    if command -v nvcc > /dev/null 2>&1; then
        echo "CUDA version (nvcc):"
        nvcc --version | grep "release"
    else
        echo "nvcc (CUDA) is not installed."
    fi
    
    # NVIDIA GPU and Driver Information
    echo "NVIDIA GPU and Driver Info (nvidia-smi):"
    if command -v nvidia-smi &> /dev/null; then
        nvidia-smi --query-gpu=name,driver_version --format=csv,noheader
    else
        echo "nvidia-smi not found."
    fi
    echo ""
    
    # Installed CUDA Versions
    echo "Checking for installed CUDA versions..."
    if ls /usr/local/cuda* 1> /dev/null 2>&1; then
        for dir in /usr/local/cuda*; do
            echo $dir
        done
    else
        echo "No CUDA installation found."
    fi
    
    # Installed CUDA Packages
    echo "Installed CUDA packages (trimmed list):"
    dpkg -l | grep -E "cuda|libcudnn" | awk '{print $2}' | tr '\n' ','
    echo ""
    
    # Installed Python Versions
    echo "Checking installed Python versions:"
    ls /usr/bin/python* | tr '\n' ' '
    echo ""
    
    # TensorFlow GPU Compatibility
    python3 -c "
    import tensorflow as tf
    physical_devices = tf.config.list_physical_devices('GPU')
    if physical_devices:
        print(physical_devices)
    else:
        print('No GPU detected or TensorFlow not installed')
    " 2>&1 | grep -vE "Unable to register cuDNN|Unable to register cuFFT|Unable to register cuBLAS|NUMA|TF-TRT Warning|cpu_feature_guard|AVX2 FMA" ||
    echo "TensorFlow not installed or GPU not detected."
    echo ""
    
    # CUDA Paths in Environment Variables
    echo "Checking for CUDA paths in environment variables:"
    echo "PATH: $PATH"
    echo "LD_LIBRARY_PATH: $LD_LIBRARY_PATH"
    echo ""
    
    # Python Paths in Environment Variables
    echo "Checking for Python paths in environment variables:"
    echo "PATH for Python:"
    echo "$PATH" | grep -i python || echo "No Python path found in PATH."
    echo ""
    
    # Full System Environment for CUDA Paths
    echo "Checking full system environment for CUDA paths:"
    echo "LD_LIBRARY_PATH=$LD_LIBRARY_PATH"
    echo "PATH=$PATH"
    echo ""
    
    # CUDA Paths in ~/.bashrc
    if [ -f ~/.bashrc ]; then
        grep -E "cuda" ~/.bashrc || echo "No CUDA paths found in ~/.bashrc."
    else
        echo "~/.bashrc not found."
    fi
    echo ""
    
    # Additional Python Versions
    echo "Checking for additional Python versions:"
    ls /usr/bin/python* | tr '\n' ' '
    echo ""
    
    # cuDNN Installations
    echo "Checking for cuDNN installations:"
    dpkg -l | grep libcudnn || echo "No cuDNN installation found."
    echo ""
    
    # Alternative CUDA Configurations
    update-alternatives --query cuda | grep 'Value:'
    echo ""
    
    # GPU-Related Issues in System Logs
    echo "Checking system logs for GPU-related issues:"
    dmesg | grep -i nvidia | tail -n 10 || echo "No GPU-related issues in system logs."
    echo ""
    
    echo "=== Diagnostics Completed ==="
        
    • Make the script executable by running chmod +x diagnostics.sh.

    • Execute the script: ./diagnostics.sh

    Alternatively, you can check for NVIDIA driver installation with the command: nvidia-smi. If it's not found, install it using sudo apt install nvidia-smi.

Step 5: Installing CUDA 12.3

  1. Download the CUDA Keyring:

    • Open a terminal in your WSL environment.

    • Run the following command: wget https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/cuda-keyring_1.1-1_all.deb

  2. Install the Keyring:

    • Run the command: sudo dpkg -i cuda-keyring_1.1-1_all.deb

  3. Update Package List and Install CUDA Toolkit:

    • Run the commands:

          sudo apt-get update
    sudo apt-get -y install cuda-toolkit-12-3
        
  4. Add CUDA to $PATH:

    • Edit your bashrc file: echo 'export PATH=/usr/local/cuda/bin:$PATH' >> ~/.bashrc

    • Add library path: echo 'export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH' >> ~/.bashrc

    • Source the changes: source ~/.bashrc

  5. Verify Installation:

    • Run the diagnostic script or the command: nvcc --version.

    • Ensure CUDA paths were added to your ~/.bashrc file.

Step 6: Installing cuDNN 8.9

  1. Create a Free NVIDIA Developer Account and download cuDNN using this link: https://developer.nvidia.com/downloads/compute/cudnn/secure/8.9.7/local_installers/12.x/cudnn-local-repo-ubuntu2004-8.9.7.29_1.0-1_amd64.deb/. You'll need to be signed in to download the .deb file.

  2. Move the downloaded .deb file to your Linux home directory. (accessible by pasting \\wsl$ into your Windows File Explorer).

  3. Install cuDNN:

    • Run the commands:

          sudo dpkg -i cudnn-local-repo-ubuntu2004-8.9.7.29_1.0-1_amd64.deb
    sudo cp /var/cudnn-local-repo-ubuntu2004-8.9.7.29/cudnn-local-30472A84-keyring.gpg /usr/share/keyrings/
    sudo apt-get update
    sudo apt-get install libcudnn8 libcudnn8-dev
        
  4. Verify Installation:

    • Run the diagnostic script or the command: dpkg -l | grep libcudnn || echo "No cuDNN installation found."

Step 7: Installing Python 3.12

  1. Add the Python repository:

    • Run the command: sudo add-apt-repository ppa:deadsnakes/ppa

  2. Update package list and install Python:

    • Run the commands:

          sudo apt-get update
    sudo apt install python3.12 python3.12-venv python3.12-dev
        
  3. Verify Installation:

    • Run the diagnostic script or the command: ls /usr/bin/python* | tr '\n' ' '.

Step 8: Setting Up a Virtual Environment

  1. Create a virtual environment using Python 3.12:

    • Run the command: python3.12 -m venv myenv

  2. Activate the virtual environment:

    • Run the command: source myenv/bin/activate

Step 9: Installing TensorFlow

  1. Upgrade pip:

    • Run the command: pip install --upgrade pip

  2. Install TensorFlow with CUDA support:

    • Run the command: pip install tensorflow[and-cuda]

    If you don't have pip installed, install it first and then upgrade: sudo apt install pip

  3. Verify GPU Detection:

    • Run the command: python3 -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))". You should see the output: [PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')].

    Note: There may be warnings about registering factory plugins and NUMA nodes. These are known limitations in TensorFlow 2.17 within a WSL2 environment and do not affect functionality.

Step 10: Installing Visual Studio Code and Linking with WSL2

  1. Download Visual Studio Code: https://code.visualstudio.com/

  2. Install the WSL Extension:

    • Launch Visual Studio Code.

    • Click the Extensions icon (four squares) on the left sidebar.

    • Search for "WSL" and install the extension.

  3. Connect to Your WSL Environment:

    • Click the WSL icon in the explorer (left sidebar).

    • Select your Ubuntu distribution from the list.

    • Visual Studio Code will reload and connect to your WSL environment.

  4. Verify the Setup:

    • Open a terminal in Visual Studio Code.

    • Activate your virtual environment: source myenv/bin/activate

    • Start a Python shell: python3.12

    • Import TensorFlow and print its version:

          import tensorflow as tf
    print(tf.__version__)
        

You're Now Ready to Use TensorFlow in Your Windows Environment!

Congratulations! You've successfully set up a complete and functional development environment for TensorFlow 2.17 using WSL2 on Windows. Start exploring the exciting world of machine learning with your newly configured environment!

0 comments:

Post a Comment