Download and Install

You can install TensorFlow using any of the Pip, Docker, Virtualenv, Anaconda or source compilation methods we provide.

Pip install

The list of

First install PIP (or Python3’s PIp3):

# Ubuntu/Linux 64-bit
$ sudo apt-get install python-pip python-dev

# Mac OS X
$ sudo easy_install pip
Copy the code

Install TensorFlow:

# Ubuntu/Linux 64-bit, CPU only, Python 2.7:
$ sudo pip install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.8.0-cp27-none-linux_x86_64.whl

# Ubuntu/Linux 64-bit, GPU enabled, Python 2.7. Requires CUDA toolkit 7.5 and CuDNN v4.
# For other versions, see "Install from sources" below.
$ sudo pip install --upgrade https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.8.0-cp27-none-linux_x86_64.whl

# Mac OS X, CPU only:
$ sudo easy_install --upgrade six
$ sudo pip install --upgrade https://storage.googleapis.com/tensorflow/mac/tensorflow-0.8.0-py2-none-any.whl
Copy the code

If Python3:

# Ubuntu/Linux 64-bit, CPU only, Python 3.4:
$ sudo pip3 install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.8.0-cp34-cp34m-linux_x86_64.whl

# Ubuntu/Linux 64-bit, GPU enabled, Python 3.4. Requires CUDA toolkit 7.5 and CuDNN v4.
# For other versions, see "Install from sources" below.
$ sudo pip3 install --upgrade https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.8.0-cp34-cp34m-linux_x86_64.whl

# Mac OS X, CPU only:
$ sudo easy_install --upgrade six
$ sudo pip3 install --upgrade https://storage.googleapis.com/tensorflow/mac/tensorflow-0.8.0-py3-none-any.whl
Copy the code

Note: If you have previously installed TensorFlow < 0.7.1, you should uninstall both TensorFlow and Protobuf using PIP uninstall to ensure that the installation package is the latest protobuf dependency.

Docker can then run TensorFlow. The advantage of this approach is that you don’t have to worry about software dependencies.

First, Virtualenv creates an isolated container to install TensorFlow. This is optional, but it makes it easier to troubleshoot installation problems.

First, install all the necessary tools:

# on Linux:
$ sudo apt-get install python-pip python-dev python-virtualenv

# on Mac:
$ sudo easy_install pip  # If PIP has not been installed
$ sudo pip install --upgrade virtualenv
Copy the code

Next, create a brand new VirtualEnv environment. To set up the environment in the ~/tensorflow directory, execute:

$ virtualenv --system-site-packages ~/tensorflow
$ cd ~/tensorflow
Copy the code

Then, activate Virtualenv:

$ source bin/activate  # if bash is used
$ source bin/activate.csh  # if CSH is used
(tensorflow)$  The terminal prompt should change
Copy the code

Within VirtualEnv, install TensorFlow:

(tensorflow)$ pip install --upgrade <$url_to_binary.whl>
Copy the code

Next, run the TensorFlow program with a similar command:

(tensorflow)$ cd tensorflow/models/image/mnist
(tensorflow)$ python convolutional.py

# When using TensorFlow
(tensorflow)$ deactivate  # stop using virtualenv

$  # Your command prompt will be restored
Copy the code

Anaconda based installation

Computing environment, similar to Virtualenv.

Like Virtualenv, Conda stores dependency packages in different places for different Python projects. Anaconda installed on TensorFlow does not overwrite previously installed Python packages.

  • Installation Anaconda
  • Set up a Conda computing environment
  • Activate the environment and install TensorFlow using Conda
  • After TensorFlow is installed, you need to activate the Conda environment every time you use TensorFlow

Installation Anaconda:

See Anaconda’s download page for tests

When you are not using TensorFlow, close the environment:

(tensorflow)$ source deactivate

$  # Your prompt should change back
Copy the code

Reactivate when used again 🙂

$ source activate tensorflow
(tensorflow)$  # Your prompt should change.
# Run Python programs that use TensorFlow..# When you are done using TensorFlow, deactivate the environment.
(tensorflow)$ source deactivate
Copy the code

Try your first TensorFlow program

(Optional) Enable GPU support

