How to build and install TensorFlow GPU/CPU for Windows from source code using bazel and Python 3.6
This is a tutorial how to build TensorFlow v1.10 with GPU (NVIDIA CUDA 9.2 + cuDNN 7.2) or CPU acceleration for Windows x64 from source code using Bazel and Python 3.6. It is possible to build package using CMake but starting TensorFlow v1.11 official support for CMake will be dropped.
- Install Git for Windows
- Install Bazel
- Install MSYS2 x64 and command line tools
- Install Visual Studio 2017 Build Tools including Visual Studio 2015 Build Tools
- Install Python 3.6 64-bits
- Install NVIDIA CUDA 9.2 and cuDNN 7.2 (for GPU acceleration)
- Configure build environment
- Clone TensorFlow source code and apply mandatory patch
- Configure build parameters
- Build TensorFlow from sources
- Create TensorFlow wheel file for Python 3.6
- Install TensorFlow wheel file for Python 3.6 and check result
Step 1: Install Git for Windows
Download and install Git for Windows. I take it here. Be sure path to git.exe is added to %PATH% environment variable. I install Git to the
folder for this tutorial.
Step 2: Install MSYS2 x64 and command line tools
Download and install 64-bits distribution here. Bazel uses grep, patch, unzip and other ports of Unix-tools to build sources. You can try to find stand-alone binaries for each of them but I prefer use MSYS2 bundle. I install it to the
folder for this tutorial. You have to add folder with tools to %PATH% environment variable. It is “C:\Bin\msys64\usr\bin” in my case.
Start “MSYS2 MinGW 64-bit” shortcut from Start Menu. Run the following command to update (restart “MSYS2 MinGW 64-bit” if it asks):
Install tools are necessary for build:
pacman -S patch unzip
Close “MSYS2 MinGW 64-bit” shell by “exit” command. We don’t need it any more.
Step 3: Install Visual Studio 2017 Build Tools including Visual Studio 2015 Build Tools
TensorFlow v1.10 bazel configuration supports Visual Studio 2017 tool chain. There is only one problem. Current version of Visual Studio 2017 Build Tools is v15.8. TensorFlow v1.10 bazel configuration accepts Visual Studio 2017 Build Tools version not higher than v15.6. That’s why we have to install “VC++ 2015.3 v14.00 (v140) toolset for desktop” also and use it to build TensorFlow:
Step 4: Install Bazel
Download latest Basel here. Look for bazel-<version>-windows-x86_64.exe file. I have tested this tutorial with bazel 0.16.1. Rename the binary to bazel.exe and move it to a directory on your %PATH%, so you can run Bazel by typing bazel in any directory. See details of Bazel installation for Windows x64 in case of problems.
Add BAZEL_SH global environment variable for bash location. My path is
Add BAZEL_VC global environment variable for “VC++ 2015.3 v14.00 (v140) toolset for desktop” tool chain location:
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC
Step 5: Install Python 3.6 64-bits
TensorFlow is not supporting Python 3.7 so you have to install 3.6 version.
I prefer Miniconda3 and use it for this tutorial. It is available for download here. You can install another Python distribution. I install Miniconda3 to the
folder for this tutorial.
Step 6: Install NVIDIA CUDA 9.2 and cuDNN 7.2 (for GPU acceleration)
This section is actual if you have NVIDIA Graphics Card that supports CUDA. Otherwise skip this section.
See step by step installation of CUDA here if you need help. I copy-paste that guide but cut some details.
Go to https://developer.nvidia.com/cuda-downloads and download CUDA 9.2 Installer for Windows [your version]. For me, version is Windows 10.
Install it in default location with default settings but uncheck the VisualStudio integration option. It will update your GPU driver if required and reboot.
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.
Next goto https://developer.nvidia.com/cudnn (Membership required).
After login download the following:
cuDNN v7.2.1 Library for Windows [your version] for me Windows 10. Goto downloaded folder and extract zip file.
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.2”.
Final step here is to add “C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.2\extras\CUPTI\libx64” to %PATH% environment variable.
Step 7: Configure build environment
Start VC++2015 shell for x64 (“VS2015 x64 Native Tools Command Prompt” shortcut) from Start Menu.
Next you have to create, activate and configure Python environment. Run inside “VS2015 x64 Native Tools Command Prompt” shell command below. You have to correct paths according your Miniconda3 location.
%windir%\System32\cmd.exe "/K" C:\Users\amsokol\Miniconda3\Scripts\activate.bat C:\Users\amsokol\Miniconda3
Your shell should looks like that after command applied where (base) means base Conda environment is activated:
Create and activate new Conda environment to build TensorFlow v1.10:
conda create -n tensorflow-v1.10 python=3.6conda activate tensorflow-v1.10
Your shell should looks like that after commands applied where tensorflow-v1.10 Conda environment is activated:
Install mandatory numpy Python package:
conda install numpy cython
That’s all for now. Do not close shell.
Step 8: Clone TensorFlow source code and apply mandatory patch
First of all you have to choose folder where to clone TensorFlow source code. It is “C:\Users\amsokol\Development\tensorflow-build” in my case. Back to shell and run:
Clone source code:
git clone https://github.com/tensorflow/tensorflow
Checkout latest of 1.10 version:
cd tensorflowgit checkout v1.10.1
Now we have sources.
There is a BUG in eigen third-party library. We have to fix it before build.
- Download patch here and save with file name eigen_half.patch to third_party folder
- Add patch_file = clean_dep(“//third_party:eigen_half.patch”), line to eigen_archive section to tensorflow/workspace.bzl file.
Result in tensorflow/workspace.bzl file should be like this:
name = "eigen_archive",
urls = [
sha256 = "d956415d784fa4e42b6a2a45c32556d6aec9d0a3d8ef48baee2522ab762556a9",
strip_prefix = "eigen-eigen-fd6845384b86",
build_file = clean_dep("//third_party:eigen.BUILD"),
patch_file = clean_dep("//third_party:eigen_half.patch"),
Step 9: Configure build parameters
Ensure we are in source code root folder:
First it asks location of Python. Press Enter to leave default value:
You have bazel 0.16.1 installed.Please specify the location of python. [Default is C:\Users\amsokol\miniconda3\envs\tensorflow-v1.10\python.exe]:
Its asks location of Python library paths. Press Enter to leave default value:
Found possible Python library paths:
Please input the desired Python library path to use. Default is [C:\Users\60051029\miniconda3\envs\tensorflow-v1.10\lib\site-packages]
It asks about CUDA support:
Do you wish to build TensorFlow with CUDA support? [y/N]:
Answer “y” if you are going to use GPU acceleration. Otherwise press “n”.
In case on Yes for CUDA configurator asks additional questions:
Answer 9.2 as CUDA SDK version:
Please specify the CUDA SDK version you want to use. [Leave empty to default to CUDA 9.0]: 9.2
Press Enter to leave default CUDA toolkit location:
Please specify the location where CUDA 9.2 toolkit is installed. Refer to README.md for more details. [Default is C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v9.2]:
Answer 7.2.1 as cuDNN version:
Please specify the cuDNN version you want to use. [Leave empty to default to cuDNN 7.0]: 7.2.1
Press Enter to leave default cuDNN library location:
Please specify the location where cuDNN 7 library is installed. Refer to README.md for more details. [Default is C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v9.2]:
Last question about CUDA is compute capabilities to build with. You can find the compute capability of your device at: https://developer.nvidia.com/cuda-gpus. I have GTX 1070 that’s why I answer 6.1:
Please specify a list of comma-separated Cuda compute capabilities you want to build with.You can find the compute capability of your device at: https://developer.nvidia.com/cuda-gpus.Please note that each additional compute capability significantly increases your build time and binary size. [Default is: 3.5,7.0]6.1
Final question is to set optimization flags. I have 6th Gen Intel CPU that’s why I answer /arch:AVX2:
Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is /arch:AVX]: /arch:AVX2
Configuration completed. Lets build.
Step 10: Build TensorFlow from sources
Ensure we are in source code root folder:
Build takes several hours. I strongly recommend turn off antivirus software including Windows Defender Antivirus real-time protection.
bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package
Sit back and relax for a long time.
Step 11: Create TensorFlow wheel file for Python 3.6
Run command to create Python wheel file:
It creates tensorflow-1.10.1-cp36-cp36m-win_amd64.whl file in “.\tmp\tensorflow_pkg” folder.
Step 12: Install TensorFlow wheel file for Python 3.6 and check result
Run command to install Python wheel file:
pip install .\tmp\tensorflow_pkg\tensorflow-1.10.1-cp36-cp36m-win_amd64.whl
Leave “tensorflow” directory (errors happens sometimes when I run Python scripts inside Tensoflow source code folder — I don’t know reason):
To make check download script here or copy-paste and run:
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
session = tf.Session()
If the system outputs the following, then everything is fine:
You have now successfully installed TensorFlow on Windows machine.
Do let me know in the comments below if it worked for you. Or if you got any errors. Thanks!