This article covers building and installing TensorFlow from source, addressing three problems along the way: installing the NVIDIA-related tooling, configuring a proxy so Bazel can download build dependencies, and the configuration needed after the build finishes.

1. Installing the NVIDIA Tooling

Before building, set up the NVIDIA tooling first — see Installing the NVIDIA driver, CUDA, cuDNN, and TensorRT on Linux.

2. Configuring a Proxy

During the build, Bazel sometimes needs to fetch things from places that aren’t reachable directly:

qing_dynasty

Use a Squid proxy for this — for installing and configuring Squid, see Setting up a Squid proxy server.

Locally, set the HTTP and HTTPS proxy environment variables:

export http_proxy=http://user_name:password@server_ip:squid_port
export https_proxy=$http_proxy

3. Installing Bazel

Download Bazel from GitHub — here we use bazel-6.2.1-installer-linux-x86_64.sh:

wget https://github.com/bazelbuild/bazel/releases/download/6.2.1/bazel-6.2.1-installer-linux-x86_64.sh
chmod +x ./bazel-6.2.1-installer-linux-x86_64.sh
sudo ./bazel-6.2.1-installer-linux-x86_64.sh

After installation, check that it worked:

bazel --version
bazel 6.2.1

4. Fetching and Building TensorFlow

4.1 Fetching the source

Fetch the TensorFlow source from GitHub:

git clone https://github.com/tensorflow/tensorflow.git
cd tensorflow
git checkout v2.13.0

4.2 Running config

Run the configure script:

config

That completes the basic configuration. A temporary directory is also needed:

mkdir -p ~/tmp
export TMP=~/tmp

4.3 Editing .bazelversion

Edit .bazel_version:

6.2.1
# this is the output of `bazel --version`

4.4 Starting the build

Finally, run the build:

bazel build //tensorflow/tools/pip_package:build_pip_package

(This is a good point to go get some sleep.)

4.5 Generating and installing the wheel package

Once TensorFlow has finished building, a separate step generates the whl package:

./bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg

Find the wheel package in the /tmp/tensorflow_pkg folder above and install it with pip.

cd /tmp/tensorflow_package
python3 -m pip install ./tensorflow-2.13.0-cp39-cp39-linux_x86_64.whl

4.6 Testing the installation

Once installed, test it:

import tensorflow as tf
print(tf.test.is_built_with_cuda())
True

5. Summary

This article covered how to build and install TensorFlow from source on Red Hat.