If you install TensorFlow with GPU support enabled using the PIP binary, you must ensure that the correct CUDA SDK and CUDNN versions are installed on your system. Please visit the CUDA installation tutorial

You also need to set the LD_LIBRARY_PATH and CUDA_HOME environment variables. Consider adding the following command to the ~/.bash_profile file so that it takes effect automatically after each login. Note that the following commands assume the CUDA installation directory is /usr/local/cuda:

export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/cuda/lib64"
export CUDA_HOME=/usr/local/cuda
Copy the code

Run TensorFlow

Open a Python terminal:

$ python

>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow! ')
>>> sess = tf.Session()
>>> print sess.run(hello)
Hello, TensorFlow!
>>> a = tf.constant(10)
>>> b = tf.constant(32)
>>> print sess.run(a+b)
42
>>>
Copy the code

Install from source

Clone the TensorFlow repository

$ git clone --recurse-submodules https://github.com/tensorflow/tensorflow
Copy the code

Recurse-submodules parameter is required to retrieve TesorFlow dependent Protobuf libraries.

Linux installation

Install Bazel

First follow the link to download the latest stable version for your operating system and finally follow the following script:

$ chmod +x PATH_TO_INSTALL.SH
$ ./PATH_TO_INSTALL.SH --user
Copy the code

Note that replace path_to_install. SH with the path of the installation package you downloaded.

Add the execution PATH output/bazel to the $PATH environment variable.

Install additional dependencies

# For Python 2.7:
$ sudo apt-get install python-numpy swig python-dev python-wheel
# For Python 3.x:
$ sudo apt-get install python3-numpy swig python3-dev python3-wheel
Copy the code

Optional: Install CUDA (enable GPU support on Linux)

To compile and run GPU-enabled TensorFlow, you need to install Cuda Toolkit 7.0 and CUDNN 6.5 V2 from NVIDIA.

TensorFlow’s GPU feature only supports graphics cards with NVidia Compute Capability >= 3.5. Supported graphics cards include but are not limited to:

  • NVidia Titan
  • NVidia Titan X
  • NVidia K20
  • NVidia K40
Download and install Cuda Toolkit 7.0

Download address

Decompress and copy CUDNN files to Cuda Toolkit 7.0 installation directory. If Cuda Toolkit 7.0 is installed in /usr/local/cuda, run the following command:

Tar XVZF cudnn-6.5-linux-x64-v2. TGZ sudo cp cudnn-6.5-linux-x64-v2/cudnn.h /usr/local/cuda/include
sudo cp cudnn-6.5-linux-x64-v2/libcudnn* /usr/local/cuda/lib64
Copy the code
Configure the Cuda options for TensorFlow

Execute from the root of the source tree:

$ ./configure
Do you wish to bulid TensorFlow with GPU support? [y/n] y
GPU support will be enabled for TensorFlow

Please specify the location where CUDA 7.0 toolkit is installed. Refer to
README.md for more details. [default is: /usr/local/cuda]: /usr/local/cuda

Please specify the location where CUDNN 6.5 V2 library is installed. Refer to
README.md for more details. [default is: /usr/local/cuda]: /usr/local/cuda

Setting up Cuda include
Setting up Cuda lib64
Setting up Cuda bin
Setting up Cuda nvvm
Configuration finished
Copy the code

These configurations will establish symbolic links to the system’s Cuda libraries. The above steps must be repeated every time the path of the Cuda library changes, otherwise bazel compilation command cannot be invoked.

Compile the target program and enable GPU support

Execute from the root of the source tree:

$ bazel build -c opt --config=cuda //tensorflow/cc:tutorials_example_trainer

$ bazel-bin/tensorflow/cc/tutorials_example_trainer --use_gpu
# A lot of output information. This example uses GPU iteration to compute the major eigenvalue of a 2x2 matrix.
The last few lines of output are similar to the following.000009/000005 lambda = 2.000000 x = [0.894427-0.447214] Y = [1.788854-0.894427] 000006/000001 lambda = 2.000000 x = [0.894427-0.447214] Y = [1.788854-0.894427] 000009/000009 lambda = 2.000000 x = [0.894427-0.447214] y = [1.788854 0.894427]Copy the code

