Installing Latest TensorFlow version with CUDA, cudNN and GPU support on Windows 11 PC

Gunter Pearson
6 min readFeb 2, 2022

--

Introduction

If you are anything like me, you know that setting up TensorFlow on a new device can be a pain. In this tutorial, I will cover the installation of CUDA, cuDNN and TensorFlow on Windows 11. The goal of this article is to make the process as painless as possible. This article assumes that you have a CUDA-compatible GPU already installed on your PC.

Installation Steps

It is very important that you follow each step in order. If you don’t follow these steps in order, then you will have to start the process over again and nobody wants to do that. So, without further ado, let’s get straight to it.

The Software that we need to install:

  • Microsoft Visual Studio (2019 Community version)
  • NVIDIA CUDA Toolkit
  • NVIDIA cuDNN
  • Python (Personally I use anaconda)
  • TensorFlow

Step 1: Remove all Current NVIDIA Drivers

Go to your search bar on windows and search “Add or remove programs”. You should now see a list of installed apps. Scroll down until you see anything that says NVIDIA. It should look something like the following image.

Now go through and delete anything that says NVIDIA on it. Don’t stress about deleting them we will install them back in later steps.

Step 2: Install Visual Studio (Not VS code)

Visual studio is required for the installation of the CUDA Toolkit. If you try to install CUDA Toolkit for Windows without installing Visual Studio, you will get the following error when downloading CUDA Toolkit.

The CUDA Toolkit also requires a certain version of Visual Studio. For this I used Visual Studio 2019 Community Version it is the most recent version that will work with the CUDA Toolkit.

Step 3: Download CUDA Toolkit

Before downloading the CUDA Toolkit the first thing we have to do is check what version we will need for this go to Tensorflow Build From Source. You should see something like the following.

For this article I am going to use the latest TensorFlow version which at the time of this article is 2.7. As of right now, the only column we are worried about is CUDA, which I have outlined in red. You can see that the version we need is CUDA 11.2. Keep this page open as we will need it later for the cuDNN version as well.

Now that we know we need CUDA version 11.2 we can install it. This is the link to the CUDA Toolkit Archive. You should see something like the following image. If you are planning on using the latest version for TensorFlow, which for me is version 2.7 then select the CUDA version I have outlined in red.

Select Windows operating system, Architecture, and at the time of this article the newest Windows version they have is Windows version 10 don’t worry this will work fine on Windows 11. Next, I selected the network installer and finally download it and install it using the express installation.

Step 4: Download and Install cuDNN

The first thing we must do is refer back to our Tensorflow Build From Source page that I mentioned earlier. This time we will be focused on the cuDNN column only for our specific version of TensorFlow.

For those following along using TensorFlow version 2.7 we will be using cuDNN version 8.1 as shown in the image above. For the actual download of the cuDNN you will have to create an account. Use this link to go to the website. Once you have created an account select Download cuDNN. It should bring you to a page that looks like this.

Select Archived cuDNN Releases. In the above image, it is outlined in red. Once this is selected, you will see a list of cuDNN version. For this we select the most recent version of cuDNN 8.1 compatible with CUDA 11.2. Below is an image of what this will look like outlined in red.

The next step gets a little tricky, so pay close attention. After downloading the cuDNN version 8.1, extract it. The extracted folder should contain three folders “bin, include, and lib”. We must copy all three folders so that we can paste them in the CUDA v11.2 folder in the following steps.

Now that we have all three folders copied, open up your file explorer go to “this PC” select “Local Disc” next select “Program Files” next select “NVIDIA GPU Computing Toolkit” then select “CUDA” and finally select your CUDA version which for me is “v11.2”.

Once you have selected your version you should now see something like the following don’t worry if it is not exactly the same.

Once you are here, paste the three folders from the cuDNN. It will throw an error because these folders already exist select replace the files in the destination option, make sure that it replaced all three folders. Now we have completed all necessary items for the installation of CUDA.

Step 5: Set up CUDA environmental variables in Windows

Now it is time to set up the environmental variables, so that TensorFlow knows where to look for our driver. In the v11.2 folder select the bin folder and copy the path.

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.2\bin

Now that we have the path copied, we can add it to our path. Go to the search bar and type in environment. Select Edit the system environment variables, a window will pop up.

Select environment variables… in the bottom right corner outlined in red a new window will pop up.

Double click on path highlighted in blue.

Select New in the top right corner and paste in the path that we copied earlier from the bin folder.

After you have pasted it select OK.

Now return back to the v11.2 folder and copy the path for the libnvvp folder and copy the path.

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.2\libnvvp

Repeat the above steps adding this location to your path. The edit environment variable page from the above image should now have the two paths added like the following.

So now that we have added both environments to our path, I would suggest restarting your computer.

Congratulations, we are done!!!!!

Step 5: Testing

For python I would suggest installing anaconda if you haven’t already. Here is the link to that installation.

If you would like some code to test your TensorFlow with GPU you can clone my machine learning repository and try it out!

--

--