使用 pip 安装 TensorFlow

本指南适用于 TensorFlow 的最新稳定版本。如需预览构建版本 (nightly),请使用名为 tf-nightly 的 pip 软件包。有关旧版 TensorFlow 的要求,请参阅这些表格。如需仅使用 CPU 的构建版本,请使用名为 tensorflow-cpu 的 pip 软件包。

以下是安装命令的快速版本。向下滚动查看分步说明。

Linux

python3 -m pip install 'tensorflow[and-cuda]'
# Verify the installation:
python3 -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"

MacOS

# There is currently no official GPU support for MacOS.
python3 -m pip install tensorflow
# Verify the installation:
python3 -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000, 1000])))"

Windows 原生

conda install -c conda-forge cudatoolkit=11.2 cudnn=8.1.0
# Anything above 2.10 is not supported on the GPU on Windows Native
python -m pip install "tensorflow<2.11"
# Verify the installation:
python -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"

Windows WSL2

python3 -m pip install tensorflow[and-cuda]
# Verify the installation:
python3 -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"

CPU

python3 -m pip install tensorflow
# Verify the installation:
python3 -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000, 1000])))"

Nightly 版本

python3 -m pip install tf-nightly
# Verify the installation:
python3 -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000, 1000])))"

硬件要求

支持以下支持 GPU 的设备

  • 具有 CUDA® 架构 3.5、5.0、6.0、7.0、7.5、8.0 及更高版本的 NVIDIA® GPU 卡。请参阅 CUDA® 支持的 GPU 卡列表
  • 对于具有不受支持的 CUDA® 架构的 GPU,或者为了避免来自 PTX 的 JIT 编译,或者为了使用不同版本的 NVIDIA® 库,请参阅 Linux 源码构建指南。
  • 除了最新支持的 CUDA® 架构外,软件包不包含 PTX 代码;因此,当设置 CUDA_FORCE_PTX_JIT=1 时,TensorFlow 无法在较旧的 GPU 上加载。(有关详情,请参阅应用程序兼容性。)

系统要求

  • Ubuntu 16.04 或更高版本(64 位)
  • macOS 12.0 (Monterey) 或更高版本(64 位)(无 GPU 支持)
  • Windows 原生 - Windows 7 或更高版本(64 位)(TF 2.10 后无 GPU 支持)
  • Windows WSL2 - Windows 10 19044 或更高版本(64 位)

软件要求

以下 NVIDIA® 软件仅在支持 GPU 时才需要。

分步说明

Linux

1. 系统要求

  • Ubuntu 16.04 或更高版本(64 位)

TensorFlow 仅官方支持 Ubuntu。但是,以下说明可能也适用于其他 Linux 发行版。

2. GPU 设置

如果您仅在 CPU 上运行 TensorFlow,则可以跳过此部分。

如果您尚未安装,请安装 NVIDIA GPU 驱动程序。您可以使用以下命令验证其是否已安装。

nvidia-smi

3. 使用 venv 创建虚拟环境

venv 模块是 Python 标准库的一部分,是官方推荐的创建虚拟环境的方式。

导航到您想要的虚拟环境目录,并使用以下命令创建一个名为 tf 的新 venv 环境。

python3 -m venv tf 

您可以使用以下命令激活它。

source tf/bin/activate    

确保虚拟环境在后续的安装过程中处于激活状态。

4. 安装 TensorFlow

TensorFlow 需要较新版本的 pip,因此请升级您的 pip 安装,以确保您运行的是最新版本。

pip install --upgrade pip

然后,使用 pip 安装 TensorFlow。

# For GPU users
pip install tensorflow[and-cuda]
# For CPU users
pip install tensorflow

6. 验证安装

验证 CPU 设置

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

如果返回了张量,则说明您已成功安装 TensorFlow。

验证 GPU 设置

python3 -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"

如果返回了 GPU 设备列表,则说明您已成功安装 TensorFlow。如果不是,请继续下一步

6. [仅限 GPU] 虚拟环境配置

如果上一节中的 GPU 测试未成功,最可能的原因是组件未被检测到,和/或与现有的系统 CUDA 安装冲突。因此,您需要添加一些符号链接来修复此问题。

  • 创建指向 NVIDIA 共享库的符号链接
pushd $(dirname $(python -c 'print(__import__("tensorflow").__file__)'))
ln -svf ../nvidia/*/lib/*.so* .
popd
  • 创建指向 ptxas 的符号链接
ln -sf $(find $(dirname $(dirname $(python -c "import nvidia.cuda_nvcc;         
print(nvidia.cuda_nvcc.__file__)"))/*/bin/) -name ptxas -print -quit) $VIRTUAL_ENV/bin/ptxas

