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

Step 7: Install Dependencies

git (required)

If you have already git then skip this else do this.

go to https://git-scm.com/download/win download git and install with all default settings.

CMake (required)

Goto https://cmake.org/download/ and download Windows win64-x64 Installer.

Direct link https://cmake.org/files/v3.10/cmake-3.10.1-win64-x64.msi

Install it but make sure to check Add Cmake to system path for all users.

swig (required)

goto http://www.swig.org/download.html and download swigwin.

Direct link http://prdownloads.sourceforge.net/swig/swigwin-3.0.12.zip

Extract it to C:\

Step 8: Verify if your CPU support AVX/ AVX2:

AVX/AVX2 optimization make tensorflow perform faster.

You can verify for AVX/ AVX2 support for your CPU by using Coreinfo tool.

Goto https://docs.microsoft.com/en-us/sysinternals/downloads/coreinfo to get it.

Direct link https://download.sysinternals.com/files/Coreinfo.zip

Extract it to C:\

Goto run (Win+R) and type cmd

Run following command:

"C:\Coreinfo\Coreinfo.exe"

Look for AVX

AVX             *       Supports AVX intruction extensions

If you got * then it supports AVX.

Step 9: Configure Tensorflow from source using CMake:

Start the process of building TensorFlow by downloading latest tensorflow 1.5.0

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:\
git clone https://github.com/tensorflow/tensorflow
cd tensorflow
git checkout r1.5
cd tensorflow\contrib\cmake
mkdir build
cd build

(Optional)

Activate your virtual environment eg. Virtualenv, conda, anaconda with python version 3.5. This makes CMake detect python automatically with python related to your virtual environment.

Install numpy (required):

pip install --upgrade numpy
pip install --upgrade wheel

If AVX is supported by your CPU then do following:

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 ..

Else do following:

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 ..

You can see:

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

Goto “C:\tensorflow\third_party\gpus\cuda\ ” and open “cuda_config.h” with any editor or notepad.

Edit this line with your CUDA capability noted in step 1:

 #define TF_CUDA_CAPABILITIES CudaVersion("3.0"),CudaVersion("3.5"),CudaVersion("5.2")

For my GPU, CUDA capability is 5.0

#define TF_CUDA_CAPABILITIES CudaVersion("5.0")

Verify following files with numbers as specified below:

#define TF_CUDA_VERSION "64_91"
#define TF_CUDNN_VERSION "64_7"

"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.1\bin\cudart64_91.dll"
"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.1\bin\cudnn64_7.dll"

If all good then we are ready to build Tensorflow.

Step 10: Build Tensorflow using MSbuild

Copy this file (required):

"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.1\include\crt\math_functions.hpp" to "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.1\include\"

(optional)

Disable real-time protection of Windows Defender or any other antivirus. We found that doing this dramatically decreases build time by a few hours.

Build using MSbuild to create whl file (pip package) using the following command:

MSBuild /p:Configuration=Release /verbosity:detailed tf_python_build_pip_package.vcxproj

It will take 4 -5 hours if antivirus disabled.

Step 11: Install Tensorflow GPU on Windows

Finally… Activate virtual environment here where you want to install tensorflow.

To install tensorflow gpu on windows with pip:

pip install "C:\tensorflow\tensorflow\contrib\cmake\build\tf_python\dist\tensorflow_gpu-1.5.0-cp35-cp35m-win_amd64.whl"

note- correct whl filename if not found.

Step 12: Verify Tensorflow installation

Run in terminal

python

import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))

If the system outputs the following, then you are ready to begin writing TensorFlow programs:

Hello, TensorFlow!

Success! You have now successfully installed tensorflow GPU on windows machine.

Since it is experimental as stated on the official website, comment below if any errors occurred.

If you can’t make it, then go to  How to install Tensorflow 1.5.0 using official pip package. for alternative.

For prebuilt wheel with optimization for AVX2 and cuda 9.1, cudnn 7.0.5, compute capability 5.0 go to this link .

Do let us know in the comments below if it worked for you. Or if you got any errors. Cheers!



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.