Build from source on Windows

Build a TensorFlow pip package from source and install it on Windows.

Setup for Windows

Install the following build tools to configure your Windows development environment.

Install Python and the TensorFlow package dependencies

Install a Python 3.9+ 64-bit release for Windows. Select pip as an optional feature and add it to your %PATH% environmental variable.

Install the TensorFlow pip package dependencies:

pip3 install -U six numpy wheel packaging
pip3 install -U keras_preprocessing --no-deps

The dependencies are listed in the setup.py file under REQUIRED_PACKAGES.

Install Bazel

Install Bazel, the build tool used to compile TensorFlow. For Bazel version, see the tested build configurations for Windows. Configure Bazel to build C++.

Add the location of the Bazel executable to your %PATH% environment variable.

Install MSYS2

Install MSYS2 for the bin tools needed to build TensorFlow. If MSYS2 is installed to C:\msys64, add C:\msys64\usr\bin to your %PATH% environment variable. Then, using cmd.exe, run:

pacman -S git patch unzip

Install Visual C++ Build Tools 2019

Install the Visual C++ build tools 2019. This comes with Visual Studio 2019 but can be installed separately:

  1. Go to the Visual Studio downloads,
  2. Select Redistributables and Build Tools,
  3. Download and install:
    • Microsoft Visual C++ 2019 Redistributable
    • Microsoft Build Tools 2019

Install GPU support (optional)

See the Windows GPU support guide to install the drivers and additional software required to run TensorFlow on a GPU.

Download the TensorFlow source code

Use Git to clone the TensorFlow repository (git is installed with MSYS2):

git clone https://github.com/tensorflow/tensorflow.git
cd tensorflow

The repo defaults to the master development branch. You can also check out a release branch to build:

git checkout branch_name  # r1.9, r1.10, etc.

Optional: Environmental Variable Set Up

Run following commands before running build command to avoid issue with package creation: (If the below commands were set up while installing the packages, please ignore them). Run set check if all the paths were set correctly, run echo %Environmental Variable% e.g., echo %BAZEL_VC% to check path set up for a specific Environmental Variable

Python path set up issue tensorflow:issue#59943,tensorflow:issue#9436,tensorflow:issue#60083

set PATH=path/to/python # [e.g. (C:/Python310)]
set PATH=path/to/python/Scripts # [e.g. (C:/Python310/Scripts)] 
set PYTHON_BIN_PATH=path/to/python_virtualenv/Scripts/python.exe 
set PYTHON_LIB_PATH=path/to/python virtualenv/lib/site-packages 
set PYTHON_DIRECTORY=path/to/python_virtualenv/Scripts 

Bazel/MSVC path set up issue tensorflow:issue#54578

set BAZEL_SH=C:/msys64/usr/bin/bash.exe 
set BAZEL_VS=C:/Program Files(x86)/Microsoft Visual Studio/2019/BuildTools 
set BAZEL_VC=C:/Program Files(x86)/Microsoft Visual Studio/2019/BuildTools/VC 

Optional: Configure the build

TensorFlow builds are configured by the .bazelrc file in the respoitory's root directory. The ./configure or ./configure.py scripts can be used to adjust common settings.

If you need to change the configuration, run the ./configure script from the repository's root directory.

python ./configure.py

This script prompts you for the location of TensorFlow dependencies and asks for additional build configuration options (compiler flags, for example). The following shows a sample run of python ./configure.py (your session may differ):

Build and install the pip package

The pip package gets built in two steps. A bazel build commands creates a "package-builder" program. You then run the package-builder to create the package.

Build the package-builder

tensorflow:master repo has been updated to build 2.x by default. Install Bazel and use bazel build to create the TensorFlow package-builder.

bazel build //tensorflow/tools/pip_package:build_pip_package

CPU-only

Use bazel to make the TensorFlow package builder with CPU-only support:

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

GPU support

To make the TensorFlow package builder with GPU support:

bazel build --config=opt --config=cuda --define=no_tensorflow_py_deps=true //tensorflow/tools/pip_package:build_pip_package

