This gist is inspired by Building TensorFlow from source (TF 2.3.0, Ubuntu 20.04).
My situation is a little different because I run Centos 8 (2004) and keep up with the current releases of CUDA, cuDNN and NCCL which the PIP version of TensorFlow does not (yet) support.
I have the following configuration:
- Centos 8 (2004)
- Current releases of CUDA, cuDNN and NCCL
- Bazel 3.1.0
You will need to install Bazel 3.1.0 to run the build, this is the recommended version and I have not tested other versions. The easiest way I have found to do this is to download and run the installer script (bazel-3.1.0-installer-linux-x86_64.sh).
This is a little involved and addressed in a separate gist.
While I use the current releases of CUDA, cuDNN and NCCL, there is no reason why previous versions could be used, though I have not tested this.
To keep things clean I like build in a virtual environment and then install the resulting package in a separate virtual environment.
Create the virtual environment:
python3 -m venv --system-site-packages ~/build_venv
Activate the virtual environment:
source ~/build_venv/bin/activate
Install Python packages in the virtual environment:
pip install --upgrade pip setuptools
pip install --upgrade six numpy wheel setuptools mock future portpicker
pip install --upgrade keras_applications --no-deps
pip install --upgrade keras_preprocessing --no-deps
Get TensorFlow source from GitHub:
git clone https://github.com/tensorflow/tensorflow
cd tensorflow
git checkout master
There are some bugs in the TensorFlow source which cause the build to fail because some scripts look for the Python interpreter in the wrong place, and you will see this kind of error:
ERROR: /home/user/tensorflow-2.4.0/tensorflow/python/BUILD:218:1: Executing genrule //tensorflow/python:py_build_info_gen failed (Exit 127)
/usr/bin/env: 'python': No such file or directory
The way I get around this is to add this to .bashrc:
export PATH=$PATH:/home/user/build_venv/bin
Configure TensorFlow:
./configure
You will need to set the Python path:
Please specify the location of python. [Default is /usr/bin/python3]: /home/user/build_venv/bin/python3
And the Python library path:
Found possible Python library paths:
/usr/lib64/python3.6/site-packages
/home/user~/build_venv/lib64/python3.6/site-packages
/usr/local/lib64/python3.6/site-packages
/usr/local/lib/python3.6/site-packages
/home/user/build_venv/lib/python3.6/site-packages
/usr/lib/python3.6/site-packages
Please input the desired Python library path to use. Default is [/usr/lib64/python3.6/site-packages]
/home/user/build_venv/lib64/python3.6/site-packages
Clean Bazel cache before build:
bazel clean --expunge
And so the build, this will take a long time:
bazel build --config=opt --verbose_failures -c opt //tensorflow/tools/pip_package:build_pip_package
Once done you can build the Python package:
./bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
The Python package is created in /tmp/tensorflow_pkg:
linux: ~ > ls /tmp/tensorflow_pkg/
tensorflow-2.4.0-cp36-cp36m-linux_x86_64.whl
Clean Bazel cache after build:
bazel clean --expunge
You can install the Python package:
pip install --upgrade /tmp/tensorflow_pkg/tensorflow-2.4.0-cp36-cp36m-linux_x86_64.whl
And test it with:
python -c "import tensorflow as tf;print(tf.reduce_sum(tf.random.normal([1000, 1000])))"
We are done, so we can clean up, :
# Deactivate the virtual environment
deactivate
# Delete the virtual environment
rm -rf ~/build_venv
# Delete the TensorFlow source
rm -rf ~/tensorflow
Finally remember to remove this from .bashrc
export PATH=$PATH:/home/user/build_venv/bin
Create the virtual environment:
python3 -m venv --system-site-packages ~/tf_source_venv
Activate the virtual environment:
source ~/tf_source_venv/bin/activate
Install pip and setuptools:
pip install --upgrade pip setuptools
Install the Python package:
pip install /tmp/tensorflow_pkg/tensorflow-2.4.0-cp36-cp36m-linux_x86_64.whl
Test the TensorFlow installation
python -c "import tensorflow as tf;print(tf.reduce_sum(tf.random.normal([1000, 1000])))"