TensorFlow
Install
More
Learn
API
Resources
Com
munity
GitHub
Install
Build from source
Build a TensorFlow pip package from source and install it on Ubuntu Linux and macOS. While the instructions might work for other sy
stems, it is only tested and supported for Ubuntu and macOS.
Note: We already provide well-tested, pre-built TensorFlow packages for Linux and macOS sy
stems.
Setup for Linux and macOS
Install the following build tools to con
figure your development environment.
Install Python and the TensorFlow package dependencies
Ubuntu
mac OS
sudo apt install python-dev python-pip # or
python3-dev
python3-pip
Install the TensorFlow pip package dependencies (if using a virtual environment, omit the --user argument):
pip install -U --user pip six numpy wheel mock
pip install -U --user keras_applications==1.0.6 --no-deps
pip install -U --user keras_preprocessing==1.0.5 --no-deps
The dependencies are listed in the setup.py file under
required_PACKAGES.
Install Bazel
Install Bazel, the build tool used to compile TensorFlow.
Add the location of the Bazel executable to your PATH environment variable.
Install GPU support (optional, Linux only)
There is no GPU support for macOS.
Read the GPU support guide to install the drivers and additional software
required to run TensorFlow on a GPU.
Note: It is easier to set up one of TensorFlow's GPU-enabled Docker images.
Download the TensorFlow source code
Use Git to clone the TensorFlow repository:
git clone https://github.com/tensorflow/tensorflow.git
cd tensorflow
The repo defaults to the master development branch. You can also checkout a release branch to build:
git checkout branch_name # r1.9, r1.10, etc.
Con
figure the build
Con
figure your sy
stem build by running the following at the root of your TensorFlow source tree:
./con
figure
This script prompts you for the location of TensorFlow dependencies and asks for additional build con
figuration options (compiler flags, for example). The following shows a sample run of ./con
figure (your session may differ):
View sample con
figuration session
Con
figuration options
For GPU support, specify the versions of CUDA and cuDNN. If your sy
stem has multiple versions of CUDA or cuDNN installed, explicitly set the version instead of relying on the default. ./con
figure creates symbolic links to your sy
stem's CUDA libraries—so if you update your CUDA library paths, this con
figuration step must be run again before building.
For compilation optimization flags, the default (-
march=native) optimizes the generated code for your machine's
cpu type. However, if building TensorFlow for a different
cpu type, consider a more specific optimization flag. See the GCC manual for examples.
There are some precon
figured build con
figs available that can be added to the bazel build command, for example:
--con
fig=mkl —Support for the Intel® MKL-DNN.
--con
fig=monolithic —Con
figuration for a mostly static, monolithic build.
Note: Starting with TensorFlow 1.6, binaries use AVX instructions which may not run on older
cpus.
Run the tests (optional)
To test your
copy of the source tree, run the following test for versions r1.12 and before (this may take a while):
bazel test -c opt -- //tensorflow/... -//tensorflow/compiler/... -//tensorflow/contrib/lite/...
For versions after r1.12 (like master), run the following:
bazel test -c opt -- //tensorflow/... -//tensorflow/compiler/... -//tensorflow/lite/...
Key Point: If you're having build problems on the latest development branch, try a release branch that is k
Nown to work.
Build the pip package
Bazel build
cpu-only
Use bazel to make the TensorFlow package builder with
cpu-only support:
bazel build --con
fig=opt //tensorflow/tools/pip_package:build_pip_package
GPU support
To make the TensorFlow package builder with GPU support:
bazel build --con
fig=opt --con
fig=cuda //tensorflow/tools/pip_package:build_pip_package
Bazel build options
Building TensorFlow from source can use a lot of RAM. If your sy
stem is memory-constrained, limit Bazel's RAM usage with: --local_resources 2048,.5,1.0.
The official TensorFlow packages are built with GCC 4 and use the older ABI. For GCC 5 and later, make your build compatible with the older ABI using: --cxxopt="-D_GLIBCXX_USE_CXX11_ABI=0". ABI compatibility ensures that custom ops built against the official TensorFlow package continue to work with the GCC 5 built package.
Build the package
The bazel build command creates an executable named build_pip_package—this is the program that builds the pip package. For example, the following builds a .whl package in the /tmp/tensorflow_pkg directory:
./bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
Although it is possible to build both CUDA and non-CUDA con
figurations under the same source tree, it's recommended to run bazel clean when switching between these two con
figurations in the same source tree.
Install the package
The filename of the generated .whl file depends on the TensorFlow version and your platform. Use pip install to install the package, for example:
pip install /tmp/tensorflow_pkg/tensorflow-version-tags.whl
Success: TensorFlow is
Now installed.
Docker Linux builds
TensorFlow's Docker development images are an easy way to set up an environment to build Linux packages from source. These images already contain the source code and dependencies
required to build TensorFlow. See the TensorFlow Docker guide for installation and the list of available image tags.
cpu-only
The following example uses the :nightly-devel image to build a
cpu-only Python 2 package from the latest TensorFlow source code. See the Docker guide for available TensorFlow -devel tags.
Download the latest development image and start a Docker container that we'll use to build the pip package:
docker pull tensorflow/tensorflow:nightly-devel
docker run -it -w /tensorflow -v $PWD:/mnt -e HOST_PERMS="$(id -u):$(id -g)" \
tensorflow/tensorflow:nightly-devel bash
git pull # within the container, download the latest source code
The above docker run command starts a shell in the /tensorflow directory—the root of the source tree. It mounts the host's current directory in the container's /mnt directory, and passes the host user's @R_422_
4045@ion to the container through an environmental variable (used to set permissions—Docker can make this tricky).
Alternatively, to build a host
copy of TensorFlow within a container, mount the host source tree at the container's /tensorflow directory:
docker run -it -w /tensorflow -v /path/to/tensorflow:/tensorflow -v $PWD:/mnt \
-e HOST_PERMS="$(id -u):$(id -g)" tensorflow/tensorflow:nightly-devel bash
With the source tree set up, build the TensorFlow package within the container's virtual environment:
Con
figure the build—this prompts the user to answer build con
figuration questions.
Build the tool used to create the pip package.
Run the tool to create the pip package.
Adjust the ownership permissions of the file for outside the container.
./con
figure # answer prompts or use defaults
bazel build --con
fig=opt //tensorflow/tools/pip_package:build_pip_package
./bazel-bin/tensorflow/tools/pip_package/build_pip_package /mnt # create package
chown $HOST_PERMS /mnt/tensorflow-version-tags.whl
Install and verify the package within the container:
pip uninstall tensorflow # remove current version
pip install /mnt/tensorflow-version-tags.whl
cd /tmp # don't import from source directory
python -c "import tensorflow as tf; print(tf.__version__)"
Success: TensorFlow is
Now installed.
On your host machine, the TensorFlow pip package is in the current directory (with host user permissions): ./tensorflow-version-tags.whl
GPU support
Docker is the easiest way to build GPU support for TensorFlow since the host machine only requires the NVIDIA® driver (the NVIDIA® CUDA® Toolkit doesn't have to be installed). See the GPU support guide and the TensorFlow Docker guide to set up nvidia-docker (Linux only).
The following example downloads the TensorFlow :nightly-devel-gpu-py3 image and uses nvidia-docker to run the GPU-enabled container. This development image is con
figured to build a Python 3 pip package with GPU support:
docker pull tensorflow/tensorflow:nightly-devel-gpu-py3
docker run --runtime=nvidia -it -w /tensorflow -v $PWD:/mnt -e HOST_PERMS="$(id -u):$(id -g)" \
tensorflow/tensorflow:nightly-devel-gpu-py3 bash
Then, within the container's virtual environment, build the TensorFlow package with GPU support:
./con
figure # answer prompts or use defaults
bazel build --con
fig=opt --con
fig=cuda //tensorflow/tools/pip_package:build_pip_package
./bazel-bin/tensorflow/tools/pip_package/build_pip_package /mnt # create package
chown $HOST_PERMS /mnt/tensorflow-version-tags.whl
Install and verify the package within the container and check for a GPU:
pip uninstall tensorflow # remove current version
pip install /mnt/tensorflow-version-tags.whl
cd /tmp # don't import from source directory
python -c "import tensorflow as tf; print(tf.contrib.eager.num_gpus())"
Success: TensorFlow is
Now installed.
Tested build con
figurations
Linux
Version Python version Compiler Build tools
tensorflow-1.12.0 2.7, 3.3-3.6 GCC 4.8 Bazel 0.15.0
tensorflow-1.11.0 2.7, 3.3-3.6 GCC 4.8 Bazel 0.15.0
tensorflow-1.10.0 2.7, 3.3-3.6 GCC 4.8 Bazel 0.15.0
tensorflow-1.9.0 2.7, 3.3-3.6 GCC 4.8 Bazel 0.11.0
tensorflow-1.8.0 2.7, 3.3-3.6 GCC 4.8 Bazel 0.10.0
tensorflow-1.7.0 2.7, 3.3-3.6 GCC 4.8 Bazel 0.10.0
tensorflow-1.6.0 2.7, 3.3-3.6 GCC 4.8 Bazel 0.9.0
tensorflow-1.5.0 2.7, 3.3-3.6 GCC 4.8 Bazel 0.8.0
tensorflow-1.4.0 2.7, 3.3-3.6 GCC 4.8 Bazel 0.5.4
tensorflow-1.3.0 2.7, 3.3-3.6 GCC 4.8 Bazel 0.4.5
tensorflow-1.2.0 2.7, 3.3-3.6 GCC 4.8 Bazel 0.4.5
tensorflow-1.1.0 2.7, 3.3-3.6 GCC 4.8 Bazel 0.4.2
tensorflow-1.0.0 2.7, 3.3-3.6 GCC 4.8 Bazel 0.4.2
Version Python version Compiler Build tools cuDNN CUDA
tensorflow_gpu-1.12.0 2.7, 3.3-3.6 GCC 4.8 Bazel 0.15.0 7 9
tensorflow_gpu-1.11.0 2.7, 3.3-3.6 GCC 4.8 Bazel 0.15.0 7 9
tensorflow_gpu-1.10.0 2.7, 3.3-3.6 GCC 4.8 Bazel 0.15.0 7 9
tensorflow_gpu-1.9.0 2.7, 3.3-3.6 GCC 4.8 Bazel 0.11.0 7 9
tensorflow_gpu-1.8.0 2.7, 3.3-3.6 GCC 4.8 Bazel 0.10.0 7 9
tensorflow_gpu-1.7.0 2.7, 3.3-3.6 GCC 4.8 Bazel 0.9.0 7 9
tensorflow_gpu-1.6.0 2.7, 3.3-3.6 GCC 4.8 Bazel 0.9.0 7 9
tensorflow_gpu-1.5.0 2.7, 3.3-3.6 GCC 4.8 Bazel 0.8.0 7 9
tensorflow_gpu-1.4.0 2.7, 3.3-3.6 GCC 4.8 Bazel 0.5.4 6 8
tensorflow_gpu-1.3.0 2.7, 3.3-3.6 GCC 4.8 Bazel 0.4.5 6 8
tensorflow_gpu-1.2.0 2.7, 3.3-3.6 GCC 4.8 Bazel 0.4.5 5.1 8
tensorflow_gpu-1.1.0 2.7, 3.3-3.6 GCC 4.8 Bazel 0.4.2 5.1 8
tensorflow_gpu-1.0.0 2.7, 3.3-3.6 GCC 4.8 Bazel 0.4.2 5.1 8
macOS
cpu
Version Python version Compiler Build tools
tensorflow-1.12.0 2.7, 3.3-3.6 Clang from xcode Bazel 0.15.0
tensorflow-1.11.0 2.7, 3.3-3.6 Clang from xcode Bazel 0.15.0
tensorflow-1.10.0 2.7, 3.3-3.6 Clang from xcode Bazel 0.15.0
tensorflow-1.9.0 2.7, 3.3-3.6 Clang from xcode Bazel 0.11.0
tensorflow-1.8.0 2.7, 3.3-3.6 Clang from xcode Bazel 0.10.1
tensorflow-1.7.0 2.7, 3.3-3.6 Clang from xcode Bazel 0.10.1
tensorflow-1.6.0 2.7, 3.3-3.6 Clang from xcode Bazel 0.8.1
tensorflow-1.5.0 2.7, 3.3-3.6 Clang from xcode Bazel 0.8.1
tensorflow-1.4.0 2.7, 3.3-3.6 Clang from xcode Bazel 0.5.4
tensorflow-1.3.0 2.7, 3.3-3.6 Clang from xcode Bazel 0.4.5
tensorflow-1.2.0 2.7, 3.3-3.6 Clang from xcode Bazel 0.4.5
tensorflow-1.1.0 2.7, 3.3-3.6 Clang from xcode Bazel 0.4.2
tensorflow-1.0.0 2.7, 3.3-3.6 Clang from xcode Bazel 0.4.2
GPU
Version Python version Compiler Build tools cuDNN CUDA
tensorflow_gpu-1.1.0 2.7, 3.3-3.6 Clang from xcode Bazel 0.4.2 5.1 8
tensorflow_gpu-1.0.0 2.7, 3.3-3.6 Clang from xcode Bazel 0.4.2 5.1 8
Was this page helpful? Let us k
Now how we did:
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 the Google Developers Site Policies. Java is a registered
Trademark of Oracle and/or its affiliates.
Stay connected
Blog
GitHub
Twitter
YouTube
Support
Issue tracker
Release notes
Stack Overflow
Terms
Privacy
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。