Build a TensorFlow pip package from source and install it on Ubuntu Linux and macOS. While the instructions might work for other systems, it is only tested and supported for Ubuntu and macOS.
Setup for Linux and macOS
Install the following build tools to configure your development environment.
Install Python and the TensorFlow package dependencies
Ubuntu
sudo apt install python3-dev python3-pip
macOS
Requires Xcode 9.2 or later.
Install using the Homebrew package manager:
brew install python
Install the TensorFlow pip package dependencies (if using a virtual
environment, omit the --user argument):
pip install -U --user pip numpy wheel packaging requests opt_einsumpip install -U --user keras_preprocessing --no-deps
Install Bazel
To build TensorFlow, you will need to install Bazel.
Bazelisk is an easy way to install
Bazel and automatically downloads the correct Bazel version for TensorFlow. For
ease of use, add Bazelisk as the bazel executable in your PATH.
If Bazelisk is not available, you can manually install Bazel. Make sure to install the correct Bazel version from TensorFlow's .bazelversion file.
Install Clang (recommended, Linux only)
Clang is a C/C++/Objective-C compiler that is compiled in C++ based on LLVM. It is the default compiler to build TensorFlow starting with TensorFlow 2.13. The current supported version is LLVM/Clang 16.
LLVM Debian/Ubuntu nightly packages provide an automatic installation script and packages for manual installation on Linux. Make sure you run the following command if you manually add llvm apt repository to your package sources:
sudo apt-get update && sudo apt-get install -y llvm-16 clang-16
Alternatively, you can download and unpack the pre-built Clang + LLVM 16.
Below is an example of steps you can take to set up the downloaded Clang + LLVM 16 binaries:
Change to the desired destination directory:
```cd <desired directory>```Load and extract an archive file...(suitable to your architecture):
wget https://github.com/llvm/llvm-project/releases/download/llvmorg-16.0.0/clang+llvm-16.0.0-x86_64-linux-gnu-ubuntu-18.04.tar.xztar -xvf clang+llvm-16.0.0-x86_64-linux-gnu-ubuntu-18.04.tar.xzCheck the obtained Clang + LLVM 16 binaries version:
./clang+llvm-16.0.0-x86_64-linux-gnu-ubuntu-18.04/bin/clang-16 --versionDirectory
/clang+llvm-16.0.0-x86_64-linux-gnu-ubuntu-18.04/bin/clang-16is the actual path to your new clang. You can run the./configurescript or manually set environment variablesCCandBAZEL_COMPILERto this path.
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.
Download the TensorFlow source code
Use Git to clone the TensorFlow repository:
git clone https://github.com/tensorflow/tensorflow.gitcd tensorflowThe repo defaults to the
masterdevelopment branch. You can also check out a release branch to build:git checkout branch_name # r2.2, r2.3, etc.
Configure the build
TensorFlow builds are configured by the
.bazelrcfile in the repository's root directory. The./configureor./configure.pyscripts can be used to adjust common settings.Please run the
./configurescript from the repository's root directory. This script will prompt you for the location of TensorFlow dependencies and asks for additional build configuration options (compiler flags, for example). Refer to the Sample session section for details../configure
There is also a python version of this script,
./configure.py. If using a virtual environment,python configure.pyprioritizes paths within the environment, whereas./configureprioritizes paths outside the environment. In both cases you can change the default.Sample session
The following shows a sample run of
./configurescript (your session may differ):Configuration options
GPU support
For GPU support, set
cuda=Yduring configuration and specify the versions of CUDA and cuDNN. If your system has multiple versions of CUDA or cuDNN installed, explicitly set the version instead of relying on the default../configurecreates symbolic links to your system's CUDA libraries—so if you update your CUDA library paths, this configuration step must be run again before building.Optimizations
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. Check the GCC manual for examples.Preconfigured configurations
There are some preconfigured build configs available that can be added to the
bazel buildcommand, for example:--config=dbg—Build with debug info. See CONTRIBUTING.md for details.--config=mkl—Support for the Intel® MKL-DNN.--config=monolithic—Configuration for a mostly static, monolithic build.
Build and install the pip package
The pip package is build in two steps. A
bazel buildcommands creates a "package-builder" program. You then run the package-builder to create the package.Build the package-builder
Use
bazel buildto create the TensorFlow 2.x package-builder:bazel build [--config=option] //tensorflow/tools/pip_package:build_pip_package
Bazel build options
Refer to the Bazel command-line reference for build options.
Building TensorFlow from source can use a lot of RAM. If your system is memory-constrained, limit Bazel's RAM usage with:
--local_ram_resources=2048.The official TensorFlow packages are built with a Clang toolchain that complies with the manylinux2014 package standard.
Build the package
The
bazel buildcommand creates an executable namedbuild_pip_package—this is the program that builds thepippackage. Run the executable as shown below to build a.whlpackage in the/tmp/tensorflow_pkgdirectory.To build from a release branch:
./bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
To build from master, use
--nightly_flagto get the right dependencies:./bazel-bin/tensorflow/tools/pip_package/build_pip_package --nightly_flag /tmp/tensorflow_pkg
Although it is possible to build both CUDA and non-CUDA configurations under the same source tree, it's recommended to run
bazel cleanwhen switching between these two configurations in the same source tree.Install the package
The filename of the generated
.whlfile depends on the TensorFlow version and your platform. Usepip installto install the package, for example:pip install /tmp/tensorflow_pkg/tensorflow-version-tags.whl
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. Go to the TensorFlow Docker guide for installation instructions and the list of available image tags.
CPU-only
The following example uses the
:develimage to build a CPU-only package from the latest TensorFlow source code. Check the Docker guide for available TensorFlow-develtags.Download the latest development image and start a Docker container that you'll use to build the pip package:
docker pull tensorflow/tensorflow:develdocker run -it -w /tensorflow_src -v $PWD:/mnt -e HOST_PERMS="$(id -u):$(id -g)" \ tensorflow/tensorflow:devel bashgit pull # within the container, download the latest source codeThe above
docker runcommand starts a shell in the/tensorflow_srcdirectory—the root of the source tree. It mounts the host's current directory in the container's/mntdirectory, and passes the host user's information 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
/tensorflowdirectory:docker run -it -w /tensorflow -v /path/to/tensorflow:/tensorflow -v $PWD:/mnt \ -e HOST_PERMS="\\((id -u):\\)(id -g)" tensorflow/tensorflow:devel bashWith the source tree set up, build the TensorFlow package within the container's virtual environment:
- Optional: Configure the build—this prompts the user to answer build configuration 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.
./configure # if necessarybazel build --config=opt //tensorflow/tools/pip_package:build_pip_package./bazel-bin/tensorflow/tools/pip_package/build_pip_package /mnt # create packagechown $HOST_PERMS /mnt/tensorflow-version-tags.whlInstall and verify the package within the container:
pip uninstall tensorflow # remove current versionpip install /mnt/tensorflow-version-tags.whlcd /tmp # don't import from source directorypython -c "import tensorflow as tf; print(tf.__version__)"On your host machine, the TensorFlow pip package is in the current directory (with host user permissions):
./tensorflow-version-tags.whlGPU 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). Refer to the GPU support guide and the TensorFlow Docker guide to set up nvidia-docker (Linux only).
The following example downloads the TensorFlow
:devel-gpuimage and usesnvidia-dockerto run the GPU-enabled container. This development image is configured to build a pip package with GPU support:docker pull tensorflow/tensorflow:devel-gpudocker run --gpus all -it -w /tensorflow -v $PWD:/mnt -e HOST_PERMS="$(id -u):$(id -g)" \ tensorflow/tensorflow:devel-gpu bashgit pull # within the container, download the latest source codeThen, within the container's virtual environment, build the TensorFlow package with GPU support:
./configure # if necessarybazel build --config=opt --config=cuda //tensorflow/tools/pip_package:build_pip_package./bazel-bin/tensorflow/tools/pip_package/build_pip_package /mnt # create packagechown $HOST_PERMS /mnt/tensorflow-version-tags.whlInstall and verify the package within the container and check for a GPU:
pip uninstall tensorflow # remove current versionpip install /mnt/tensorflow-version-tags.whlcd /tmp # don't import from source directorypython -c "import tensorflow as tf; print(\"Num GPUs Available: \", len(tf.config.list_physical_devices('GPU')))"Tested build configurations
Linux
CPU
Version Python version Compiler Build tools tensorflow-2.15.0 3.9-3.11 Clang 16.0.0 Bazel 6.1.0 tensorflow-2.14.0 3.9-3.11 Clang 16.0.0 Bazel 6.1.0 tensorflow-2.13.0 3.8-3.11 Clang 16.0.0 Bazel 5.3.0 tensorflow-2.12.0 3.8-3.11 GCC 9.3.1 Bazel 5.3.0 tensorflow-2.11.0 3.7-3.10 GCC 9.3.1 Bazel 5.3.0 tensorflow-2.10.0 3.7-3.10 GCC 9.3.1 Bazel 5.1.1 tensorflow-2.9.0 3.7-3.10 GCC 9.3.1 Bazel 5.0.0 tensorflow-2.8.0 3.7-3.10 GCC 7.3.1 Bazel 4.2.1 tensorflow-2.7.0 3.7-3.9 GCC 7.3.1 Bazel 3.7.2 tensorflow-2.6.0 3.6-3.9 GCC 7.3.1 Bazel 3.7.2 tensorflow-2.5.0 3.6-3.9 GCC 7.3.1 Bazel 3.7.2 tensorflow-2.4.0 3.6-3.8 GCC 7.3.1 Bazel 3.1.0 tensorflow-2.3.0 3.5-3.8 GCC 7.3.1 Bazel 3.1.0 tensorflow-2.2.0 3.5-3.8 GCC 7.3.1 Bazel 2.0.0 tensorflow-2.1.0 2.7, 3.5-3.7 GCC 7.3.1 Bazel 0.27.1 tensorflow-2.0.0 2.7, 3.3-3.7 GCC 7.3.1 Bazel 0.26.1 tensorflow-1.15.0 2.7, 3.3-3.7 GCC 7.3.1 Bazel 0.26.1 tensorflow-1.14.0 2.7, 3.3-3.7 GCC 4.8 Bazel 0.24.1 tensorflow-1.13.1 2.7, 3.3-3.7 GCC 4.8 Bazel 0.19.2 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 GPU
Version Python version Compiler Build tools cuDNN CUDA tensorflow-2.15.0 3.9-3.11 Clang 16.0.0 Bazel 6.1.0 8.8 12.2 tensorflow-2.14.0 3.9-3.11 Clang 16.0.0 Bazel 6.1.0 8.7 11.8 tensorflow-2.13.0 3.8-3.11 Clang 16.0.0 Bazel 5.3.0 8.6 11.8 tensorflow-2.12.0 3.8-3.11 GCC 9.3.1 Bazel 5.3.0 8.6 11.8 tensorflow-2.11.0 3.7-3.10 GCC 9.3.1 Bazel 5.3.0 8.1 11.2 tensorflow-2.10.0 3.7-3.10 GCC 9.3.1 Bazel 5.1.1 8.1 11.2 tensorflow-2.9.0 3.7-3.10 GCC 9.3.1 Bazel 5.0.0 8.1 11.2 tensorflow-2.8.0 3.7-3.10 GCC 7.3.1 Bazel 4.2.1 8.1 11.2 tensorflow-2.7.0 3.7-3.9 GCC 7.3.1 Bazel 3.7.2 8.1 11.2 tensorflow-2.6.0 3.6-3.9 GCC 7.3.1 Bazel 3.7.2 8.1 11.2 tensorflow-2.5.0 3.6-3.9 GCC 7.3.1 Bazel 3.7.2 8.1 11.2 tensorflow-2.4.0 3.6-3.8 GCC 7.3.1 Bazel 3.1.0 8.0 11.0 tensorflow-2.3.0 3.5-3.8 GCC 7.3.1 Bazel 3.1.0 7.6 10.1 tensorflow-2.2.0 3.5-3.8 GCC 7.3.1 Bazel 2.0.0 7.6 10.1 tensorflow-2.1.0 2.7, 3.5-3.7 GCC 7.3.1 Bazel 0.27.1 7.6 10.1 tensorflow-2.0.0 2.7, 3.3-3.7 GCC 7.3.1 Bazel 0.26.1 7.4 10.0 tensorflow_gpu-1.15.0 2.7, 3.3-3.7 GCC 7.3.1 Bazel 0.26.1 7.4 10.0 tensorflow_gpu-1.14.0 2.7, 3.3-3.7 GCC 4.8 Bazel 0.24.1 7.4 10.0 tensorflow_gpu-1.13.1 2.7, 3.3-3.7 GCC 4.8 Bazel 0.19.2 7.4 10.0 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-2.15.0 3.9-3.11 Clang from xcode 10.15 Bazel 6.1.0 tensorflow-2.14.0 3.9-3.11 Clang from xcode 10.15 Bazel 6.1.0 tensorflow-2.13.0 3.8-3.11 Clang from xcode 10.15 Bazel 5.3.0 tensorflow-2.12.0 3.8-3.11 Clang from xcode 10.15 Bazel 5.3.0 tensorflow-2.11.0 3.7-3.10 Clang from xcode 10.14 Bazel 5.3.0 tensorflow-2.10.0 3.7-3.10 Clang from xcode 10.14 Bazel 5.1.1 tensorflow-2.9.0 3.7-3.10 Clang from xcode 10.14 Bazel 5.0.0 tensorflow-2.8.0 3.7-3.10 Clang from xcode 10.14 Bazel 4.2.1 tensorflow-2.7.0 3.7-3.9 Clang from xcode 10.11 Bazel 3.7.2 tensorflow-2.6.0 3.6-3.9 Clang from xcode 10.11 Bazel 3.7.2 tensorflow-2.5.0 3.6-3.9 Clang from xcode 10.11 Bazel 3.7.2 tensorflow-2.4.0 3.6-3.8 Clang from xcode 10.3 Bazel 3.1.0 tensorflow-2.3.0 3.5-3.8 Clang from xcode 10.1 Bazel 3.1.0 tensorflow-2.2.0 3.5-3.8 Clang from xcode 10.1 Bazel 2.0.0 tensorflow-2.1.0 2.7, 3.5-3.7 Clang from xcode 10.1 Bazel 0.27.1 tensorflow-2.0.0 2.7, 3.5-3.7 Clang from xcode 10.1 Bazel 0.27.1 tensorflow-2.0.0 2.7, 3.3-3.7 Clang from xcode 10.1 Bazel 0.26.1 tensorflow-1.15.0 2.7, 3.3-3.7 Clang from xcode 10.1 Bazel 0.26.1 tensorflow-1.14.0 2.7, 3.3-3.7 Clang from xcode Bazel 0.24.1 tensorflow-1.13.1 2.7, 3.3-3.7 Clang from xcode Bazel 0.19.2 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