Commands to clean the bazel cache to resolve errors due to invalid or outdated cached data, bazel clean with --expunge flag removes files permanently

bazel clean 
bazel clean --expunge  

Bazel build options

Use this option when building to avoid issue with package creation: tensorflow:issue#22390

--define=no_tensorflow_py_deps=true

See the Bazel command-line reference for build options.

Building TensorFlow from source can use a lot of RAM. If your system is memory-constrained, limit Bazel's RAM usage with: --local_ram_resources=2048.

If building with GPU support, add --copt=-nvcc_options=disable-warnings to suppress nvcc warning messages.

Build the package

The bazel build command creates an executable named build_pip_package—this is the program that builds the pip package. For example, the following builds a .whl package in the C:/tmp/tensorflow_pkg directory:

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

Although it is possible to build both CUDA and non-CUDA configs under the same source tree, we recommend running bazel clean when switching between these two configurations in the same source tree.

Install the package

The filename of the generated .whl file depends on the TensorFlow version and your platform. Use pip3 install to install the package, for example:

pip3 install C:/tmp/tensorflow_pkg/tensorflow-version-tags.whl

e.g., pip3 install C:/tmp/tensorflow_pkg/tensorflow-2.12.0-cp310-cp310-win_amd64.whl

Build using the MSYS shell

TensorFlow can also be built using the MSYS shell. Make the changes listed below, then follow the previous instructions for the Windows native command line (cmd.exe).

Disable MSYS path conversion

MSYS automatically converts arguments that look like Unix paths to Windows paths, and this doesn't work with bazel. (The label //path/to:bin is considered a Unix absolute path since it starts with a slash.)

export MSYS_NO_PATHCONV=1
export MSYS2_ARG_CONV_EXCL="*"

Set your PATH

Add the Bazel and Python installation directories to your $PATH environmental variable. If Bazel is installed to C:\tools\bazel.exe, and Python to C:\Python\python.exe, set your PATH with:

# Use Unix-style with ':' as separator
export PATH="/c/tools:$PATH"
export PATH="/c/path/to/Python:$PATH"

For GPU support, add the CUDA and cuDNN bin directories to your $PATH:

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

Tested build configurations

CPU

VersionPython versionCompilerBuild tools
tensorflow-2.16.13.9-3.12MSVC 2019Bazel 6.5.0
tensorflow-2.15.03.9-3.11MSVC 2019Bazel 6.1.0
tensorflow-2.14.03.9-3.11MSVC 2019Bazel 6.1.0
tensorflow-2.12.03.8-3.11MSVC 2019Bazel 5.3.0
tensorflow-2.11.03.7-3.10MSVC 2019Bazel 5.3.0
tensorflow-2.10.03.7-3.10MSVC 2019Bazel 5.1.1
tensorflow-2.9.03.7-3.10MSVC 2019Bazel 5.0.0
tensorflow-2.8.03.7-3.10MSVC 2019Bazel 4.2.1
tensorflow-2.7.03.7-3.9MSVC 2019Bazel 3.7.2
tensorflow-2.6.03.6-3.9MSVC 2019Bazel 3.7.2
tensorflow-2.5.03.6-3.9MSVC 2019Bazel 3.7.2
tensorflow-2.4.03.6-3.8MSVC 2019Bazel 3.1.0
tensorflow-2.3.03.5-3.8MSVC 2019Bazel 3.1.0
tensorflow-2.2.03.5-3.8MSVC 2019Bazel 2.0.0
tensorflow-2.1.03.5-3.7MSVC 2019Bazel 0.27.1-0.29.1
tensorflow-2.0.03.5-3.7MSVC 2017Bazel 0.26.1
tensorflow-1.15.03.5-3.7MSVC 2017Bazel 0.26.1
tensorflow-1.14.03.5-3.7MSVC 2017Bazel 0.24.1-0.25.2
tensorflow-1.13.03.5-3.7MSVC 2015 update 3Bazel 0.19.0-0.21.0
tensorflow-1.12.03.5-3.6MSVC 2015 update 3Bazel 0.15.0
tensorflow-1.11.03.5-3.6MSVC 2015 update 3Bazel 0.15.0
tensorflow-1.10.03.5-3.6MSVC 2015 update 3Cmake v3.6.3
tensorflow-1.9.03.5-3.6MSVC 2015 update 3Cmake v3.6.3
tensorflow-1.8.03.5-3.6MSVC 2015 update 3Cmake v3.6.3
tensorflow-1.7.03.5-3.6MSVC 2015 update 3Cmake v3.6.3
tensorflow-1.6.03.5-3.6MSVC 2015 update 3Cmake v3.6.3
tensorflow-1.5.03.5-3.6MSVC 2015 update 3Cmake v3.6.3
tensorflow-1.4.03.5-3.6MSVC 2015 update 3Cmake v3.6.3
tensorflow-1.3.03.5-3.6MSVC 2015 update 3Cmake v3.6.3
tensorflow-1.2.03.5-3.6MSVC 2015 update 3Cmake v3.6.3
tensorflow-1.1.03.5MSVC 2015 update 3Cmake v3.6.3
tensorflow-1.0.03.5MSVC 2015 update 3Cmake v3.6.3

