How to install Tensorflow GPU on Windows

STEP BY STEP INSTALLATION OF CUDA TOOLKIT 9.1, CUDNN 7.0.5 AND TENSORFLOW 1.5.0 GPU VERSION ON WINDOWS OS

This is going to be a tutorial on how to install tensorflow GPU on Windows OS. We will be installing tensorflow 1.5.0 along with CUDA Toolkit 9.1 and cuDNN 7.0.5. At the time of writing this blog post, the latest version of tensorflow is 1.5.0. We also did the installation guide for tensorflow 1.5.0 GPU on ubuntu 16.04 which you can find in this post here. This tutorial is for building tensorflow from source. If you want to use the official pre-built pip package instead, I recommend another post, How to install Tensorflow 1.5.0 using official pip package. If you want to install tensorflow alongside CUDA 10.0, I highly recommend our other article, How to install Tensorflow GPU with CUDA 10.0 for python on Windows.

Tensorflow is an open source software library developed and used by Google that is fairly common among students, researchers, and developers for deep learning applications such as neural networks. It has both the CPU as well as GPU version available and although the CPU version works quite well, realistically, if you are going for deep learning, you will need GPU. In order to use the GPU version of TensorFlow, you will need an NVIDIA GPU with a compute capability > 3.0.

To install tensorflow GPU on Windows is complicated especially when compared to Mac or Linux OS. Even the tensorflow’s official website states, “We don’t officially support building TensorFlow on Windows; however, you may try to build TensorFlow on Windows if you don’t mind using the highly experimental Bazel on Windows or TensorFlow CMake build.” However, here is a complete step by step working tutorial to install Tensorflow GPU on Windows (64-bit only) OS using Visual Studio 2015 Update 3 and CMake.

The steps you need to take in order to install Tensorflow GPU on Windows OS are as follows:

Step 1: Verify you have a CUDA-Capable GPU:

Before doing anything else, you need to verify that you have a CUDA-Capable GPU in order to install Tensorflow GPU. You can verify that you have a CUDA-capable GPU through the Display Adapters section in the Windows Device Manager. Here you will find the vendor name and model of your graphics card(s).

The Windows Device Manager can be opened via the following steps:

Open a run window from the Start Menu or (Win+R)

Run:

control /name Microsoft.DeviceManager

If your graphics card is from NVIDIA then go to http://developer.nvidia.com/cuda-gpus and verify if listed in CUDA enabled GPU list.

Note down its Compute Capability. eg. GeForce 840M 5.0

Step 2: Install Visual Studio 2015 Update 3:

You will need Visual Studio 2015 in order to install tensorflow GPU on Windows. We tried installing with Visual Studio 2017 but it seems as if currently, Visual Studio 2017 is not fully supported to build tensorflow-gpu from source. So we recommend installing Visual Studio 2015 Update 3 by going to https://my.visualstudio.com/Downloads (Sign In required with Developer Account). Search for “Visual Studio Community 2015 with Update 3”. Download x64 version.

Goto Custom installation and make sure to install Visual C++ and Python Tools for Visual Studio inside Programming Language. Finish Installation.

Step 3: Download the NVIDIA CUDA Toolkit:

Go to https://developer.nvidia.com/cuda-downloads and download Installer for Windows [your version][network]. For me, version is Windows 10. We recommend network installer.

Install it in default location with default settings. It will update your GPU driver if required.

Step 4: Reboot the system to load the NVIDIA drivers.

Step 5: Check Cuda Toolkit is set to path:

Go to run (Win + R) type cmd

The following command will check for nvcc version and insure that it is set in path environment variable.

nvcc --version

You will see something like:

nvcc: NVIDIA (R) Cuda compiler driver

Copyright (c) 2005-2017 NVIDIA Corporation

Built on Fri_Nov__3_21:08:12_Central_Daylight_Time_2017

Cuda compilation tools, release 9.1, V9.1.85

Step 6: Install cuDNN 7.0.5:

Goto https://developer.nvidia.com/cudnn (Membership required)

After login

Download the following:

cuDNN v7.0.5 Library for Windows [your version] for me Windows 10

Goto downloaded folder and extract cudnn-9.1-windows[your version]-x64-v7.zip

Go inside extracted folder and copy all files and folder from cuda folder (eg. Bin, include, lib) and paste to “C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.1”.

