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.
Windows 10 or 11: Your Windows operating system should be version 10 or 11 with administrative access.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.Basic Programming Knowledge: Familiarity with fundamental programming concepts and command-line interfaces is helpful.Python & VS Code Fundamentals: A basic understanding of Python and the basics of Visual Studio Code will streamline your setup process.NVIDIA Drivers: Ensure you have the latest NVIDIA drivers installed on your Windows system for GPU support (if applicable).
Python Version: 3.9–3.12cuDNN: 8.9CUDA: 12.3
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.
Windows Settings (GUI): Go to Settings > Update & Security > For Developers. Enable "Windows Subsystem for Linux". Reboot your system.
Search "Windows Features" and click on "Turn Windows features on or off".Check both "Virtual Machine Platform" and "Windows Subsystem for Linux". Reboot your machine if you enabled any features.
Open the Microsoft Store and search for "Ubuntu 20.04.6 LTS" (or the latest version available).Click "Get" to download and install the distribution.Open Ubuntu from the Start Menu. The terminal will initialize the Ubuntu environment.Create a username and password when prompted.
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.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.
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
Install the Keyring: Run the command: sudo dpkg -i cuda-keyring_1.1-1_all.deb
Update Package List and Install CUDA Toolkit: Run the commands:
sudo apt-get update sudo apt-get -y install cuda-toolkit-12-3
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
Verify Installation: Run the diagnostic script or the command: nvcc --version. Ensure CUDA paths were added to your ~/.bashrc file.
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.Move the downloaded .deb file to your Linux home directory. (accessible by pasting \\wsl$ into your Windows File Explorer).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
Verify Installation: Run the diagnostic script or the command: dpkg -l | grep libcudnn || echo "No cuDNN installation found."
Add the Python repository: Run the command: sudo add-apt-repository ppa:deadsnakes/ppa
Update package list and install Python: Run the commands:
sudo apt-get update sudo apt install python3.12 python3.12-venv python3.12-dev
Verify Installation: Run the diagnostic script or the command: ls /usr/bin/python* | tr '\n' ' '.
Create a virtual environment using Python 3.12: Run the command: python3.12 -m venv myenv
Activate the virtual environment: Run the command: source myenv/bin/activate
Upgrade pip: Run the command: pip install --upgrade pip
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 pipVerify 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.
Download Visual Studio Code: https://code.visualstudio.com/ 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.
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.
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__)
0 comments:
Post a Comment