Build and Install TensorFlow for CPU from source code on Windows (Part 2)

Sadanand Modak
Analytics Vidhya
Published in
7 min readJul 11, 2020

Have you been struggling for quite some-time to build TensorFlow from source code on Windows 10? Unable to find a one-shot complete guide on how to do so? Then this article is just for you!

This is the second (and the last) post in the series of two posts for building TensorFlow for CPU from source code on Windows. Kindly check out the first post here if you haven’t already before proceeding any further.

Overview of Steps Involved

In this post, we will perform the actual “build and install” of TensorFlow as we have already installed all the required pre-requisites in part 1.

  1. Clone the official TensorFlow repo having source code
  2. Configure the build parameters using Bazel
  3. Build TensorFlow package using the configured parameters
  4. Create binary wheel file for TensorFlow installation using pip
  5. Pip install TensorFlow package using the created binary wheel file
  6. (Optional) Test the TensorFlow installation

The official site (here) covers some of these steps, but I found it a bit less understandable for any beginner!

After every step, I will provide a STEP CHECK which will help you to ensure that the step has been properly completed or not. This will help you debug if anything goes wrong!

Step 1: Clone the official TensorFlow repo having source code

We need to get the TensorFlow source code. For this, first, choose a directory where you want to clone the entire TensorFlow repository (~700 MB), for instance, I chose E:\ . Now open the command prompt and execute the following commands:

# Change E:\ below to your chosen folder path.
cd /d E:\
mkdir build-tensorflow-from-source
cd build-tensorflow-from-source
# Now we are in E:\build-tensorflow-from-source
git clone https://github.com/tensorflow/tensorflow.git
# This will create a folder named "tensorflow"

Now, we need to enter the “tensorflow” folder. Now here comes the tricky part, which was missing in most of the online resources I found on the web! By default, we are in the master branch after entering the “tensorflow” folder. As we are building TensorFlow v1.14.0, we need to checkout to the specific tag “v1.14.0" of our target version (think of a tag as an alias for a particular commit in the entire commit history), so as to have all the files in our working directory in accordance with our target version.

All tags corresponding to different available TensorFlow versions can be viewed here.

Execute the commands below in command prompt:

# To ensure you are in the correct directory
cd /d E:\build-tensorflow-from-source\tensorflow
git checkout v1.14.0
# Now the HEAD shifts to this tag and gets detached from master branch

Now if you make any changes in the working directory, those will be temporary and will vanish if you checkout to any other branch/tag, ie, you cannot store changes by making commits to a tag. However, for the purposes of this post, we don’t need to modify anything to this tag, and we will simply use this tag to build our own custom binary wheel file.

Still if at all you want to store changes that you want to make in the working directory after checking out to the v1.14.0 tag, before making those changes just create a branch with its HEAD at that tag using the command git checkout -b v1.14.0-branch v1.14.0 , and then you can simply keep committing all your changes to that branch as you normally do! To know more about this, click here.

To know more about tags vs branches in git, check this link.

STEP CHECK: Open the command prompt and execute:

cd /d E:\build-tensorflow-from-source\tensorflow
git branch

EXPECTED OUTPUT: If you get the below output then you proceed forward!

Step 2: Configure the build parameters using Bazel

Here we will be specifying our system-specific parameters (including CPU instructions like AVX, AVX2, etc) using which our custom binary wheel would be built. Execute the following commands in the command prompt:

cd /d E:\build-tensorflow-from-source\tensorflow
python ./configure.py

Now you should see the below message:

...
You have bazel 0.25.2 installed.

After that, you should see a number of questions related to python.exe path, compiler flags, etc. Answer all those questions according to your specific system. However, if your system, python version, and the TensorFlow version that you are trying to build exactly match with what I am using in this article, then you will be asked the following questions, the answers to which I am providing below (questions are in Italic, and my answers are in Bold). Press Enter if you want to accept the default answer.

To see my system specifications and python version etc. refer to the “This article deals with:” section of part 1 of this series of posts.