68 Comments on How to install Tensorflow GPU on Windows

  1. Thank you for your tutorial. I received this error message after the code was trying to build tensorflow using MSbuild. Could you please help me to resolve it?
    C:\tensorflow\tensorflow\python\eager\pywrap_tfe_src.cc(97): error C2440: ‘=’: cannot convert from ‘const char *’ to ‘char *’ [C:\tensorflow\tensorflow\contrib\cmake\build\pywrap_tensorflow_internal_static.vcxproj]
    C:\tensorflow\tensorflow\python\eager\pywrap_tfe_src.cc(427): error C2440: ‘return’: cannot convert from ‘const char
    *’ to ‘char *’ [C:\tensorflow\tensorflow\contrib\cmake\build\pywrap_tensorflow_internal_static.vcxproj]
    C:\tensorflow\tensorflow\python\lib\core\ndarray_tensor.cc(157): error C2440: ‘=’: cannot convert from ‘const char *’
    to ‘char *’ [C:\tensorflow\tensorflow\contrib\cmake\build\pywrap_tensorflow_internal_static.vcxproj]
    C:\tensorflow\tensorflow\python\lib\core\py_func.cc(303): error C2440: ‘=’: cannot convert from ‘const char *’ to ‘ch
    ar *’ [C:\tensorflow\tensorflow\contrib\cmake\build\pywrap_tensorflow_internal_static.vcxproj]

    • hello, are you using Visual Studio 2017? On Windows 10 Visual Studio 2015 Update 3 (Visual Studio 2017 Uninstalled) should build it successfully. Also the tutorial is updated.

      • Thank you for your reply. I have uninstalled the VS 2017 and installed 2015 Update 3. However, there are still the same errors. Please guide me which step should I rollback? Thank you very much.

        • Delete build and clear temp dir and then start from step 7. Also c drive should have enough space. You can use prebuilt whl tool in case.

  2. Can anyone share their working python wheel, please (on github, for example)?

    Ideally using the released Tensorflow 1.5.0, CUDA 9.1, AVX optimizations.

    Cheers 🙂

  3. After failing to build a new Tensorflow I managed to get the latest Cuda Toolkit 9.1 work together with the Tensorflow downloaded from the alternative method above. Just copy all DLLs in C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.1\bin which have names ending with ’91’ to new DLLs ending with ’90’. All works perfectly after that.

    • Thank you for the information. This may prove to be helpful for others as well. By the way, what error did you encounter while building tensorflow?

      • Sorry, I don’t remember now. After about 20 mins of building, the process stopped and there were many red lines. It might have been wrong paths, for I had to install VS2015 and tensorflow on drive D: due to a lack of room on drive C:. Anyway I removed VS2015 after having succeeded with renaming DLLs.

    • Thank you so much for the hint! I did not succeed in the building the .whl file, but ran into your comment and got it done so fast! Awesome!

    • Hi. It may take 6-7 hours. Disabling realtime protection and other running programs may speed up the build process.

  4. Is there any way to build Tensorflow using Visual Studio 2013? I followed this tutorial with path change to Visual Studio 12 2013 Win64. It’s OK when configure using CMake, but failed when building using MSbuild. (It’s work when using VS 2015) Is there anything I can try?

    • I have not tested with Visual Studio 2013 yet. But if you want to build it than uninstall other visual studio because defining visual studio path in cmake only works for tensorflow solution but dependencies required by tensorflow will automatically select latest other version of visual studio which may give error. Please build with command “MSBuild /p:Configuration=Release /verbosity:detailed tf_python_build_pip_package.vcxproj” and comment the error.

    • Goto http://my.visualstudio.com and signin with microsoft account. Then goto downloads tab and search for “Visual Studio Community 2015 with Update 3” in search bar. Then download from there as x64 version is selected by default. Reply if any other query.

          • 1. There is no `VS2015 x64 Native Tools Command Prompt.lnk`, so I tried with `Developer Command Prompt for VS2015` instead.

            2. While installing VS 2015 on Win 10, we have to select `Microsoft SDK under Windows > Windows 8.1> `

            • VS2015 x64 Native Tools Command Prompt is shortcut created in start menu after installation of Visual Studio 2015 Update 3. We can get same effect (setting of environment) from normal command prompt by using following command [“C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat” amd64].
              Did you build tensorflow on windows successfully? I want to know if the tutorial above worked for you.

            • >“C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat” amd64

              #The system cannot find the path specified.

              We installed X64 version of VS right? Wouldn’t it be under Program Files and not Program Files (x66)

              and nope, it didn’t build.
              https://hastebin.com/uwuqukuway.log

            • Hi, Actually Visual Studio is 32 bit program but x64 bit VS include 64 bit compiler toolset. I think you have not installed VS with custom settings and not installed Visual C++ and Python Tools under programming language. Please check. Please read step 2 carefully. Also delete all files and start from step 9 might fix problem. There is alternative way at end too using pre-built pip package.

            • If the file build/zlib/install/bin/zlib.dll exists then you can ignore that error the build process should continue.

  5. Thank you for your tutorial,

    Following your instruction, I got this error message after 7:30 hours while the code was trying to build tensorflow using MSbuild. Could you please help me to resolve it

    “C:\tensorflow\tensorflow\contrib\cmake\build\tf_python_build_pip_package.vcxpr
    oj” (default target) (1) ->
    “C:\tensorflow\tensorflow\contrib\cmake\build\pywrap_tensorflow_internal.vcxpro
    j” (default target) (3) ->
    “C:\tensorflow\tensorflow\contrib\cmake\build\pywrap_tensorflow_internal_static
    .vcxproj” (default target) (4) ->
    “C:\tensorflow\tensorflow\contrib\cmake\build\tf_c.vcxproj” (default target) (5
    ) ->
    “C:\tensorflow\tensorflow\contrib\cmake\build\tf_cc_framework.vcxproj” (default
    target) (6) ->
    “C:\tensorflow\tensorflow\contrib\cmake\build\tf_core_framework.vcxproj” (defau
    lt target) (7) ->
    (CustomBuild target) ->
    C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.CppCommon.ta
    rgets(171,5): error MSB6006: “cmd.exe” exited with code 3. [C:\tensorflow\tenso
    rflow\contrib\cmake\build\tf_core_framework.vcxproj]

    • Do you have latest cmake x64? If no then install it as given in step 6 and reboot your pc. If you have the latest cmake x64 than reboot pc.

      Then Goto run (Win+R) and copy paste following:

      “C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Visual Studio 2015\Visual Studio Tools\Windows Desktop Command Prompts\VS2015 x64 Native Tools Command Prompt.lnk”

      cd c:\tensorflow\tensorflow\contrib\cmake\build
      MSBuild /p:Configuration=Release /verbosity:detailed tf_python_build_pip_package.vcxproj

      Hope this time it can build it successfully but just in case if you got error again then this time we will have detailed info. Please comment that info.

      • Thanks for your reply Arun! Actually, I installed tensorflow using the alternative method provided at the very end of the tutorial and it was successful. Could you please let me know how these two methods are compared in terms of performance?

        • I am glad to hear successful installation of tensorflow. Actually build on machine fully optimize tensorflow with optimum performance and with compatibility with installed dependencies and libraries. Tensorflow found in https://pypi.python.org/pypi/tensorflow-gpu as a whl package are built as generic so that it will work for all system.

      • Following your instruction, I got this error message after several times of tries while the code was trying to build tensorflow using MSbuild. Could you please help me to resolve it:
        “c:\tensorflow\tensorflow\contrib\cmake\build\tf_python_build_pip_package.vcxproj”(default target) (1) ->
        “C:\tensorflow\tensorflow\contrib\cmake\build\pywrap_tensorflow_internal.vcxproj”(default target) (3) ->
        “C:\tensorflow\tensorflow\contrib\cmake\build\pywrap_tensorflow_internal_static.vcxproj”(default target) (4) ->
        “C:\tensorflow\tensorflow\contrib\cmake\build\tf_c.vcxproj”(default target) (5) ->
        “C:\tensorflow\tensorflow\contrib\cmake\build\tf_cc_framework.vcxproj”(default target) (6) ->
        “C:\tensorflow\tensorflow\contrib\cmake\build\tf_core_framework.vcxproj”(default target) (7) ->
        “C:\tensorflow\tensorflow\contrib\cmake\build\proto_text.vcxproj”(default target) (8) ->
        “C:\tensorflow\tensorflow\contrib\cmake\build\grpc.vcxproj”(default target) (9) ->
        (CustomBuild target) ->
        C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.CppCommon.targets(171,5): error MSB6006: “cmd.exe” exited with code 1. [C:\tensorflow\tensorflow\contrib\cmake\build\grpc.vcxproj]
        Is it related to I’m in China?

        • Build with “MSBuild /p:Configuration=Release /verbosity:detailed tf_python_build_pip_package.vcxproj” and send me the log link. Does cmake, git set to path? Does AVX supported? Configure cmake without AVX and try to build with verbosity.

          • Thank you for the reply,here is the log:https://github.com/JiangFengJason/TensorflowLearning/blob/master/tf_python_build_pip_package.vcxproj ,and I check the path,make sure that the C:\Program Files\Git\cmd and C:\Program Files\CMake\bin is already set to path.And AVX is supported,so I follow your instruction and do this:

            cmake -G “Visual Studio 14 2015 Win64” -T host=x64 -DCMAKE_BUILD_TYPE=Release -DSWIG_EXECUTABLE=c:/swigwin-3.0.12/swig.exe -Dtensorflow_ENABLE_GPU=ON -Dtensorflow_CUDA_VERSION=9.1 -Dtensorflow_CUDNN_VERSION=7 -Dtensorflow_WIN_CPU_SIMD_OPTIONS=/arch:AVX2 ..

            and I see this successfully:

            — Configuring done
            — Generating done
            — Build files have been written to: C:/tensorflow/tensorflow/contrib/cmake/build

            so do I need to configure cmake without AVX and try to build with verbosity?and how?

            thanks again!

      • Hi, when I ran:

        cmake -G “Visual Studio 14 2015 Win64” -T host=x64 -DCMAKE_BUILD_TYPE=Release -DSWIG_EXECUTABLE=c:/swigwin-3.0.12/swig.ex
        e -Dtensorflow_ENABLE_GPU=ON -Dtensorflow_CUDA_VERSION=8.0 -Dtensorflow_CUDNN_VERSION=6 -Dtensorflow_WIN_CPU_SIMD_OPTIONS=/arch:AVX2 ..

        I got this error:

        — The C compiler identification is unknown
        — The CXX compiler identification is unknown
        CMake Error at CMakeLists.txt:5 (project):
        No CMAKE_C_COMPILER could be found.

        CMake Error at CMakeLists.txt:5 (project):
        No CMAKE_CXX_COMPILER could be found.

        — Configuring incomplete, errors occurred!
        See also “C:/tensorflow/tensorflow/contrib/cmake/build/CMakeFiles/CMakeOutput.log”.
        See also “C:/tensorflow/tensorflow/contrib/cmake/build/CMakeFiles/CMakeError.log”.

        I got this error even though I installed Visual Studio and Cmake as mentioned above. I have also put Cmake in the %PATH% list. The only difference is that I want to use CUDA=8.0 and CuDNN=6. I have attached the log, error and the build files here: https://github.com/SurajDonthi/Cmake-Error-for-building-Tensorflow-on-windows/tree/master/build/CMakeFiles

        • I ran into the same issue… If you just downloaded/installed VS Community 2015 with Update 3 (Step 2) and then proceeded without opening VS, you would definitely run into the “No CMAKE_CXX_COMPILER” issue… Reason being VSC 2015 does not download C++ dependencies when installed… The solution is to open VSC 2015 after installation completes, create a new C++ Project and there it will prompt you to install additional files. Once you do that it works like a charm!

          • Thanks a lot for this great Tutorial Arun! My first ever build from the source and it worked perfect. Just a few issues I ran into in Step 2:
            a. One has to Install Windows 8.1 SDK from ‘Individual Components’ section when installing VSC 2015. Failure to do so will result in an error message stating missing Win 8.1 SDK or something during step 9.
            b. Once installation completes, open VSC 2015 and create a C++ project – this will prompt you to download additional C++ dependencies which are not installed during the actual VSC 2015 Update 3 installation. Failure to do so will result in “No CMAKE_CXX_COMPILER” error message.

            The build took me Time Elapsed 18:07:53.69, with 20452 Warning(s), 0 Error(s).
            Thanks for the great tutorial Arun!!

Leave a Reply

Your email address will not be published.




This site uses Akismet to reduce spam. Learn how your comment data is processed.