验证 GPU 设置

python3 -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"

MacOS

1. 系统要求

  • macOS 10.12.6 (Sierra) 或更高版本(64 位)

目前在 MacOS 上运行 TensorFlow 没有官方 GPU 支持。以下说明用于在 CPU 上运行。

2. 检查 Python 版本

检查您的 Python 环境是否已配置

python3 --version
python3 -m pip --version

3. 安装 TensorFlow

TensorFlow 需要较新版本的 pip,因此请升级您的 pip 安装,以确保您运行的是最新版本。

pip install --upgrade pip

然后,使用 pip 安装 TensorFlow。

pip install tensorflow

4. 验证安装

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

如果返回了张量,则说明您已成功安装 TensorFlow。

Windows 原生

1. 系统要求

  • Windows 7 或更高版本(64 位)

2. 安装 Microsoft Visual C++ Redistributable

安装 Microsoft Visual C++ Redistributable for Visual Studio 2015, 2017, and 2019。从 TensorFlow 2.1.0 版本开始,需要此软件包中的 msvcp140_1.dll 文件(旧版可再发行组件包可能不提供)。此可再发行组件包随 Visual Studio 2019 一起提供,但也可以单独安装。

  1. 前往 Microsoft Visual C++ 下载页面
  2. 向下滚动页面至 Visual Studio 2015, 2017 and 2019 部分。
  3. 为您的平台下载并安装 Microsoft Visual C++ Redistributable for Visual Studio 2015, 2017 and 2019

确保在 Windows 上启用了长路径

3. 安装 Miniconda

Miniconda 是安装支持 GPU 的 TensorFlow 的推荐方法。它创建一个独立的环境,以避免更改系统中已安装的任何软件。这也是安装所需软件的最简单方法,特别是对于 GPU 设置。

下载 Miniconda Windows 安装程序。双击下载的文件并按照屏幕上的说明进行操作。

4. 创建 conda 环境

使用以下命令创建一个名为 tf 的新 conda 环境。

conda create --name tf python=3.9

您可以使用以下命令停用和激活它。

conda deactivate
conda activate tf

确保它在后续的安装过程中处于激活状态。

5. GPU 设置

如果您仅在 CPU 上运行 TensorFlow,则可以跳过此部分。

如果您尚未安装,请首先安装 NVIDIA GPU 驱动程序

然后使用 conda 安装 CUDA 和 cuDNN。

conda install -c conda-forge cudatoolkit=11.2 cudnn=8.1.0

6. 安装 TensorFlow

TensorFlow 需要较新版本的 pip,因此请升级您的 pip 安装,以确保您运行的是最新版本。

pip install --upgrade pip

然后,使用 pip 安装 TensorFlow。

# Anything above 2.10 is not supported on the GPU on Windows Native
pip install "tensorflow<2.11" 

7. 验证安装

验证 CPU 设置

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

如果返回了张量,则说明您已成功安装 TensorFlow。

验证 GPU 设置

python -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"

如果返回了 GPU 设备列表,则说明您已成功安装 TensorFlow。

Windows WSL2

1. 系统要求

  • Windows 10 19044 或更高版本(64 位)。这对应于 Windows 10 版本 21H2(2021 年 11 月更新)。

请参阅以下文档以:

2. GPU 设置

如果您仅在 CPU 上运行 TensorFlow,则可以跳过此部分。

如果您尚未安装,请安装 NVIDIA GPU 驱动程序。您可以使用以下命令验证其是否已安装。

nvidia-smi

3. 安装 TensorFlow

TensorFlow 需要较新版本的 pip,因此请升级您的 pip 安装,以确保您运行的是最新版本。

pip install --upgrade pip

然后,使用 pip 安装 TensorFlow。

# For GPU users
pip install tensorflow[and-cuda]
# For CPU users
pip install tensorflow

4. 验证安装

验证 CPU 设置

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

如果返回了张量,则说明您已成功安装 TensorFlow。

验证 GPU 设置

python3 -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"

如果返回了 GPU 设备列表,则说明您已成功安装 TensorFlow。

软件包位置

一些安装机制需要 TensorFlow Python 软件包的 URL。您指定的值取决于您的 Python 版本。

