Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

How to build and install TensorFlow

📅 2019-Apr-25 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ tensorflow ⬩ 📚 Archive

TensorFlow (TF) can be built from source easily and installed as a Python wheel package. I used the following steps to build it using Python3 and with support for CUDA and TensorRT:

$ sudo apt install python3-dev python3-pip
$ pip3 install -U --user six numpy wheel setuptools mock
$ pip3 install -U --user keras_applications==1.0.6 --no-deps
$ pip3 install -U --user keras_preprocessing==1.0.5 --no-deps

These packages are installed to your ~/.local/lib/python3.x/site-packages directory. TF documentation also installs the latest pip3 from PyPI. However, doing that causes the infamous "Cannot import name main" error, so I do not do that.

$ cd tensorflow_root
$ git clone git@github.com:tensorflow/tensorflow.git
$ cd tensorflow
$ ./configure

Some of the questions it asks and my replies:

$ bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package

The first build can take 2-4 hours to complete.

$ ./bazel-bin/tensorflow/tools/pip_package/build_pip_package /path_to_tensorflow_root/tensorflow_pkg

I found that it had generated a Python wheel file named: tensorflow-1.13.1-cp36-cp36m-linux_x86_64.whl

$ pip3 uninstall tensorflow tensorflow-estimator
$ pip3 install -U --user tensorflow-1.13.1-cp36-cp36m-linux_x86_64.whl
$ python3 -c "import tensorflow"

Tried with: Tensorflow 1.13.1 and Ubuntu 18.04