Install TensorFlow with pip

This guide is for the latest stable version of TensorFlow. For the preview build (nightly), use the pip package named tf-nightly. Refer to these tables for older TensorFlow version requirements. For the CPU-only build, use the pip package named tensorflow-cpu.

Here are the quick versions of the install commands. Scroll down for the step-by-step instructions.

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 Native

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])))"

Hardware requirements

The following GPU-enabled devices are supported:

  • NVIDIA® GPU card with CUDA® architectures 3.5, 5.0, 6.0, 7.0, 7.5, 8.0 and higher. See the list of CUDA®-enabled GPU cards.
  • For GPUs with unsupported CUDA® architectures, or to avoid JIT compilation from PTX, or to use different versions of the NVIDIA® libraries, see the Linux build from source guide.
  • Packages do not contain PTX code except for the latest supported CUDA® architecture; therefore, TensorFlow fails to load on older GPUs when CUDA_FORCE_PTX_JIT=1 is set. (See Application Compatibility for details.)

System requirements

  • Ubuntu 16.04 or higher (64-bit)
  • macOS 10.12.6 (Sierra) or higher (64-bit) (no GPU support)
  • Windows Native - Windows 7 or higher (64-bit) (no GPU support after TF 2.10)
  • Windows WSL2 - Windows 10 19044 or higher (64-bit)

Software requirements

The following NVIDIA® software are only required for GPU support.

Step-by-step instructions

Linux

1. System requirements

  • Ubuntu 16.04 or higher (64-bit)

TensorFlow only officially supports Ubuntu. However, the following instructions may also work for other Linux distros.

2. GPU setup

You can skip this section if you only run TensorFlow on the CPU.

Install the NVIDIA GPU driver if you have not. You can use the following command to verify it is installed.

nvidia-smi

3. Create a virtual environment with venv

The venv module is part of Python’s standard library and is the officially recommended way to create virtual environments.

Navigate to your desired virtual environments directory and create a new venv environment named tf with the following command.

python3 -m venv tf 

You can activate it with the following command.

source tf/bin/activate    

Make sure that the virtual environment is activated for the rest of the installation.

4. Install TensorFlow

TensorFlow requires a recent version of pip, so upgrade your pip installation to be sure you're running the latest version.

pip install --upgrade pip

Then, install TensorFlow with pip.

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

6. Verify the installation

Verify the CPU setup:

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

If a tensor is returned, you've installed TensorFlow successfully.

Verify the GPU setup:

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

If a list of GPU devices is returned, you've installed TensorFlow successfully. If not continue to the next step.

6. [GPU only] Virtual environment configuration

If the GPU test in the last section was unsuccessful, the most likely cause is that components aren't being detected, and/or conflict with the existing system CUDA installation. So you need to add some symbolic links to fix this.

  • Create symbolic links to NVIDIA shared libraries:
