Skip to main content
Saram Consulting LLC

TensorFlow on Windows

Prerequisites #

  1. Windows 10 or 11: Ensure that you have a 64-bit version of Windows 10 (version 2004 or later) or Windows 11.

  2. WSL Installed: Make sure you have WSL 2 installed. You can set it up using the steps below.

Installing WSL 2 #

  1. Enable WSL:

    • Open PowerShell as an administrator.
    • Run the following command:
    wsl --install

    This command will enable the required features and install the latest Ubuntu distribution. You may be prompted to restart your computer.

  2. Set the Default WSL Version to 2:

    • Run this command to set WSL 2 as the default:
    wsl --set-default-version 2
  3. Verify Installation:

    • After rebooting, you can check if WSL is working by opening Ubuntu from the Start menu and entering the following command:
    wsl --list --verbose

Installing CUDA Toolkit #

wget https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/cuda-wsl-ubuntu.pin
sudo mv cuda-wsl-ubuntu.pin /etc/apt/preferences.d/cuda-repository-pin-600
wget https://developer.download.nvidia.com/compute/cuda/12.4.1/local_installers/cuda-repo-wsl-ubuntu-12-4-local_12.4.1-1_amd64.deb
sudo dpkg -i cuda-repo-wsl-ubuntu-12-4-local_12.4.1-1_amd64.deb
sudo cp /var/cuda-repo-wsl-ubuntu-12-4-local/cuda-*-keyring.gpg /usr/share/keyrings/
sudo apt-get update
sudo apt-get -y install cuda-toolkit-12-4

Installing miniconda #

curl https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -o Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh

Installing TensorFlow #

conda create --name tf python=3.9
conda activate tf
mkdir -p $CONDA_PREFIX/etc/conda/activate.d
echo 'CUDNN_PATH=$(dirname $(python -c "import nvidia.cudnn;print(nvidia.cudnn.__file__)"))' >> $CONDA_PREFIX/etc/conda/activate.d/env_vars.sh
echo 'export LD_LIBRARY_PATH=$CONDA_PREFIX/lib/:$CUDNN_PATH/lib:$LD_LIBRARY_PATH' >> $CONDA_PREFIX/etc/conda/activate.d/env_vars.sh
pip install --upgrade pip
pip install tensorflow==2.12.*
python3 -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"

Visual Studio Code Integration #

code .

Conclusion #

With TensorFlow now running on your WSL setup, you can enjoy the flexibility of Linux development tools while working seamlessly within Windows. Happy coding and good luck with your machine learning projects!