Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bygreencn/69dc4a920a5678d8d50b8983a4ac789d to your computer and use it in GitHub Desktop.
Save bygreencn/69dc4a920a5678d8d50b8983a4ac789d to your computer and use it in GitHub Desktop.
Build TensorFlow on Windows With Bazel (Preview)

Install TensorFlow from Sources on Windows (Preview)

This guide explains how to build TensorFlow sources into a TensorFlow binary and how to install that TensorFlow binary on Windows.

Determine which TensorFlow to install

You must choose one of the following types of TensorFlow to build and install:

  • TensorFlow with CPU support only. If your system does not have a NVIDIA® GPU, build and install this version. Note that this version of TensorFlow is typically easier to build and install, so even if you have an NVIDIA GPU, we recommend building and installing this version first.

  • TensorFlow with GPU support. TensorFlow programs typically run significantly faster on a GPU than on a CPU. Therefore, if your system has a NVIDIA GPU and you need to run performance-critical applications, you should ultimately build and install this version. Beyond the NVIDIA GPU itself, your system must also fulfill the NVIDIA software requirements described in the following document:

Prepare environment for Windows

Before building TensorFlow on Windows, install the following build tools on your system:

Install MSYS2

Bash bin tools are used in TensorFlow Bazel build, you can install them through MSYS2.

Assume you installed MSYS2 at C:\msys64, add C:\msys64\usr\bin to your %PATH% environment variable.

To install necessary bash bin tools, issue the following command under cmd.exe:

C:\> pacman -S git patch unzip

Install Visual C++ Build Tools 2015

To build TensorFlow, you need to install Visual C++ build tools 2015. It is a part of Visual Studio 2015. But you can install it separately by the following way:

  • Open the official downloand page.
  • Go to Redistributables and Build Tools section.
  • Find Microsoft Build Tools 2015 Update 3 and click download.
  • Run the installer.

It's possible to build TensorFlow with newer version of Visual C++ build tools, but we only test against Visual Studio 2015 Update 3.

Install Bazel

If bazel is not installed on your system, install it now by following these instructions. It is recommended to use a Bazel version >= 0.15.0.

Add the directory where you installed Bazel to your %PATH% environment variable.

Install TensorFlow Python dependencies

If you don't have Python 3.5 or Python 3.6 installed, install it now:

To build and install TensorFlow, you must install the following python packages:

  • six, which provides simple utilities for wrapping over differences between Python 2 and Python 3.
  • numpy, which is a numerical processing package that TensorFlow requires.
  • wheel, which enables you to manage Python compressed packages in the wheel (.whl) format.
  • keras_applications, the applications module of the Keras deep learning library.
  • keras_preprocessing, the data preprocessing and data augmentation module of the Keras deep learning library.

Assume you already have pip3 in %PATH%, issue the following command:

C:\> pip3 install six numpy wheel
C:\> pip3 install keras_applications==1.0.4 --no-deps
C:\> pip3 install keras_preprocessing==1.0.2 --no-deps

Optional: install TensorFlow for GPU prerequisites

If you are building TensorFlow without GPU support, skip this section.

The following NVIDIA® hardware must be installed on your system:

  • GPU card with CUDA Compute Capability 3.5 or higher. See NVIDIA documentation for a list of supported GPU cards.

The following NVIDIA® software must be installed on your system:

  • GPU drivers. CUDA 9.0 requires 384.x or higher.
  • CUDA Toolkit (>= 8.0). We recommend version 9.0.
  • cuDNN SDK (>= 6.0). We recommend version 7.1.x.
  • CUPTI ships with the CUDA Toolkit, but you also need to append its path to %PATH% environment variable.

Assume you have CUDA Toolkit installed at C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0 and cuDNN at C:\tools\cuda, issue the following commands.

C:\> SET PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\bin;%PATH%
C:\> SET PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\extras\CUPTI\libx64;%PATH%
C:\> SET PATH=C:\tools\cuda\bin;%PATH%

Clone the TensorFlow repository

Now you need to clone the latest TensorFlow repository, thanks to MSYS2 we already have git avaiable, issue the following command:

C:\> git clone https://github.com/tensorflow/tensorflow.git 

The preceding git clone command creates a subdirectory named tensorflow. After cloning, you may optionally build a specific branch (such as a release branch) by invoking the following commands:

C:\> cd tensorflow
C:\> git checkout Branch # where Branch is the desired branch

For example, to work with the r1.11 release instead of the master release, issue the following command:

C:\> git checkout r1.11

Next, you must now configure the installation.

