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

Sadanand Modak
Analytics Vidhya
Published in
8 min readJul 10, 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 first post in the series of two posts describing how to build TensorFlow from source on Windows. If you have already checked out this post, you can directly go to the next post here.

This being my very first article on medium, I am pretty enthusiastic to share my own experience of building and installing TensorFlow version 1.14.0 on Windows 10 from source code. In this article (consisting of a series of two posts), I will try to ensure that all bits-and-pieces of the complete process are included (because I struggled quite a lot in solving small issues here and there!) so that you can go through the entire process smoothly.

First things first, let's discuss in brief why do we need to build TensorFlow from source when we can simply pip-install it.

Why build from source in the first place?

Just as a side-note, I found out about this “build TensorFlow from source” thingy when I was using the pip-installed TensorFlow v1.14.0 and during the execution of some program, the following message showed up-

Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2

After this, I dug deeper and found out about this “build from source” method.

So basically, there are two standard ways to install any python package (such as TensorFlow). The first one is the most basic one and it is the one which is generally used which is by using the pip command. This command uses the pre-built binary wheel to install the package. This binary is built to be compatible widely across different systems and hence doesn’t use the CPU extensions as these extensions’ compatibility may vary from CPU to CPU. The CPU extensions such as SSE4.1, SSE4.2, AVX, AVX2, FMA, etc. are very useful and significantly speed-up linear algebra computations. Thus, essentially the second method exploits the usage of these CPU extensions as we ourselves build our own binary wheel file (specific to our system such that it uses the CPU extensions supported by our system) and then install the package using this “own-made” binary, and this is what is called the build from source method which we will cover in this article.

A more detailed discussion on the above paragraph can be found here.

This article deals with:

  • Building Tensorflow version 1.14.0
  • on Windows 10 x64-based processor
  • using Python 3.7
  • configured for CPU only setup (this article doesn’t involve building TensorFlow for GPU)

However, the process is generic and can be easily extended to other systems as well. Still, one has to keep in mind the specific requirements (given here) for building any TensorFlow version, for instance, to build TensorFlow v1.14.0 which we’ll be doing in this article, Python 3.8 is NOT supported.

Please note that it is assumed that one already has Python and pip installed. If not, then simply install the required python version from here. The installation is very easy. Also, pip comes pre-installed with python.

IMPORTANT: After python and pip installation, add the following paths to the environment system variables (to know more on how to do that, click here):

C:\Program Files\Python37
C:\Program Files\Python37\Scripts

Overview of Steps Involved

This article has been divided into a series of two posts. The first post describes in great detail all the pre-requisites required and how to install them, and then the second post describes the actual “build and install” process. All in all, this provides a one-shot complete recipe to successfully build TensorFlow from source code.

Post 1: Pre-Requisites

  1. Uninstall any existing TensorFlow installations
  2. Install Git for Windows
  3. Install MSYS2 with its command-line tools
  4. Install Visual Studio Community 2017 along with Visual C++ Build Tools 2015
  5. Install Bazel
  6. Install required Python packages

Post 2: Build and Install TensorFlow (link to it)

  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: Uninstall any existing TensorFlow installations

Click on the start menu and type “Run” and press enter. Then type “cmd” in the dialog box to open the Windows Command Prompt. Now execute the following commands:

pip uninstall tensorflow
pip uninstall tensorflow-estimator
pip uninstall tensorboard

Note: Sometimes using just pip may give the following message:

WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip.

So if this warning appears, for all the commands in this and the next post, you may simply use python -m pip in place of using pip, if you want to avoid this warning!

After this, go to the Python directory C:\Users\<Username>\AppData\Roaming\Python\Python37\site-packages and search for any folders with the names “tensorflow”, “tensorflow-estimator”, or “tensorboard” and delete if any such folders are found. This is necessary for a clean uninstall.

You may find folders like “tensorflow_estimator-2.2.0.dist-info” etc. still remaining even after using pip uninstall . So simply manually delete these.

STEP CHECK: Open the Command Prompt and execute the following commands:

pip show tensorflow
pip show tensorflow-estimator
pip show tensorboard

EXPECTED RESULT: If the uninstallation is successful, then you must get “WARNING: Package not found” for each of the above three statements.

Step 2: Install Git for Windows

To download Git, click here and download the latest available git version for windows. Then run the executable setup file, and simply click “Next” until the setup finishes. Add the following paths to the environment system variables (to know more on how to do that, click here):