pushd $(dirname $(python -c 'print(__import__("tensorflow").__file__)'))
ln -svf ../nvidia/*/lib/*.so* .
popd
  • Create a symbolic link to 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

Verify the GPU setup:

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

MacOS

1. System requirements

  • macOS 10.12.6 (Sierra) or higher (64-bit)

Currently there is no official GPU support for running TensorFlow on MacOS. The following instructions are for running on CPU.

2. Check Python version

Check if your Python environment is already configured:

python3 --version
python3 -m pip --version

3. Install TensorFlow

TensorFlow requires a recent version of pip, so upgrade your pip installation to be sure you're running the latest version.

pip install --upgrade pip

Then, install TensorFlow with pip.

pip install tensorflow

4. Verify the installation

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

If a tensor is returned, you've installed TensorFlow successfully.

Windows Native

1. System requirements

  • Windows 7 or higher (64-bit)

2. Install Microsoft Visual C++ Redistributable

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.

3. Install Miniconda

Miniconda is the recommended approach for installing TensorFlow with GPU support. It creates a separate environment to avoid changing any installed software in your system. This is also the easiest way to install the required software especially for the GPU setup.

Download the Miniconda Windows Installer. Double-click the downloaded file and follow the instructions on the screen.

4. Create a conda environment

Create a new conda environment named tf with the following command.

conda create --name tf python=3.9

You can deactivate and activate it with the following commands.

conda deactivate
conda activate tf

Make sure it is activated for the rest of the installation.

5. GPU setup

You can skip this section if you only run TensorFlow on CPU.

First install NVIDIA GPU driver if you have not.

Then install the CUDA, cuDNN with conda.

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

6. Install TensorFlow

TensorFlow requires a recent version of pip, so upgrade your pip installation to be sure you're running the latest version.

pip install --upgrade pip

Then, install TensorFlow with pip.

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

7. Verify the installation

Verify the CPU setup:

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

If a tensor is returned, you've installed TensorFlow successfully.

Verify the GPU setup:

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

If a list of GPU devices is returned, you've installed TensorFlow successfully.

Windows WSL2

1. System requirements

  • Windows 10 19044 or higher (64-bit). This corresponds to Windows 10 version 21H2, the November 2021 update.

See the following documents to:

2. GPU setup

You can skip this section if you only run TensorFlow on the CPU.

Install the NVIDIA GPU driver if you have not. You can use the following command to verify it is installed.

nvidia-smi

3. Install TensorFlow

TensorFlow requires a recent version of pip, so upgrade your pip installation to be sure you're running the latest version.

pip install --upgrade pip

Then, install TensorFlow with pip.

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

4. Verify the installation

Verify the CPU setup:

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

If a tensor is returned, you've installed TensorFlow successfully.

Verify the GPU setup:

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

If a list of GPU devices is returned, you've installed TensorFlow successfully.

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 x86
Python 3.9 GPU support https://storage.googleapis.com/tensorflow/versions/2.17.0/tensorflow-2.17.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Python 3.9 CPU-only https://storage.googleapis.com/tensorflow/versions/2.17.0/tensorflow_cpu-2.17.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Python 3.10 GPU support https://storage.googleapis.com/tensorflow/versions/2.17.0/tensorflow-2.17.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Python 3.10 CPU-only https://storage.googleapis.com/tensorflow/versions/2.17.0/tensorflow_cpu-2.17.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Python 3.11 GPU support https://storage.googleapis.com/tensorflow/versions/2.17.0/tensorflow-2.17.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Python 3.11 CPU-only https://storage.googleapis.com/tensorflow/versions/2.17.0/tensorflow_cpu-2.17.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Python 3.12 GPU support https://storage.googleapis.com/tensorflow/versions/2.17.0/tensorflow-2.17.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Python 3.12 CPU-only https://storage.googleapis.com/tensorflow/versions/2.17.0/tensorflow_cpu-2.17.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Linux Arm64 (CPU-only)
Python 3.9 https://storage.googleapis.com/tensorflow/versions/2.17.0/tensorflow-2.17.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Python 3.10 https://storage.googleapis.com/tensorflow/versions/2.17.0/tensorflow-2.17.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Python 3.11 https://storage.googleapis.com/tensorflow/versions/2.17.0/tensorflow-2.17.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Python 3.12 https://storage.googleapis.com/tensorflow/versions/2.17.0/tensorflow-2.17.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
macOS x86 (CPU-only)
Caution: TensorFlow 2.16 was the last TensorFlow release that supported macOS x86
Python 3.9 https://storage.googleapis.com/tensorflow/versions/2.16.2/tensorflow-2.16.2-cp39-cp39-macosx_10_15_x86_64.whl
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-only)
Python 3.9 https://storage.googleapis.com/tensorflow/versions/2.17.0/tensorflow-2.17.0-cp39-cp39-macosx_12_0_arm64.whl
Python 3.10 https://storage.googleapis.com/tensorflow/versions/2.17.0/tensorflow-2.17.0-cp310-cp310-macosx_12_0_arm64.whl
Python 3.11 https://storage.googleapis.com/tensorflow/versions/2.17.0/tensorflow-2.17.0-cp311-cp311-macosx_12_0_arm64.whl
Python 3.12 https://storage.googleapis.com/tensorflow/versions/2.17.0/tensorflow-2.17.0-cp312-cp312-macosx_12_0_arm64.whl
Windows (CPU-only)
Python 3.9 https://storage.googleapis.com/tensorflow/versions/2.17.0/tensorflow-2.17.0-cp39-cp39-win_amd64.whl
Python 3.10 https://storage.googleapis.com/tensorflow/versions/2.17.0/tensorflow-2.17.0-cp310-cp310-win_amd64.whl
Python 3.11 https://storage.googleapis.com/tensorflow/versions/2.17.0/tensorflow-2.17.0-cp311-cp311-win_amd64.whl
Python 3.12 https://storage.googleapis.com/tensorflow/versions/2.17.0/tensorflow-2.17.0-cp312-cp312-win_amd64.whl