Step by step guide to setup Tensorflow with GPU support on windows 10

Melika
5 min readMay 5, 2022

Hi! Hope you’re in a good mood today. In this article, we are going to set up a new anaconda environment with TensorFlow 2.8, GPU support using NVIDIA CUDA 11.6 and CUDNN 8.3 along with Python 3.8

I know getting the CUDA support up and running on TensorFlow is such a pain point and that is why there are posts and videos on different platforms for how to do it properly, and this article is yet another way. hope you could follow the guide step by step and enjoy the result!

Take a deep breath and let's dive in!

Steps:

let’s take a brief look at all steps

  1. NVIDIA Graphics Driver: You can download the drivers that match your GPU from this link. you need to select your graphics card and operating system to be able to download the correct driver
  2. Visual Studio 2019 community edition— for C++ runtime: You can download it from the official website here.
  3. Anaconda: You can get the latest version of Anaconda from here.
  4. CUDA Toolkit for Windows 10:You can download the CUDA toolkit from this link. Note that before deciding the version to download, check this table to see the CUDA version that supports your driver version
  5. CUDNN: CUDNN is a set of C/C++ Libraries that expose the deep neural network capabilities on top of CUDA. All deep learning frameworks use CUDNN to use NVIDIA GPUs — including TensorFlow. you can download your CUDNN from this.
  6. New conda environment: after installing all the above, we make a new environment and do the magic

my setup:

Operating System: Windows 10 Enterprise

Graphics Card: NVIDIA GeForce 940MX

Graphic driver version: 511.23

Step 1: NVIDIA Graphics Driver Installation

Here is a sample download for my setup from https://www.nvidia.com/download/index.aspx :

Once you have found and downloaded the setup binary that suits your machine, just double-click on the file to run it. Click next as many times as you are prompted to and …Congratulations you have just completed step 1.

Step 2: Visual Studio 2019 Installation

I am using the community edition which is a full development environment and it’s totally free! If you have any other license (professional or enterprise) that is also fine. Download the visual studio installer from https://visualstudio.microsoft.com/downloads/, Double click the file, and make sure that you have the highlighted (“Desktop development with C++”) workloads installed. This will install the required C++ toolchain.

This will take a while to install, wait for it to finish. restart your computer before going to the next step.

Step 3: Anaconda Installation

Anaconda is a python distribution that allows you to have several python environments on one machine. This gives you the freedom to work with multiple isolated versions of python without relying on a global environment that might have package conflicts. Download the installer from https://www.anaconda.com/ and finish the steps with the regular options.

Step 4: CUDA Toolkit

choose the CUDA toolkit version that suits your machine from https://developer.nvidia.com/cuda-toolkit-archive. Download the installer and just click next until finished. I download CUDA toolkit version 11.6 for my windows 10.

CUDA download

Step 5: CUDNN Library

Now you should install the CUDNN library which is used by tensorflow for creating deep neural networks on GPU using CUDA. Visit the page https://developer.nvidia.com/rdp/cudnn-download and download CUDNN for your CUDA Version. Please note that in order to access the download page, you need to have an NVIDIA developer account (which is free to create)

Download the zip file as shown above, and extract the contents in any directory you want. Open the extracted folder, and also open another explorer window with the following path: C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA. Inside this folder, you will find a folder for your current CUDA toolkit version, for example for me it is v11.6. Copy the files from the extracted folder like so. In the below tables ALL_CAPS values like these refer to the specific parameters specific to your environment. for example CUDA_VERSION refers to the version of CUDA that you downloaded, likewise EXTRACTED_FOLDER refers to the path where you extracted the CUDNN Library zip file.

Please note that you need to copy the files of the above folders and not the folders themselves.

Now let’s verify that you have the below listed Environment Variables created on your machine.

here is mine:

If you need to know how to access and set environment variables on Windows 10, read this tiny article to help you through.

Step 6: Create a Conda environment

Here we are! there are some little short steps left, you can do it!

In order to create the conda environment with Python version 3.8, type Anaconda Prompt in your start menu and open it.

now create an environment with any desired name with the below command, my environment name is gpu

conda create --name gpu python=3.8

activate your environment

conda activate gpu

then we can install TensorFlow

pip install --upgrade tensorflow-gpu

open vs code

code

now check if everything is working fine:

import sys
import tensorflow.keras
import tensorflow as tf
import numpy as np
print(f"Tensor Flow Version: {tf.__version__}")
print(f"Keras Version: {tensorflow.keras.__version__}")
print()
print(f"Python {sys.version}")
print("GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))
tf.test.is_built_with_cuda()
tf.test.is_gpu_available(cuda_only=False, min_cuda_compute_capability=None)

congratulations!

you did it :)

comment, if you face any issues.

--

--

Melika

computer engineer, bookworm. Love to learn, create and help! I write about various things, from AI, motivation and … who knows what’s next :)