Python 版本支持
版本URL
Linux x86
Python 3.10 GPU 支持 https://storage.googleapis.com/tensorflow/versions/%222.21.0%22/tensorflow-2.21.0-cp310-cp310-manylinux_2_27_x86_64.whl
Python 3.10 仅 CPU https://storage.googleapis.com/tensorflow/versions/%222.21.0%22/tensorflow_cpu-2.21.0-cp310-cp310-manylinux_2_27_x86_64.whl
Python 3.11 GPU 支持 https://storage.googleapis.com/tensorflow/versions/%222.21.0%22/tensorflow-2.21.0-cp311-cp311-manylinux_2_27_x86_64.whl
Python 3.11 仅 CPU https://storage.googleapis.com/tensorflow/versions/%222.21.0%22/tensorflow_cpu-2.21.0-cp311-cp311-manylinux_2_27_x86_64.whl
Python 3.12 GPU 支持 https://storage.googleapis.com/tensorflow/versions/%222.21.0%22/tensorflow-2.21.0-cp312-cp312-manylinux_2_27_x86_64.whl
Python 3.12 仅 CPU https://storage.googleapis.com/tensorflow/versions/2.20.0/tensorflow_cpu-2.20.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Python 3.13 GPU 支持 https://storage.googleapis.com/tensorflow/versions/%222.21.0%22/tensorflow-2.21.0-cp313-cp313-manylinux_2_27_x86_64.whl
Python 3.13 仅 CPU https://storage.googleapis.com/tensorflow/versions/%222.21.0%22/tensorflow_cpu-2.21.0-cp313-cp313-manylinux_2_27_x86_64.whl
Linux Arm64 (仅 CPU)
Python 3.10 https://storage.googleapis.com/tensorflow/versions/%222.21.0%22/tensorflow-2.21.0-cp310-cp310-manylinux_2_27_aarch64.whl
Python 3.11 https://storage.googleapis.com/tensorflow/versions/%222.21.0%22/tensorflow-2.21.0-cp311-cp311-manylinux_2_27_aarch64.whl
Python 3.12 https://storage.googleapis.com/tensorflow/versions/%222.21.0%22/tensorflow-2.21.0-cp312-cp312-manylinux_2_27_aarch64.whl
Python 3.13 https://storage.googleapis.com/tensorflow/versions/%222.21.0%22/tensorflow-2.21.0-cp313-cp313-manylinux_2_27_aarch64.whl
macOS x86 (仅 CPU)
警告: TensorFlow 2.16 是 最后一个 支持 macOS x86 的 TensorFlow 版本
Python 3.10 https://storage.googleapis.com/tensorflow/versions/2.16.2/tensorflow-2.16.2-cp310-cp310-macosx_10_15_x86_64.whl
Python 3.11 https://storage.googleapis.com/tensorflow/versions/2.16.2/tensorflow-2.16.2-cp311-cp311-macosx_10_15_x86_64.whl
Python 3.12 https://storage.googleapis.com/tensorflow/versions/2.16.2/tensorflow-2.16.2-cp312-cp312-macosx_10_15_x86_64.whl
macOS Arm64 (仅 CPU)
Python 3.10 https://storage.googleapis.com/tensorflow/versions/%222.21.0%22/tensorflow-2.21.0-cp310-cp310-macosx_12_0_arm64.whl
Python 3.11 https://storage.googleapis.com/tensorflow/versions/%222.21.0%22/tensorflow-2.21.0-cp311-cp311-macosx_12_0_arm64.whl
Python 3.12 https://storage.googleapis.com/tensorflow/versions/%222.21.0%22/tensorflow-2.21.0-cp312-cp312-macosx_12_0_arm64.whl
Python 3.13 https://storage.googleapis.com/tensorflow/versions/%222.21.0%22/tensorflow-2.21.0-cp313-cp313-macosx_12_0_arm64.whl
Windows (仅 CPU)
Python 3.10 https://storage.googleapis.com/tensorflow/versions/%222.21.0%22/tensorflow-2.21.0-cp310-cp310-win_amd64.whl
Python 3.11 https://storage.googleapis.com/tensorflow/versions/%222.21.0%22/tensorflow-2.21.0-cp311-cp311-win_amd64.whl
Python 3.12 https://storage.googleapis.com/tensorflow/versions/%222.21.0%22/tensorflow-2.21.0-cp312-cp312-win_amd64.whl
Python 3.13 https://storage.googleapis.com/tensorflow/versions/%222.21.0%22/tensorflow-2.21.0-cp313-cp313-win_amd64.whl