[Update 1] How to build and install TensorFlow GPU/CPU for Windows from source code using bazel and Python 3.6
This is an update for my previous story. What’s new here:
- TensorFlow v1.11
- CUDA v10.0
- cuDNN v7.3
There is guide on official site. It is not very comprehensive but useful sometimes.
- 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 10.0 and cuDNN 7.3 (for GPU acceleration)
- Configure build environment
- Clone TensorFlow v1.11 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, unzipand 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
We have to install “VC++ 2015.3 v14.00 (v140) toolset for desktop” from Visual Studio 2017 Build Tools to build TensorFlow v1.11:
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.17.2. 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.
Looks like TensorFlow v1.11 is not supporting Anaconda/Miniconda for build any more — I get strange error. That’s why I use Python virtual environment for build.
Python 3.6 is available for download here. Install it and add location to python.exe to the %PATH% variable.
Step 6: Install NVIDIA CUDA 10.0 and cuDNN 7.3 (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 10.0 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.3.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\v10.0”.
Final step here is to add “C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0\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 commands below (correct paths according your locations).
pip3 install -U virtualenv
virtualenv --system-site-packages C:\Users\amsokol\tensorflow-v1.11
Your shell should looks like that after commands applied:
Install mandatory Python packages:
pip3 install six numpy wheel
pip3 install keras_applications==1.0.5 --no-deps
pip3 install keras_preprocessing==1.0.3 --no-deps
Run “pip3 list” to ensure the mandatory packages are installed:
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.11 version:
git checkout v1.11.0
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.17.2 installed.
Please specify the location of python. [Default is C:\Users\amsokol\tensorflow-v1.11\Scripts\python.exe]:
Then its asks location of Python library paths. Press Enter to leave default value:
Traceback (most recent call last):
File "<string>", line 1, in <module>
AttributeError: module 'site' has no attribute 'getsitepackages'
Found possible Python library paths:
Please input the desired Python library path to use. Default is [C:\Users\amsokol\tensorflow-v1.11\Lib\site-packages]
Then it asks about nGraph support. We don’t need it. Press “n”:
Do you wish to build TensorFlow with nGraph support? [y/N]: n
No nGraph support will be enabled for TensorFlow.
Then 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 10.0 as CUDA SDK version:
Please specify the CUDA SDK version you want to use. [Leave empty to default to CUDA 9.0]: 10.0
Press Enter to leave default CUDA toolkit location:
Please specify the location where CUDA 10.0 toolkit is installed. Refer to README.md for more details. [Default is C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.0]:
Answer 7.3.1 as cuDNN version:
Please specify the cuDNN version you want to use. [Leave empty to default to cuDNN 7.0]: 7.3.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/v10.0]:
Next question is about CUDA 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
Next 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
Last question is about eigen. Answer “y”. It reduces compile time dramatically.
Would you like to override eigen strong inline for some C++ compilation to reduce the compilation time? [Y/n]: y
Eigen strong inline overridden.
Configuration completed. Lets build.
Step 10: Build TensorFlow from sources
Ensure we are in source code root folder:
Build takes long time. 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 sometime.
Step 11: Create TensorFlow wheel file for Python 3.6
Run command to create Python wheel file:
There is a known issue. Look at the “bazel-bin\tensorflow\tools\pip_package” folder. It contains zero length “simple_console_for_windows.zip” file. This is the problem. Bazel contains 32-bits zip utility that fails for file 2GB bigger. See links for details and workaround:
There are steps to fix the problem:
Open “simple_console_for_windows.zip-0.params” file and remove line contains “mnist.zip”:
It helps me. In case it does not help you just remove other lines with zip files (see details here). The purpose of this activity is to make “simple_console_for_windows.zip” length less then 2GB.
Delete empty “simple_console_for_windows.zip” file.
Next look at the your home folder. You have to see the folder with name like “_bazel_<username>”. It is “_bazel_amsokol” in my case. It contains folder with build files. It is “lx6zoh4k” in my case. Back to shell an run (correct according your folder names):
Create “simple_console_for_windows.zip” file manually:
external\bazel_tools\tools\zip\zipper\zipper.exe vcC bazel-out/x64_windows-opt/bin/tensorflow/tools/pip_package/simple_console_for_windows.zip @bazel-out/x64_windows-opt/bin/tensorflow/tools/pip_package/simple_console_for_windows.zip-0.params
Run command to create Python wheel file:
It creates tensorflow-1.11.0-cp36-cp36m-win_amd64.whl file in the “..\out” folder.
Step 12: Install TensorFlow wheel file for Python 3.6 and check result
Run command to install Python wheel file:
pip3 install ..\out\tensorflow-1.11.0-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!