Note that GPU support must be enabled with the compilation option “–config= CUDa “.

Known issues
  • Although versions of Cuda with and without Cuda support can be compiled under the same source tree, we recommend using “bazel Clean “to clean the environment when switching between the two different build configurations.
  • You must run configure before bazel compilation, otherwise the compilation will fail with an error message. In the future, we might consider including the Configure step in the build process to simplify the whole process, if Bazel can provide new features to support this.

Mac OS X installation

Mac and Linux require exactly the same software dependencies, but the installation process is very different. The following links are to help you install these dependencies on Mac OS X:

Bazel

See the Mac OS X Installation tutorial.

Note: You will need to install the installation tutorial.

Create the PIP package and install it

$ bazel build -c opt //tensorflow/tools/pip_package:build_pip_package

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

The actual name of the #.whl file depends on the platform you are using$PIP install/TMP/tensorflow_pkg/tensorflow - 0.5.0 - cp27 - none - linux_x86_64. WHLCopy the code

Train your first TensorFlow neural network model

Execute from the root of the source tree:

$ cd tensorflow/models/image/mnist
$ python convolutional.py
Succesfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.
Succesfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.
Succesfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.
Succesfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.
Extracting data/train-images-idx3-ubyte.gz
Extracting data/train-labels-idx1-ubyte.gz
Extracting data/t10k-images-idx3-ubyte.gz
Extracting data/t10k-labels-idx1-ubyte.gz
Initialized!
Epoch 0.00
Minibatch loss: 12.054, learning rate: 0.010000
Minibatch error: 90.6%
Validation error: 84.6%
Epoch 0.12
Minibatch loss: 3.285, learning rate: 0.010000
Minibatch error: 6.2%
Validation error: 7.0%... .Copy the code

Q&A

Gpu-related Issues

If the following error occurs while trying to run a TensorFlow program:

ImportError: libcudart.so7.. 0: cannot open shared object file: No such file or directory
Copy the code

Make sure you have GPU support correctly installed, refer to the relevant section.

On Linux

If an error occurs:

."__add__"."__radd__",
             ^
SyntaxError: invalid syntax
Copy the code

Solution: Ensure that the Python version you are using is Python 2.7.

On Mac OS X

If an error occurs:

import six.moves.copyreg as copyreg

ImportError: No module named copyreg
Copy the code

Solution: Protobuf used by TensorFlow relies on six-1.10.0. However, Apple’s default Python environment already has SIX-1.4.1 installed, which can be difficult to upgrade. Here are several ways to solve this problem:

  1. Upgrade the system-wide SIX:

      sudo easy_install -U six
    Copy the code
  2. Install an isolated copy of Python via Homebrew:

      brew install python
    Copy the code
  3. Compile or use TensorFlow within VirtualEnv.

If an error occurs:

>>> import tensorflow as tf Traceback (most recent call last): The File < stdin ">", line 1, the in the < module > File "/ usr/local/lib/python2.7 / site - packages/tensorflow/set py", line 4, In < module > from tensorflow. Python import * File "/ usr/local/lib/python2.7 / site - packages/tensorflow/python/set p y",  line 13, in <module> from tensorflow.core.framework.graph_pb2 import * ... The File "/ usr/local/lib/python2.7 / site - packages/tensorflow/core/framework/tensor_shape_pb2 py", line 22, in <module> serialized_pb=_b('\n,tensorflow/core/framework/tensor_shape.proto\x12\ntensorflow\"d\n\x10TensorShapeProto\x12-\n\x03\x6 4im\x18\x02 \x03(\x0b\x32 .tensorflow.TensorShapeProto.Dim\x1a! \n\x03\x44im\x12\x0c\n\x04size\x18\x01 \x01(\x03\x12\x0c\n\x04name\x18\x02 \x01(\tb\x06proto3') TypeError: __init__() got an unexpected keyword argument 'syntax'Copy the code

This is due to having a conflicting protobuf version installed, TensorFlow requires protobuf 3.0.0. Currently the best solution is to ensure that no older version of the Protobuf is installed, which can be resolved by reinstalling the protobuf using the following command:

brew reinstall --devel protobuf
Copy the code