Configure the installation

The root of the source tree contains a python script named configure.py. This script asks you to identify the pathname of all relevant TensorFlow dependencies and specify other build configuration options such as compiler flags. You must run this script prior to creating the pip package and installing TensorFlow.

If you wish to build TensorFlow with GPU, configure.py will ask you to specify the version numbers of CUDA and cuDNN. If several versions of CUDA or cuDNN are installed on your system, explicitly select the desired version instead of relying on the default.

One of the questions that configure.py will ask is as follows:

Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is /arch:AVX]:

Here is an example execution of the configure.py script. Note that your own input will likely differ from our sample input:

C:\> cd tensorflow  # cd to the top-level directory created
C:\tensorflow> python ./configure.py
Starting local Bazel server and connecting to it...
................
You have bazel 0.15.0 installed.
Please specify the location of python. [Default is C:\python36\python.exe]: 

Found possible Python library paths:
  C:\python36\lib\site-packages
Please input the desired Python library path to use.  Default is [C:\python36\lib\site-packages]

Do you wish to build TensorFlow with CUDA support? [y/N]: Y
CUDA support will be enabled for TensorFlow.

Please specify the CUDA SDK version you want to use. [Leave empty to default to CUDA 9.0]:

Please specify the location where CUDA 9.0 toolkit is installed. Refer to README.md for more details. [Default is C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v9.0]:

Please specify the cuDNN version you want to use. [Leave empty to default to cuDNN 7.0]: 7.0

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.0]: C:\tools\cuda

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]: 3.7

Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is /arch:AVX]: 

Would you like to override eigen strong inline for some C++ compilation to reduce the compilation time? [Y/n]:
Eigen strong inline overridden.

Configuration finished

Build the pip package

CPU-only support

To build a pip package for TensorFlow with CPU-only support:

C:\tensorflow> bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package

GPU support

To build a pip package for TensorFlow with GPU support:

C:\tensorflow> bazel build --config=opt --config=cuda //tensorflow/tools/pip_package:build_pip_package

NOTE : When building with GPU support, you might want to add --copt=-nvcc_options=disable-warnings to suppress nvcc warning messages.

The bazel build command builds a binary named build_pip_package (an executable binary to launch bash and run a bash script to create the pip package). Running this binary as follows will build a .whl file within the C:/tmp/tensorflow_pkg directory:

C:\tensorflow> bazel-bin\tensorflow\tools\pip_package\build_pip_package C:/tmp/tensorflow_pkg

Install the pip package

Invoke pip3 install to install that pip package. The filename of the .whl file depends on the TensorFlow version and your platform. For example, the following command will install the pip package for TensorFlow 1.11.0rc0:

C:\tensorflow> pip3 install C:/tmp/tensorflow_pkg/tensorflow-1.11.0rc0-cp36-cp36m-win_amd64.whl

Validate your installation

Validate your TensorFlow installation by doing the following:

Start a terminal.

Change directory (cd) to any directory on your system other than the tensorflow subdirectory from which you invoked the configure command.

Invoke python:

$ python

Enter the following short program inside the python interactive shell:

# 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!

To learn more, see the TensorFlow tutorials.

Build under MSYS shell

The above instruction assumes you are building under the Windows native command line (cmd.exe), but you can also build TensorFlow from MSYS shell. There are a few things to notice:

  • Disable the path conversion heuristic in MSYS. MSYS automatically converts arguments that look like a Unix path to Windows path when running a program, this will confuse Bazel. (eg. A Bazel label //foo/bar:bin is considered a Unix absolute path, only because it starts with a slash)
$ export MSYS_NO_PATHCONV=1
$ export MSYS2_ARG_CONV_EXCL="*"
  • Add the directory where you install Bazel in $PATH. Assume you have Bazel installed at C:\tools\bazel.exe, issue the following command:
# `:` is used as path separator, so we have to convert the path to Unix style.
$ export PATH="/c/tools:$PATH"
  • Add the directory where you install Python in $PATH. Assume you have Python installed at C:\Python36\python.exe, issue the following command:
$ export PATH="/c/Python36:$PATH"
  • If you have Python in $PATH, you can run configure script just by ./configure, a shell script will help you invoke python.

  • (For GPU build only) Add Cuda and cuDNN bin directories in $PATH in the following way:

$ export PATH="/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v9.0/bin:$PATH"
$ export PATH="/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v9.0/extras/CUPTI/libx64:$PATH"
$ export PATH="/c/tools/cuda/bin:$PATH"

The rest steps should be the same as building under cmd.exe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment