Google is committed to advancing racial equity for Black communities. See how.
Trang này được dịch bởi Cloud Translation API.
Switch to English

Install TensorFlow with pip

TensorFlow 2 packages are available

  • tensorflow —Latest stable release with CPU and GPU support (Ubuntu and Windows)
  • tf-nightly —Preview build (unstable). Ubuntu and Windows include GPU support.

Older versions of TensorFlow

For TensorFlow 1.x, CPU and GPU packages are separate:

  • tensorflow==1.15 —Release for CPU-only
  • tensorflow-gpu==1.15 —Release with GPU support (Ubuntu and Windows)

System requirements

Hardware requirements

  • Starting with TensorFlow 1.6, binaries use AVX instructions which may not run on older CPUs.
  • Read the GPU support guide to set up a CUDA®-enabled GPU card on Ubuntu or Windows.

1. Install the Python development environment on your system

Check if your Python environment is already configured:

python3 --version
pip3 --version

If these packages are already installed, skip to the next step.
Otherwise, install Python, the pip package manager, and venv:

Ubuntu

sudo apt update
sudo apt install python3-dev python3-pip python3-venv

macOS

Install using the Homebrew package manager:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
export PATH="/usr/local/opt/python/libexec/bin:$PATH"
# if you are on macOS 10.12 (Sierra) use `export PATH="/usr/local/bin:/usr/local/sbin:$PATH"`
brew update
brew install python  # Python 3

Windows

Install the Microsoft Visual C++ Redistributable for Visual Studio 2015, 2017, and 2019. Starting with the TensorFlow 2.1.0 version, the msvcp140_1.dll file is required from this package (which may not be provided from older redistributable packages). The redistributable comes with Visual Studio 2019 but can be installed separately:

  1. Go to the Microsoft Visual C++ downloads,
  2. Scroll down the page to the Visual Studio 2015, 2017 and 2019 section.
  3. Download and install the Microsoft Visual C++ Redistributable for Visual Studio 2015, 2017 and 2019 for your platform.

Make sure long paths are enabled on Windows.

Install the 64-bit Python 3 release for Windows (select pip as an optional feature).

Raspberry Pi

Requirements for the Raspbian operating system:

sudo apt update
sudo apt install python3-dev python3-pip python3-venv
sudo apt install libatlas-base-dev        # required for numpy

Other

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py

Python virtual environments are used to isolate package installation from the system.

Ubuntu / macOS

Create a new virtual environment by choosing a Python interpreter and making a ./venv directory to hold it:

python3 -m venv --system-site-packages ./venv

Activate the virtual environment using a shell-specific command:

source ./venv/bin/activate  # sh, bash, or zsh
. ./venv/bin/activate.fish  # fish
source ./venv/bin/activate.csh  # csh or tcsh

When the virtual environment is active, your shell prompt is prefixed with (venv).

Install packages within a virtual environment without affecting the host system setup. Start by upgrading pip:

pip install --upgrade pip

pip list  # show packages installed within the virtual environment

And to exit the virtual environment later:

deactivate  # don't exit until you're done using TensorFlow

Windows

Create a new virtual environment by choosing a Python interpreter and making a .\venv directory to hold it:

python -m venv --system-site-packages .\venv

Activate the virtual environment:

.\venv\Scripts\activate

Install packages within a virtual environment without affecting the host system setup. Start by upgrading pip:

pip install --upgrade pip

pip list  # show packages installed within the virtual environment

And to exit the virtual environment later:

deactivate  # don't exit until you're done using TensorFlow

Conda

While the TensorFlow provided pip package is recommended, a community-supported Anaconda package is available. To install, read the Anaconda TensorFlow guide.

3. Install the TensorFlow pip package

Choose one of the following TensorFlow packages to install from PyPI:

  • tensorflow —Latest stable release with CPU and GPU support (Ubuntu and Windows).
  • tf-nightly —Preview build (unstable). Ubuntu and Windows include GPU support.
  • tensorflow==1.15 —The final version of TensorFlow 1.x.

Virtual environment install

pip install --upgrade tensorflow

Verify the install:

python -c "import tensorflow as tf;print(tf.reduce_sum(tf.random.normal([1000, 1000])))"

System install

pip3 install --user --upgrade tensorflow  # install in $HOME

Verify the install:

python3 -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000, 1000])))"

Package location

A few installation mechanisms require the URL of the TensorFlow Python package. The value you specify depends on your Python version.

VersionURL
Linux
Python 3.5 GPU support https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-2.3.0-cp35-cp35m-manylinux2010_x86_64.whl
Python 3.5 CPU-only https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow_cpu-2.3.0-cp35-cp35m-manylinux2010_x86_64.whl
Python 3.6 GPU support https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-2.3.0-cp36-cp36m-manylinux2010_x86_64.whl
Python 3.6 CPU-only https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow_cpu-2.3.0-cp36-cp36m-manylinux2010_x86_64.whl
Python 3.7 GPU support https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-2.3.0-cp37-cp37m-manylinux2010_x86_64.whl
Python 3.7 CPU-only https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow_cpu-2.3.0-cp37-cp37m-manylinux2010_x86_64.whl
Python 3.8 GPU support https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-2.3.0-cp38-cp38-manylinux2010_x86_64.whl
Python 3.8 CPU-only https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow_cpu-2.3.0-cp38-cp38-manylinux2010_x86_64.whl
macOS (CPU-only)
Python 3.5 https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-2.3.0-cp35-cp35m-macosx_10_6_intel.whl
Python 3.6 https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-2.3.0-cp36-cp36m-macosx_10_9_x86_64.whl
Python 3.7 https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-2.3.0-cp37-cp37m-macosx_10_9_x86_64.whl
Python 3.8 https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-2.3.0-cp38-cp38-macosx_10_14_x86_64.whl
Windows
Python 3.5 GPU support https://storage.googleapis.com/tensorflow/windows/gpu/tensorflow_gpu-2.3.0-cp35-cp35m-win_amd64.whl
Python 3.5 CPU-only https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow_cpu-2.3.0-cp35-cp35m-win_amd64.whl
Python 3.6 GPU support https://storage.googleapis.com/tensorflow/windows/gpu/tensorflow_gpu-2.3.0-cp36-cp36m-win_amd64.whl
Python 3.6 CPU-only https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow_cpu-2.3.0-cp36-cp36m-win_amd64.whl
Python 3.7 GPU support https://storage.googleapis.com/tensorflow/windows/gpu/tensorflow_gpu-2.3.0-cp37-cp37m-win_amd64.whl
Python 3.7 CPU-only https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow_cpu-2.3.0-cp37-cp37m-win_amd64.whl
Python 3.8 GPU support https://storage.googleapis.com/tensorflow/windows/gpu/tensorflow_gpu-2.3.0-cp38-cp38-win_amd64.whl
Python 3.8 CPU-only https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow_cpu-2.3.0-cp38-cp38-win_amd64.whl
Raspberry PI (CPU-only)
Python 3, Pi0 or Pi1 https://storage.googleapis.com/tensorflow/raspberrypi/tensorflow-2.3.0-cp35-none-linux_armv6l.whl
Python 3, Pi2 or Pi3 https://storage.googleapis.com/tensorflow/raspberrypi/tensorflow-2.3.0-cp35-none-linux_armv7l.whl