GPU

VersionPython versionCompilerBuild toolscuDNNCUDA
tensorflow_gpu-2.10.03.7-3.10MSVC 2019Bazel 5.1.18.111.2
tensorflow_gpu-2.9.03.7-3.10MSVC 2019Bazel 5.0.08.111.2
tensorflow_gpu-2.8.03.7-3.10MSVC 2019Bazel 4.2.18.111.2
tensorflow_gpu-2.7.03.7-3.9MSVC 2019Bazel 3.7.28.111.2
tensorflow_gpu-2.6.03.6-3.9MSVC 2019Bazel 3.7.28.111.2
tensorflow_gpu-2.5.03.6-3.9MSVC 2019Bazel 3.7.28.111.2
tensorflow_gpu-2.4.03.6-3.8MSVC 2019Bazel 3.1.08.011.0
tensorflow_gpu-2.3.03.5-3.8MSVC 2019Bazel 3.1.07.610.1
tensorflow_gpu-2.2.03.5-3.8MSVC 2019Bazel 2.0.07.610.1
tensorflow_gpu-2.1.03.5-3.7MSVC 2019Bazel 0.27.1-0.29.17.610.1
tensorflow_gpu-2.0.03.5-3.7MSVC 2017Bazel 0.26.17.410
tensorflow_gpu-1.15.03.5-3.7MSVC 2017Bazel 0.26.17.410
tensorflow_gpu-1.14.03.5-3.7MSVC 2017Bazel 0.24.1-0.25.27.410
tensorflow_gpu-1.13.03.5-3.7MSVC 2015 update 3Bazel 0.19.0-0.21.07.410
tensorflow_gpu-1.12.03.5-3.6MSVC 2015 update 3Bazel 0.15.07.29.0
tensorflow_gpu-1.11.03.5-3.6MSVC 2015 update 3Bazel 0.15.079
tensorflow_gpu-1.10.03.5-3.6MSVC 2015 update 3Cmake v3.6.379
tensorflow_gpu-1.9.03.5-3.6MSVC 2015 update 3Cmake v3.6.379
tensorflow_gpu-1.8.03.5-3.6MSVC 2015 update 3Cmake v3.6.379
tensorflow_gpu-1.7.03.5-3.6MSVC 2015 update 3Cmake v3.6.379
tensorflow_gpu-1.6.03.5-3.6MSVC 2015 update 3Cmake v3.6.379
tensorflow_gpu-1.5.03.5-3.6MSVC 2015 update 3Cmake v3.6.379
tensorflow_gpu-1.4.03.5-3.6MSVC 2015 update 3Cmake v3.6.368
tensorflow_gpu-1.3.03.5-3.6MSVC 2015 update 3Cmake v3.6.368
tensorflow_gpu-1.2.03.5-3.6MSVC 2015 update 3Cmake v3.6.35.18
tensorflow_gpu-1.1.03.5MSVC 2015 update 3Cmake v3.6.35.18
tensorflow_gpu-1.0.03.5MSVC 2015 update 3Cmake v3.6.35.18