devtools

2 min read

The short, yet complete tutorial to help you setup Anaconda and TensorFlow on your linux dev machine. We’ll compile TensorFlow from source to benefit from any CPU extension not compiled into the usual binary distribution. This helps speed up CPU-bound TensorFlow programs substantially if your CPU supports extensions like AVX(2), FMA et al.


👋 Welcome, it's great to have you here! First time? Then click here to learn more about me, my work and this website. There are no comments, please submit feedback via e-mail.

Prerequisites

This tutorial was tested on Ubuntu 18.04, but it should work on any linux distro with minimal customization.

First, download the latest Anaconda installer into a temp directory, e.g.:

cd /tmp && curl -O https://repo.continuum.io/archive/Anaconda3-5.1.0-Linux-x86_64.sh

Note: Replace the version number if there is a newer version. Right-click-copy the link from the download page linked above.

Now run the installer. Answer yes to any license questions and click enter when asked for the install location. Changing this location might break expectations by third-party tools.

bash Anaconda3-5.1.0-Linux-x86_64.sh

If you are using bash as your primary shell, a source ~/.bashrc should give you access to the conda command. On zsh, you might want to add the conda binary path to your ~/.zshrc instead:

export PATH=$PATH:$HOME/anaconda3/bin

Test your installation using conda --version :

conda 4.4.10

Splendid. Now we’ll setup a default Python 3 environment. You can give it any name you want, I’m going with py3 :

conda create --name py3 python=3
source activate py3

Note: source deactivate will exit this environment.

Building and Installing TensorFlow

Note: This is done in the active environment.

Lets clone the TensorFlow repo and checkout the latest release:

cd tensorflow
git clone https://github.com/tensorflow/tensorflow
git checkout $(git describe --abbrev=0 --tags)

This will also print the version number we just checked out. At the time of writing, this is 1.7.

First, we need bazel (and thus JDK) and some other python dependencies:

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 -y openjdk-8-jdk python3-numpy python3-dev python3-pip python3-wheel bazel
pip install numpy

Now you have to decide whether you want to compile with CUDA support. The choice is simple: If you have an NVIDIA GPU, then you want it, otherwise you can’t have it. If the former applies to you, add --config=cuda to the bazel build command. This will compile some 6,5k source files and thus take a while.

yes "" | ./configure
bazel build --config=opt tensorflow/tools/pip_package:build_pip_package
mkdir build_pkg
bazel-bin/tensorflow/tools/pip_package/build_pip_package `pwd`/build_pkg
cd build_pkg
pip install --upgrade tensorflow-*

Note: If after following this tutorial you still get a CPU extension warning from TensorFlow (or degraded performance), try forcing the inclusion of the appropriate extensions using additional bazel options like --copt=-mavx --copt=-msse4.1 --copt=-msse4.2.

In order to verify your installation, run

python -c 'import tensorflow as tf; print(tf.__version__); sess = tf.InteractiveSession(); sess.close();'

This should print the version number and no warnings about CPU extensions.

You can now use conda to install common packages, like jupyter . Or browse Medium for posts tagged tensorflow, keras, conda etc.

Pierre

Pierre

Read more posts by this author.