C:\Program Files\Git
C:\Program Files\Git\bin

STEP CHECK: Open the Command Prompt and execute the command git --version .

EXPECTED RESULT: You should get a message “git version <version>”.

Step 3: Install MSYS2 with its command-line tools

Download the MSYS2 installer file msys2-x86_64-<version>.exe from here. Run the installer.

Note: I would recommend NOT changing the default installation folder which is C:\msys64 as even a slight mistake in the naming convention (such as spaces in the folder name) can lead to catastrophic failures later!

Keep clicking “Next” until the setup finishes. After it finishes, add the following paths to the environment system variables (to know more on how to do that, click here):

C:\msys64
C:\msys64\usr\bin

Now, click on the start menu, and search “MSYS2 MSYS” and open the msys2 shell. Now execute the following commands in order:

You may need to close and reopen the msys2 shell after each of the below commands (if you see a warning telling you to restart the shell).

pacman -Syu
pacman -Su
pacman -S git patch unzip

After executing above commands, close the “MSYS2 MSYS” shell.

STEP CHECK: If executing the above commands produces no error, and all required packages (if any) are installed successfully, then you are good to go ahead!

Step 4: Install Visual Studio Community 2017 along with Visual C++ Build Tools 2015

Go to this link. Then under the “Older downloads” section, click on the “download” button for 2017. You may be required to login to your Microsoft account. Now, search for “Visual Studio Community 2017". Install the executable for Visual Studio Community 2017 (version 15.9). Run the installer.

Again, I recommend NOT changing the default installation folder which is C:\Program Files (x86)\Microsoft Visual Studio 14.0 if you want to avoid future problems!

Installation Menu

When the Installation Menu (as shown above) shows up, make sure you select the following options:

  • Desktop development with C++ (see left part of above image)
  • Windows 10 SDK latest version (in this case it is 10.0.17763.0) (see right part of above image)
  • VC++ 2015.3 v14.00 (v140) toolset for desktop (see bottom-right part of above image)

Then click on the Install button and wait for it to finish.

STEP CHECK: Go to C:\Program Files (x86) and if you see a folder named “Microsoft Visual Studio 14.0”, then you are good to go ahead!

Step 5: Install Bazel

First, check the Bazel version required for the TensorFlow version you are trying to build from here and then download your specific Bazel version from here.

For the purposes of this post, to build TensorFlow v1.14.0 using python 3.7, we will be downloading Bazel version 0.25.2. Click on this link and the download of the executable binary file will start automatically. Rename the binary file from “bazel-0.25.2-windows-x86_64.exe” to “bazel.exe”. Now move this binary file to any folder of your choice, say we move it to C:\Program Files , and then add C:\Program Files (or the path to the folder you have chosen) to environment system variables.

Tip: Move the Bazel binary exe file to a folder in that drive (C, D, E, etc) which has at least 8–9 GB of free disk space in it. You will see in part 2 that we require around 8–9 GB of disk space for building TensorFlow package using Bazel.

Now we need to create 3 new environment system variables. Open the environment system variables dialog box as described here, and click on the new button under “System Variables”, and create each of the following new variables:

STEP CHECK: Open the Command Prompt and execute the command bazel version .

EXPECTED RESULT: You should get a message displaying information about Bazel such as the version, build target and timestamp, etc.

Step 6: Install required Python packages

Finally, at the end of all the pre-requisites required to build TensorFlow from the source code are the necessary Python packages.

A complete list of it can be found here in the official TensorFlow repository.

To install all these packages, I am providing a GitHub Gist link here, after clicking on which a zip file will be download. Unzip it. Clicking on the unzipped folder, you will find a “requirements.txt” file in it. Copy the “requirements.txt” and place it in any folder of your choice (such as the Desktop). Now, simply open your command prompt and execute the following commands:

Note: The /d flag below just lets you change drive if at all you are in a different drive currently.

cd /d C:\Users\<Username>\Desktop
pip install -r requirements.txt --no-deps

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 each and every package listed in “requirements.txt” also present in the output of the command pip list with the required version.

If you have reached here passing successfully all the above mentioned step checks, then congrats! You have now successfully installed all pre-requisites to build TensorFlow from source.

Hope you liked this post! 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.

And don’t forget to check out the next (and the last) post in this series of two posts. The link to the next post is here.

--

--

Sadanand Modak
Analytics Vidhya

Student at Indian Institute of Technology Delhi, India