Cuda Compatible Tensorflow Installation in Windows

Hitesh Aggarwal
May 19, 2020 · 7 min read

Let’s start with a basic and most confusing article about installing Tensorflow. We generally don’t know which Tensorflow version should be suitable for us and which is compatible with Cuda. So in this article, I will be discussing a very full proof methodology to get over this problem. Here I will not provide you with any other confusing resources to find Cuda compatible TensorFlow version but I will show you guys to check it on your system. So that you can find by yourself, which Cuda version is suited for particular TensorFlow version. Please read the full article and I bet you will never face this issue again.

First of all, remove previously installed Cuda and cudnn.

We will be having have these things in our system:

  1. Cuda Toolkit
  2. Cudnn
  3. Tensorflow-GPU
  4. Python-3.6 or higher( anaconda is recommended)
  5. Microsoft visual studio ( for the smooth running of Cuda without any problem)

Let’s start.

  1. Installing Python (The recommended method is b. Anaconda)

a) We can directly install python from the official site using the following link: https://www.python.org/downloads/

From this link, you can install the latest python version or any old version which you want. I recommend having python version≥3.6. One thing to keep in mind that during installation if it asks for adding the path of python in an environment variable, then you must check that box to add this in environment variables.

Image for post
Image for post
Figure 1.

As shown in Figure 1. First, check to add python 3.x version to PATH and click install now.

b) Anaconda (recommended way for easy installation of other packages and TensorFlow too in future)

Go to this link and download anaconda:

https://www.anaconda.com/products/individual and scroll down the page to find Anaconda Installer and download according to 32/64 bit windows.

Now install anaconda on your system and remember to check the box of add path to the environment. Follow this for installation of anaconda: https://docs.anaconda.com/anaconda/install/windows/

and check the box which says, “Add Anaconda3 to my PATH environment variable”.

Done!. Check anaconda installation using conda command in cmd. You will see something like this as shown in Figure 2.

Image for post
Image for post
Figure 2.

2. Which Cuda Version should install?

Before installing right Cuda version for your system, first, you need to find the Tensorflow version which you want to install. There are many different versions of TensorFlow available. And it depends on your requirement. Some may want to install the latest TensorFlow. Some may require to install lower version according to need. So this thing is user-dependent. Now after deciding your TensorFlow version (say 1.x or 2.x), go to cmd and type following command: conda search tensorflow

This command shows all the available version of Tensorflow present on Anaconda server. It includes almost all version of TensorFlow. After that type:

conda install -c anaconda tensorflow-gpu=your_version

here your_version can be any of the following: 1.12, 1.13,1.14.1.15,……..2.0,2.1 etc.

eg. Let’s say I am installing version 2.0 using:

conda install -c anaconda tensorflow-gpu=2.0

Image for post
Image for post
Figure 3.

After that command, it will prompt all packages that will be installed in our system( see above Figure 3.). Now from list, find cudatoolkit and cudnn. I have shown them by the red line in the above Figure 3. From here (using this list)we can easily see that for TensorFlow 2.0 we need

cudatoolkit -10.0.130.0 and cudnn-7.6.5(based on cuda 10.0).

It is visible in the above list. So we don’t have to search on the internet any more for this.

In the same way, you can find cudatoolkit and cudnn version for any TensorFlow version that you want to install. Now before proceeding to yes, first we need to install our cudatoolkit and cudnn that we have just found. Just type ’n’ in cmd after ‘proceed’ to cancel it.

Now we have found the compatible version of Cuda and cudnn for our TensorFlow version. Now just install cudatoolkit and cudnn.

3. Installing Cuda and cudnn with visual studio

Go to this link to install your Cuda version:

Go to Legacy Releases option and choose the correct version of Cuda to install. After that choose os as windows and then choose windows version and then download Cuda as exe(local) if asks. (see Figure 4.)

Image for post
Image for post
Figure 4.

After downloading it, install Cuda

Image for post
Image for post
Figure 5.

Click ok(see Figure 5.)and do whatever it asks during the installation process. At a certain point, it demands Microsoft visual studio during installation. So then you have to install Microsoft visual studio from here( I recommend you to install visual studio already before Cuda) :

For the latest, download community edition. For other version go to release notes and find the other versions there.

After downloading Visual studio, continue to install( maybe you require to install it from starting) Cudatoolkit. It will look like this:

Image for post
Image for post
Figure 6.

After System check( finding all required software like Nvidia graphics, Visual studio) and Accepting License Agreement, click on Express(recommended) as shown in Figure 6. Installation option and click next and wait for the installation to complete.

After this Install cudnn of right version which you have found already. For downloading cudnn, just register on this site (see Figure 7.):

https://developer.nvidia.com/rdp/form/cudnn-download-survey

Image for post
Image for post
Figure 7.

After this agree to conditions and you will see like this as shown in Figure 8.

Image for post
Image for post
Figure 8.

If you find your version of cudnn here then you are good. Otherwise, find it in Archived cuDNN Releases and download it for your windows version.

It will be downloaded as a zip file. First, unzip it in the same directory(<installpath>) and then follow the steps as referring :

https://docs.nvidia.com/deeplearning/sdk/cudnn-install/index.html#installwindows

Copy the following files into the CUDA Toolkit directory.

  1. Copy <installpath>\cuda\bin\cudnn64_7.6.5.32.dll to C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.2\bin.
  2. Copy <installpath>\cuda\ include\cudnn.h to C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.2\include.
  3. Copy <installpath>\cuda\lib\x64\cudnn.lib to C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.2\lib\x64.

After this check, if the Cuda path is set in the environment variables.

Environment Variables can be set using:

Control Panel ->System and Security->System->Advanced System settings.

After this, Click on Environment variables as shown in Fig. 9

Image for post
Image for post
Figure 9.

After this, a window opens up like shown in Figure 10.

Image for post
Image for post
Figure 10.

Go to the System variables section. Go to Path Variable and click edit as shown in Figure 11.

Image for post
Image for post
Figure 11.

After this, a window opens like as shown in Figure 12.

Image for post
Image for post
Figure 12.

We need to add 2 paths to this:

  1. C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0\bin
  2. C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0\libnvvp

If you find these variables already there, then we are done. Just close all these tabs.

But if these variables are not there, then we add both these variables by clicking on New option as shown in Figure12.

4. Installing TensorFlow

Now we have cudatoolkit and cudnn installed on our system. After this install tensorflow-gpu the same way which I have shown above using:

conda install -c anaconda tensorflow-gpu=your_version

Keep in mind that your_version should be that one for which you have installed cudatoolkit and cudnn.

Proceed to ‘y’ in cmd. Done!

5. Checking GPU usage

Now go to cmd.

type python in cmd and Enter

type: import tensorflow as tf and press Enter

then type:

sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))

Press Enter and then you should some console output mentioning GPU and its memory:

Image for post
Image for post

Yeah. We have just installed TensorFlow Compatible with Cuda and cudnn.

In the same way, you can install any TensorFlow version. Just find cudatoolkit and cudnn version by the method as shown above( 2. Which Cuda Version should install).

After that, we will install that version of Cuda and cudnn. After this, we will install tensorflow-GPU.

Done!!!……. Thanks for reading and I will be very happy if you find this article helpful.

The Startup

Medium's largest active publication, followed by +752K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store