$ sudo apt-get install python-pip python-dev python-virtualenv # for Python 2.7
$ virtualenv --system-site-packages ~/tensorflow # for Python 2.7
$ source ~/tensorflow/bin/activate # bash, sh, ksh, or zsh
提示符改变
(tensorflow)$
(tensorflow)$ easy_install -U pip
(tensorflow)$ pip install --upgrade tensorflow # for Python 2.7
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))
(tensorflow) $ python hello.py
Hello, TensorFlow!
(tensorflow)$ deactivate**
This guide explains how to install TensorFlow on Ubuntu. Although these instructions might also work on other Linux variants, we have only tested (and we only support) these instructions on machines meeting the following requirements:
You must choose one of the following types of TensorFlow to install:
If you are installing TensorFlow with GPU support using one of the mechanisms described in this guide, then the following NVIDIA software must be installed on your system:
LD_LIBRARY_PATH
environment variable as described in the NVIDIA documentation.CUDA_HOME
environment variable as described in the NVIDIA documentation.$ sudo apt-get install cuda-command-line-tools
and add its path to your `LD_LIBRARY_PATH` environment variable:
$ export LD_LIBRARY_PATH=${LD_LIBRARY_PATH:+${LD_LIBRARY_PATH}:}/usr/local/cuda/extras/CUPTI/lib64
For CUDA Toolkit <= 7.5 do:
$ sudo apt-get install libcupti-dev
* **[OPTIONAL]** For optimized inferencing performance, you can also install NVIDIA TensorRT 3.0\. For details, see [NVIDIA's TensorRT documentation](http://docs.nvidia.com/deeplearning/sdk/tensorrt-install-guide/index.html#installing-tar). Only steps 1-4 in the TensorRT Tar File installation instructions are required for compatibility with TensorFlow; the Python package installation in steps 5 and 6 can be omitted. Detailed installation instructions can be found at [package documentataion](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/tensorrt#installing-tensorrt-304)
**IMPORTANT:** For compatibility with the pre-built `tensorflow-gpu` package, please use the Ubuntu **14.04** tar file package of TensorRT even when installing onto an Ubuntu 16.04 system.
If you have an earlier version of the preceding packages, please upgrade to the specified versions. If upgrading is not possible, then you may still run TensorFlow with GPU support, but only if you do the following:
You must pick the mechanism by which you install TensorFlow. The supported choices are as follows:
We recommend the Virtualenv installation. Virtualenv is a virtual Python environment isolated from other Python development, incapable of interfering with or being affected by other Python programs on the same machine. During the Virtualenv installation process, you will install not only TensorFlow but also all the packages that TensorFlow requires. (This is actually pretty easy.) To start working with TensorFlow, you simply need to "activate" the virtual environment. All in all, Virtualenv provides a safe and reliable mechanism for installing and running TensorFlow.
Native pip installs TensorFlow directly on your system without going through any container system. We recommend the native pip install for system administrators aiming to make TensorFlow available to everyone on a multi-user system. Since a native pip installation is not walled-off in a separate container, the pip installation might interfere with other Python-based installations on your system. However, if you understand pip and your Python environment, a native pip installation often entails only a single command.
Docker completely isolates the TensorFlow installation from pre-existing packages on your machine. The Docker container contains TensorFlow and all its dependencies. Note that the Docker image can be quite large (hundreds of MBs). You might choose the Docker installation if you are incorporating TensorFlow into a larger application architecture that already uses Docker.
In Anaconda, you may use conda to create a virtual environment. However, within Anaconda, we recommend installing TensorFlow with the pip install
command, not with the conda install
command.
NOTE: The conda package is community supported, not officially supported. That is, the TensorFlow team neither tests nor maintains the conda package. Use that package at your own risk.
Take the following steps to install TensorFlow with Virtualenv:
$ sudo apt-get install python-pip python-dev python-virtualenv** # for Python 2.7
$ sudo apt-get install python3-pip python3-dev python-virtualenv** # for Python 3.n
$ virtualenv --system-site-packages** *targetDirectory* # for Python 2.7
$ virtualenv --system-site-packages -p python3** *targetDirectory* # for Python 3.n
where `*targetDirectory*` specifies the top of the Virtualenv tree. Our instructions assume that `*targetDirectory*` is `~/tensorflow`, but you may choose any directory.
$ source ~/tensorflow/bin/activate** # bash, sh, ksh, or zsh
$ source ~/tensorflow/bin/activate.csh** # csh or tcsh
The preceding **source** command should change your prompt to the following:
(tensorflow)$
(tensorflow)$ easy_install -U pip
(tensorflow)$ pip install --upgrade tensorflow** # for Python 2.7
(tensorflow)$ pip3 install --upgrade tensorflow** # for Python 3.n
(tensorflow)$ pip install --upgrade tensorflow-gpu** # for Python 2.7 and GPU
(tensorflow)$ pip3 install --upgrade tensorflow-gpu** # for Python 3.n and GPU
If the above command succeeds, skip Step 6\. If the preceding command fails, perform Step 6.
(tensorflow)$ pip install --upgrade** *tfBinaryURL* # Python 2.7
(tensorflow)$ pip3 install --upgrade** *tfBinaryURL* # Python 3.n
where `*tfBinaryURL*` identifies the URL of the TensorFlow Python package. The appropriate value of `*tfBinaryURL*`depends on the operating system, Python version, and GPU support. Find the appropriate value for `*tfBinaryURL*` for your system [here](https://tensorflow.google.cn/install/install_linux#the_url_of_the_tensorflow_python_package). For example, if you are installing TensorFlow for Linux, Python 3.4, and CPU-only support, issue the following command to install TensorFlow in the active Virtualenv environment:
(tensorflow)$ pip3 install --upgrade \
https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.7.0-cp34-cp34m-linux_x86_64.whl
If you encounter installation problems, see Common Installation Problems.
After installing TensorFlow, validate the installation.
Note that you must activate the Virtualenv environment each time you use TensorFlow. If the Virtualenv environment is not currently active, invoke one of the following commands:
$ source ~/tensorflow/bin/activate** # bash, sh, ksh, or zsh
$ source ~/tensorflow/bin/activate.csh** # csh or tcsh
When the Virtualenv environment is active, you may run TensorFlow programs from this shell. Your prompt will become the following to indicate that your tensorflow environment is active:
(tensorflow)$
When you are done using TensorFlow, you may deactivate the environment by invoking the deactivate
function as follows:
(tensorflow)$ deactivate**
The prompt will revert back to your default prompt (as defined by the PS1
environment variable).
To uninstall TensorFlow, simply remove the tree you created. For example:
$ rm -r** *targetDirectory*
You may install TensorFlow through pip, choosing between a simple installation procedure or a more complex one.
Note: The REQUIRED_PACKAGES section of setup.py lists the TensorFlow packages that pip will install or upgrade.
Python is automatically installed on Ubuntu. Take a moment to confirm (by issuing a python -V
command) that one of the following Python versions is already installed on your system:
The pip or pip3 package manager is usually installed on Ubuntu. Take a moment to confirm (by issuing a pip -V
or pip3 -V
command) that pip or pip3 is installed. We strongly recommend version 8.1 or higher of pip or pip3. If Version 8.1 or later is not installed, issue the following command, which will either install or upgrade to the latest pip version:
$ sudo apt-get install python-pip python-dev** # for Python 2.7
$ sudo apt-get install python3-pip python3-dev** # for Python 3.n
Assuming the prerequisite software is installed on your Linux host, take the following steps:
$ pip install tensorflow** # Python 2.7; CPU support (no GPU support)
$ pip3 install tensorflow** # Python 3.n; CPU support (no GPU support)
$ pip install tensorflow-gpu** # Python 2.7; GPU support
$ pip3 install tensorflow-gpu** # Python 3.n; GPU support
If the preceding command runs to completion, you should now [validate your installation](https://tensorflow.google.cn/install/install_linux#ValidateYourInstallation).
$ sudo pip install --upgrade** *tfBinaryURL* # Python 2.7
$ sudo pip3 install --upgrade** *tfBinaryURL* # Python 3.n
where `*tfBinaryURL*` identifies the URL of the TensorFlow Python package. The appropriate value of `*tfBinaryURL*` depends on the operating system, Python version, and GPU support. Find the appropriate value for `*tfBinaryURL*` [here](https://tensorflow.google.cn/install/install_linux#the_url_of_the_tensorflow_python_package). For example, to install TensorFlow for Linux, Python 3.4, and CPU-only support, issue the following command:
$ sudo pip3 install --upgrade \
https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.7.0-cp34-cp34m-linux_x86_64.whl
If this step fails, see [Common Installation Problems](https://tensorflow.google.cn/install/install_linux#common_installation_problems).
After installing TensorFlow, validate your installation.
To uninstall TensorFlow, issue one of following commands:
$ sudo pip uninstall tensorflow** # for Python 2.7
$ sudo pip3 uninstall tensorflow** # for Python 3.n
Take the following steps to install TensorFlow through Docker:
docker
to allow launching containers without sudo as described in the Docker documentation. (If you don't do this step, you'll have to use sudo each time you invoke Docker.)The remainder of this section explains how to launch a Docker container.
To launch a Docker container with CPU-only support (that is, without GPU support), enter a command of the following format:
$ docker run -it *-p hostPort:containerPort TensorFlowCPUImage*
where:
-p
flag, setting both hostPort and containerPort to 6006.TensorFlow images are available at dockerhub.
For example, the following command launches the latest TensorFlow CPU binary image in a Docker container from which you can run TensorFlow programs in a shell:
$ docker run -it tensorflow/tensorflow bash
The following command also launches the latest TensorFlow CPU binary image in a Docker container. However, in this Docker container, you can run TensorFlow programs in a Jupyter notebook:
$ docker run -it -p 8888:8888 tensorflow/tensorflow
Docker will download the TensorFlow binary image the first time you launch it.
Prior to installing TensorFlow with GPU support, ensure that your system meets all NVIDIA software requirements. To launch a Docker container with NVidia GPU support, enter a command of the following format:
$ nvidia-docker run -it** *-p hostPort:containerPort TensorFlowGPUImage*
where:
*containerPort*
to 8888
.We recommend installing one of the latest
versions. For example, the following command launches the latest TensorFlow GPU binary image in a Docker container from which you can run TensorFlow programs in a shell:
$ nvidia-docker run -it tensorflow/tensorflow:latest-gpu bash
The following command also launches the latest TensorFlow GPU binary image in a Docker container. In this Docker container, you can run TensorFlow programs in a Jupyter notebook:
$ nvidia-docker run -it -p 8888:8888 tensorflow/tensorflow:latest-gpu
The following command installs an older TensorFlow version (0.12.1):
$ nvidia-docker run -it -p 8888:8888 tensorflow/tensorflow:0.12.1-gpu
Docker will download the TensorFlow binary image the first time you launch it. For more details see the TensorFlow docker readme.
You should now validate your installation.
Take the following steps to install TensorFlow in an Anaconda environment:
$ conda create -n tensorflow pip python=2.7 # or python=3.3, etc.
$ source activate tensorflow
(tensorflow)$ # Your prompt should change
(tensorflow)$ pip install --ignore-installed --upgrade** *tfBinaryURL*
where `*tfBinaryURL*` is the [URL of the TensorFlow Python package](https://tensorflow.google.cn/install/install_linux#the_url_of_the_tensorflow_python_package). For example, the following command installs the CPU-only version of TensorFlow for Python 3.4:
(tensorflow)$ pip install --ignore-installed --upgrade \
https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.7.0-cp34-cp34m-linux_x86_64.whl
To validate your TensorFlow installation, do the following:
If you installed on native pip, Virtualenv, or Anaconda, then do the following:
If you installed through Docker, start a Docker container from which you can run bash. For example:
$ docker run -it tensorflow/tensorflow bash
Invoke python from your shell as follows:
$ python
Enter the following short program inside the python interactive shell:
# Python import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!') sess = tf.Session() print(sess.run(hello))`
If the system outputs the following, then you are ready to begin writing TensorFlow programs:
Hello, TensorFlow!
If you are new to TensorFlow, see Getting Started with TensorFlow.
If the system outputs an error message instead of a greeting, see Common installation problems.
We are relying on Stack Overflow to document TensorFlow installation problems and their remedies. The following table contains links to Stack Overflow answers for some common installation problems. If you encounter an error message or other installation problem not listed in the following table, search for it on Stack Overflow. If Stack Overflow doesn't show the error message, ask a new question about it on Stack Overflow and specify the tensorflow
tag.
Stack Overflow Link Error Message
ImportError: libcudart.so.*Version*: cannot open shared object file:
No such file or directory
ImportError: libcudnn.*Version*: cannot open shared object file:
No such file or directory
libprotobuf ERROR google/protobuf/src/google/protobuf/io/coded_stream.cc:207] A
protocol message was rejected because it was too big (more than 67108864 bytes).
To increase the limit (or to disable these warnings), see
CodedInputStream::SetTotalBytesLimit() in google/protobuf/io/coded_stream.h.
Error importing tensorflow. Unless you are using bazel, you should
not try to import tensorflow from its source directory; please exit the
tensorflow source tree, and relaunch your python interpreter from
there.
IOError: [Errno 2] No such file or directory:
'/tmp/pip-o6Tpui-build/setup.py'
ImportError: Traceback (most recent call last):
File ".../tensorflow/core/framework/graph_pb2.py", line 6, in <module>
from google.protobuf import descriptor as _descriptor
ImportError: cannot import name 'descriptor'</module>
SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify
failed
Installing collected packages: setuptools, protobuf, wheel, numpy, tensorflow
Found existing installation: setuptools 1.1.6
Uninstalling setuptools-1.1.6:
Exception:
...
[Errno 1] Operation not permitted:
'/tmp/pip-a1DXRT-uninstall/.../lib/python/_markerlib'
...
Installing collected packages: setuptools, protobuf, wheel, numpy, tensorflow
Found existing installation: setuptools 1.1.6
Uninstalling setuptools-1.1.6:
Exception:
...
[Errno 1] Operation not permitted:
'/tmp/pip-a1DXRT-uninstall/System/Library/Frameworks/Python.framework/
Versions/2.7/Extras/lib/python/_markerlib'
A few installation mechanisms require the URL of the TensorFlow Python package. The value you specify depends on three factors:
This section documents the relevant values for Linux installations.
CPU only:
https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.7.0-cp27-none-linux_x86_64.whl
GPU support:
https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.7.0-cp27-none-linux_x86_64.whl
Note that GPU support requires the NVIDIA hardware and software described in NVIDIA requirements to run TensorFlow with GPU support.
CPU only:
https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.7.0-cp34-cp34m-linux_x86_64.whl
GPU support:
https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.7.0-cp34-cp34m-linux_x86_64.whl
Note that GPU support requires the NVIDIA hardware and software described in NVIDIA requirements to run TensorFlow with GPU support.
CPU only:
https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.7.0-cp35-cp35m-linux_x86_64.whl
GPU support:
https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.7.0-cp35-cp35m-linux_x86_64.whl
Note that GPU support requires the NVIDIA hardware and software described in NVIDIA requirements to run TensorFlow with GPU support.
CPU only:
https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.7.0-cp36-cp36m-linux_x86_64.whl
GPU support:
https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.7.0-cp36-cp36m-linux_x86_64.whl
Note that GPU support requires the NVIDIA hardware and software described in NVIDIA requirements to run TensorFlow with GPU support.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 3.0 License, and code samples are licensed under the Apache 2.0 License. For details, see our Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated April 9, 2018.
https://tensorflow.google.cn/install/install_linux