Please specify the location of python. [Default is C:\python37\python.exe]: C:\Program Files\Python37\python.exeFound possible Python library paths:
C:\Program Files\Python37\lib\site-packages
Please input the desired Python library path to use. Default is [C:\Program Files\Python37\lib\site-packages]
<Press Enter>
Do you wish to build TensorFlow with CUDA support? [y/N]: NDo you wish to build TensorFlow with ROCm support? [y/N]: NDo you wish to build TensorFlow with XLA JIT support? [y/N]: NPlease specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is /arch:AVX]: /arch:AVX2Would you like to override eigen strong inline for some C++ compilation to reduce the compilation time? [Y/n]: Y

STEP CHECK: After answering all the above questions properly, you should see a message displaying different values you can use with the --config flag. If you see such a message then you are good to go ahead!

Step 3: Build TensorFlow package using the configured parameters

Now, we will use the bazel build command to create the creator of the binary wheel file, ie, using the bazel build command we will create build_pip_package executable, using which (in the next step) we will build the binary wheel file. Two important notes of caution are:

  • Ensure that the drive (C, D, E, etc.) where the “bazel.exe” is stored has at least 8–9 GB of free space.
  • The bazel build takes quite some time (in my case it took ~2.5 hours). So I would recommend disabling your windows defender and any other anti-virus software to have an uninterrupted package build.

Execute the following commands in your command prompt:

cd /d E:\build-tensorflow-from-source\tensorflow
bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package

STEP CHECK: If you followed all the above instructions correctly, most likely after the build is complete you should get a message conveying successful completion of the bazel build .

However, if you get some error in between the whole process, you can try the following solutions:

  • Use --define=no_tensorflow_py_deps=true flag in the above bazel build statement (refer this link to know more)
  • Use --local_ram_resources=2048 flag in the above bazel build statement (refer this link to know more)
  • Use --incompatible_bzl_disallow_load_after_statement=false flag in the above bazel build statement (refer this link to know more)
  • Use --cxxopt=-std=c++11 flag in the above bazel build statement (refer this link to know more)

Step 4: Create binary wheel file for TensorFlow installation using pip

We will now build the binary wheel from the build_pip_package executable that we have built in the previous step. The executable can be found at E:\build-tensorflow-from-source\tensorflow\bazel-bin\tensorflow\tools\pip_package . Now, simply open the command prompt and execute the following:

cd /d E:\build-tensorflow-from-source\tensorflow
mkdir ..\out
# Run the build_pip_package executable
bazel-bin\tensorflow\tools\pip_package\build_pip_package ..\out

STEP CHECK: Go to E:\build-tensorflow-from-source\out . If you see a binary wheel file “tensorflow-1.14.0-cp37-cp37m-win_amd64.whl”, then you are good to go to the next step!

Step 5: Pip install TensorFlow package using the created binary wheel file

Finally! After such a long and tedious process, we will now pip-install TensorFlow v1.14.0 using the custom binary wheel file built in the previous step. Open the command prompt and execute the following command:

cd /d E:\build-tensorflow-from-source\out
pip install tensorflow-1.14.0-cp37-cp37m-win_amd64.whl

STEP CHECK: Open the Command Prompt and execute the command pip list . It will show you a list of all the packages that you have installed.

EXPECTED OUTPUT: You should see v1.14.0 of tensorflow, tensorflow-estimator, and tensorboard listed in the output of the command pip list .

Step 6: (Optional) Test the TensorFlow installation

Now comes the moment of truth! Let's see if all your hard work has successfully paid-off or not. Click on this link, and zip would be downloaded. Unzip that folder. Open that folder and copy the “test.py” script into any folder of your choice (say for instance Desktop). Now open the Command Prompt and execute the following commands:

cd /d C:\Users\<Username>\Desktop
python test.py

EXPECTED OUTPUT:

If you see the above output, then Congratulations! You have now successfully built-from-source TensorFlow version 1.14.0 on Windows, equipped with very useful CPU instruction set like the AVX, AVX2, etc. which will make your computations a lot faster!

I enjoyed writing this article (my very first one!) a lot. Hope you liked the post as well! Do let me know in the comments section below if the process described here worked for you, or if you faced any issues when following the steps mentioned in this post.

--

--

Sadanand Modak
Analytics Vidhya

Student at Indian Institute of Technology Delhi, India