The official instructions on building TensorFlow are here: https://www.tensorflow.org/install/install_sources
We are assuming a build with CUDA support, as well as including SIMD optimizations (SSE3, SSE4, AVX, AVX2, FMA), on a Debian-like system (e.g. Ubuntu Linux).
On new systems, one will have to install CUDA, CuDNN, plus the following dependencies:
$ sudo apt-get install python3-numpy python3-dev python3-pip python3-wheel libcupti-dev
(Leave out libcupti-dev when not building with GPU support.)
Good to know: The compute capabilities for
- Maxwell TITAN X:
5.2 - Pascal TITAN X (2016):
6.1 - GeForce GTX 1080 Ti:
6.1
(See here for the full list.)
Bazel is Google's own build system, required to build TensorFlow. Building TensorFlow usually requires an up-to-date version of Bazel; there is a good chance that whatever your package manager provides will be outdated.
There are various ways to obtain a build of Bazel (see https://bazel.build/versions/master/docs/install-ubuntu.html).
Recommended by Google, but you need to be comfortable adding another APT package souce.
$ echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
$ curl https://bazel.build/bazel-release.pub.gpg | sudo apt-key add -
$ sudo apt-get update && sudo apt-get install bazel
In case you prefer building from source, it's unfortunately not as easy as cloning the Git repository and typing make.
Recent versions of Bazel can only be built with Bazel, unless one downloads a distribution source build, which contains
some already pre-generated files. With one such installation in place, one could build Bazel straight from the
repository source, but that's probably not necessary.
So we will go with building a distribution build, which is reasonably straightforward:
-
Download a distribution package from the releases page. The current version at the time of writing was 0.9.0.
$ mkdir bazel && cd bazel $ wget https://github.com/bazelbuild/bazel/releases/download/0.9.0/bazel-0.9.0-dist.zip -
Unzip the sources. This being a zip file, the files are stored without containing folder. Glad we already put it in its own directory...
$ unzip bazel-0.9.0-dist.zip -
Compile Bazel
$ bash ./compile.sh -
The output executable is now located in
output/bazel. Add aPATHentry to your.bashrc, or just export it in your current shell:$ export PATH=`pwd`/output:$PATH
You should now be able to call the bazel executable from anywhere on your filesystem.
-
Create a Python 3 virtualenv, if you have not done this yet. For example:
$ virtualenv -p python3 --system-site-packages ~/.virtualenvs/tf_dev -
Activate your respective Python 3 based virtual environment.
$ source ~/.virtualenvs/tf_dev/bin/activate-
This can later be deactivated with
$ deactivate
-
-
Clone the sources, and check out the desired branch. At the time of writing, 1.5.0 was the latest version; adjust if necessary.
$ git clone https://github.com/tensorflow/tensorflow $ cd tensorflow $ git checkout v1.5.0 -
Run the configuration script
$ ./configureYou can leave most defaults, but do specify the following (or similar):
CUDA support -> Y CUDA compute capability -> 5.2,6.1 -
Compile TensorFlow using Bazel.
This command will build Tensorflow using optimized settings for the current machine architecture.
$ bazel build --config=opt --config=cuda //tensorflow/tools/pip_package:build_pip_package-
Leave out
--config=cudawhen not building with GPU support. -
Due to this issue, you might need to add the argument
--incompatible_load_argument_is_label=falseif you see an error similar toname 'sycl_library_path' is not defined. -
TensorFlow 1.5: As mentioned here, you might have to add
--action_env=LD_LIBRARY_PATH=${LD_LIBRARY_PATH}to the call to Bazel, in case you encounter linking errors related to CUDA/cuDNN. -
Add
-c dbg --strip=neverin case you do not want debug symbols to be stripped (e.g. for debugging purposes). Usually, you won't need to add this option. -
Add
--compilation_mode=dbgto build in debug instead of release mode, i.e. without optimizations. You shouldn't do this unless you really want to.
-
-
We still need to build a Python package using the now generated build_pip_package script.
$ bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg -
Now there will be a package inside /tmp/tensorflow_pkg, which can be installed with pip.
$ pip install /tmp/tensorflow_pkg/tensorflow-1.5.0-cp35-cp35m-linux_x86_64.whl(Adjust the file name, if necessary.)
That should be it!
-
Additional steps may be necessary when building TensorFlow on an officially unsupported system (e.g. on a non-LTS Ubuntu version). For example, Ubuntu versions >16.04 ship with GCC 6 as default compiler, but CUDA 8 (still) requires a GCC compiler from the GCC 5.x series. In this case:
-
Build GCC 5.x from source and install, preferably user-local. (https://github.com/kmhofmann/build_stuff provides a script to build various versions of GCC from source.)
Add the respective paths to the
PATHandLD_LIBRARY_PATHvariables, e.g.:$ export PATH=$HOME/local/bin:$PATH $ export LD_LIBRARY_PATH=$HOME/local/lib64:$LD_LIBRARY_PATHEnsure that
gcc --versionis the desired version. -
Proceed with building, as described below.
-