How to install TensorFlow on Windows 11 (WSL2, Ubuntu 22.04) with CUDA and cuDNN

This instruction shows how to install TensorFlow on Windows 11 (WSL2, Ubuntu 22.04) with CUDA and cuDNN.

Lod in

Reference: https://codensync.com/blog/2023/11/23/how-to-install-ubuntu-22-04-on-wsl2-on-windows-11/

Install virtual environment management system Miniconda

Run the following command to install the latest 64-bit version of the installer and then clean up after themselves.

mkdir -p ~/miniconda3
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda3/miniconda.sh
bash ~/miniconda3/miniconda.sh -b -u -p ~/miniconda3
rm -rf ~/miniconda3/miniconda.sh

After installing, run the following commands initialize for bash and zsh shells:

~/miniconda3/bin/conda init bash
~/miniconda3/bin/conda init zsh

You need to close and re-open the current shell for changes to take effect. First, exit the shell:

exit

Then, run the following command to enter the shell:

ubuntu

Run the following command to verify the installation:

conda --version

Create a virtual environment with Python 3.11

Run this command to create a virtual environment ‘tf’ with python version 3.11

conda create --name tf python=3.11

Activate virtual environment ‘tf’

conda activate tf

There will be a ‘(tf)’ prefix in the command line. If you want to deactivate current virtual environment, run the following command:

conda deactivate

Activate virtual environment ‘tf’, install gcc and g++, install CUDA11.8

conda activate tf

Install gcc and g++

sudo apt update
sudo apt install gcc g++

Run the following commands to download and install CUDA 11.8

wget https://developer.download.nvidia.com/compute/cuda/11.8.0/local_installers/cuda_11.8.0_520.61.05_linux.run
sudo sh cuda_11.8.0_520.61.05_linux.run

Install cuDNN 8.6

Install cuDNN 8.6.0.163

pip install nvidia-cudnn-cu11==8.6.0.163

To check the installation location of cudnn:

python -c "import nvidia.cudnn;print(nvidia.cudnn.__file__)"

Add environment variables

Add environment variables:

# Need to adapt to your cudnn installation location:
export CUDNN_PATH="$HOME/miniconda3/envs/tf/lib/python3.11/site-packages/nvidia/cudnn"
export LD_LIBRARY_PATH="$CUDNN_PATH/lib":"/usr/local/cuda/lib64"

export PATH="$PATH":"/usr/local/cuda/bin"

Check CUDA version:

nvcc --version

Install TensorFlow 2.14.1

Install TensorFlow 2.14.1

pip install tensorflow==2.14.1

Check installation version

pip show tensorflow

Check if GPU acceleration can be enabled:

python3 -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"

If you see the following text, the GPU acceleration can be enabled.

[PhysicalDevice(name=’/physical_device:GPU:0′, device_type=’GPU’)]

Leave a Reply

Your email address will not be published. Required fields are marked *