pip を使用して TensorFlow をインストールする

このガイドは、TensorFlow の最新の安定バージョンを対象としています。プレビュービルド(ナイトリービルド)の場合は、 tf-nightlyという pip パッケージを使用してください。以前のバージョンの TensorFlow の要件については、こちらの表を参照してください。CPU のみのビルドの場合は、 tensorflow-cpuという pip パッケージを使用してください。

インストールコマンドのクイックバージョンを以下に示します。詳細な手順については、下にスクロールしてください。

リナックス

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

ウィンドウズ 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])))"

夜間

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設定されている場合、古いGPUではTensorFlowの読み込みに失敗します。(詳細はアプリケーションの互換性をご覧ください。)

システム要件

  • 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 サポートにのみ必要です。

ステップバイステップの説明

リナックス

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++再頒布可能パッケージをインストールする

Visual Studio 2015、2017、2019用のMicrosoft Visual C++再頒布可能パッケージをインストールしてください。TensorFlow 2.1.0バージョン以降では、このパッケージのmsvcp140_1.dllファイルが必要です(古い再頒布可能パッケージでは提供されない場合があります)。この再頒布可能パッケージはVisual Studio 2019に付属していますが、別途インストールすることもできます。

  1. Microsoft Visual C++ のダウンロードに移動します。
  2. ページを下にスクロールして、Visual Studio 2015、2017、2019 のセクションまで移動します。
  3. お使いのプラットフォーム用のVisual Studio 2015、2017、2019 用の Microsoft Visual C++ 再頒布可能パッケージをダウンロードしてインストールします。

Windows で長いパスが有効になっていることを確認してください。

3. Minicondaをインストールする

Minicondaは、GPU対応のTensorFlowをインストールするのに推奨される方法です。Minicondaは、システムにインストールされているソフトウェアを変更することなく、独立した環境を作成します。また、特にGPUセットアップに必要なソフトウェアをインストールする最も簡単な方法でもあります。

Miniconda Windowsインストーラーをダウンロードします。ダウンロードしたファイルをダブルクリックし、画面の指示に従います。

4. conda環境を作成する

次のコマンドで、 tfという名前の新しい conda 環境を作成します。

conda create --name tf python=3.9

以下のコマンドで無効化および有効化できます。

conda deactivate
conda activate tf

インストールの残りの間はそれがアクティブになっていることを確認してください。

5. GPUのセットアップ

TensorFlow を CPU 上でのみ実行する場合は、このセクションをスキップできます。

まだインストールしていない場合は、まず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 は正常にインストールされています。

ウィンドウズ WSL2

1. システム要件

  • Windows 10 19044以降(64ビット)。これは、2021年11月の更新プログラムであるWindows 10バージョン21H2に相当します。

以下のドキュメントを参照してください。

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 のバージョンによって異なります。

バージョンURL
Linux x86
Python 3.9 GPU サポートhttps://storage.googleapis.com/tensorflow/versions/2.20.0/tensorflow-2.20.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Python 3.9 CPUのみhttps://storage.googleapis.com/tensorflow/versions/2.20.0/tensorflow_cpu-2.20.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Python 3.10 GPU サポートhttps://storage.googleapis.com/tensorflow/versions/2.20.0/tensorflow-2.20.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Python 3.10 CPUのみhttps://storage.googleapis.com/tensorflow/versions/2.20.0/tensorflow_cpu-2.20.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Python 3.11 GPU サポートhttps://storage.googleapis.com/tensorflow/versions/2.20.0/tensorflow-2.20.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Python 3.11 CPUのみhttps://storage.googleapis.com/tensorflow/versions/2.20.0/tensorflow_cpu-2.20.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Python 3.12 GPU サポートhttps://storage.googleapis.com/tensorflow/versions/2.20.0/tensorflow-2.20.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_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/2.20.0/tensorflow-2.20.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Python 3.13 CPUのみhttps://storage.googleapis.com/tensorflow/versions/2.20.0/tensorflow_cpu-2.20.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Linux Arm64 (CPUのみ)
Python 3.9 https://storage.googleapis.com/tensorflow/versions/2.20.0/tensorflow-2.20.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Python 3.10 https://storage.googleapis.com/tensorflow/versions/2.20.0/tensorflow-2.20.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Python 3.11 https://storage.googleapis.com/tensorflow/versions/2.20.0/tensorflow-2.20.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Python 3.12 https://storage.googleapis.com/tensorflow/versions/2.20.0/tensorflow-2.20.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Python 3.13 https://storage.googleapis.com/tensorflow/versions/2.20.0/tensorflow-2.20.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
macOS x86 (CPUのみ)
注意: TensorFlow 2.16はmacOS x86をサポートする最後のTensorFlowリリースでした。
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のみ)
Python 3.9 https://storage.googleapis.com/tensorflow/versions/2.20.0/tensorflow-2.20.0-cp39-cp39-macosx_12_0_arm64.whl
Python 3.10 https://storage.googleapis.com/tensorflow/versions/2.20.0/tensorflow-2.20.0-cp310-cp310-macosx_12_0_arm64.whl
Python 3.11 https://storage.googleapis.com/tensorflow/versions/2.20.0/tensorflow-2.20.0-cp311-cp311-macosx_12_0_arm64.whl
Python 3.12 https://storage.googleapis.com/tensorflow/versions/2.20.0/tensorflow-2.20.0-cp312-cp312-macosx_12_0_arm64.whl
Python 3.13 https://storage.googleapis.com/tensorflow/versions/2.20.0/tensorflow-2.20.0-cp313-cp313-macosx_12_0_arm64.whl
Windows (CPUのみ)
Python 3.9 https://storage.googleapis.com/tensorflow/versions/2.20.0/tensorflow_cpu-2.20.0-cp39-cp39-win_amd64.whl
Python 3.10 https://storage.googleapis.com/tensorflow/versions/2.20.0/tensorflow_cpu-2.20.0-cp310-cp310-win_amd64.whl
Python 3.11 https://storage.googleapis.com/tensorflow/versions/2.20.0/tensorflow_cpu-2.20.0-cp311-cp311-win_amd64.whl
Python 3.12 https://storage.googleapis.com/tensorflow/versions/2.20.0/tensorflow_cpu-2.20.0-cp312-cp312-win_amd64.whl
Python 3.13 https://storage.googleapis.com/tensorflow/versions/2.20.0/tensorflow_cpu-2.20.0-cp313-cp313-win_amd64.whl