TensorFlow Installation Tutorial

TensorFlow is a symbolic math system based on data flow programming, widely used for programming implementations of various machine learning algorithms. Its predecessor is Google’s neural network algorithm library DistBelief.

Now, let’s learn how to install TensorFlow.

Preparation

  1. On the TensorFlow official website, it is recommended to use Python versions 3.6-3.9. If you are currently using a version above 3.10, you may encounter compatibility issues. You need to install an older version of Python first. image-20241112163204711

  2. Go to the Python official website and find the download for the Windows version. image-20241112163516436

    Scroll down to find a version of Python 3.8, click to download, and make sure to select the installer version. image-20241112164008617

  3. After downloading, double-click to install and make sure to check Add Python 3.8 to PATH; otherwise, using commands later will be troublesome. image-20241112164401372image-20241112164457986

  4. We also need to install the C++ Redistributable, which you can find a link to on the TensorFlow official website and click directly. Scroll down to find Latest Microsoft Visual C++ Redistributable, and download the X64 version. image-20241112164650764

  5. Double-click to install.

    image-20241112164822855

    image-20241112164837251

CPU Version Installation

  1. Installing the CPU version is relatively simple and can be completed with just two commands. Open Command Prompt (recommended to run as administrator) and enter the following two commands: the first command updates the pip package manager, and the second command installs TensorFlow.

    1
    2
    pip install --upgrade pip
    pip install tensorflow

    If your internet speed is slow, you can use Tsinghua University’s mirror source (recommended! TensorFlow packages are large, and downloading from the official foreign source is very slow and prone to failure).

    1
    2
    pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple
    pip install tensorflow -i https://pypi.tuna.tsinghua.edu.cn/simple

    image-20241112165632763

    image-20241112170058531

  2. Enter the Python console and try applying it. If there are no errors, the installation is successful.

    1
    2
    import tensorflow as tf
    tf.__version__

    image-20241112170413343

GPU Version Installation

  1. First, ensure your computer has an NVIDIA GPU and that the driver is installed correctly (as long as the NVIDIA Control Panel appears).

    image-20241112183449302

  2. Confirm that your GPU supports CUDA and cuDNN.

    image-20241112212435097

  3. Go to the CUDA official website and download the CUDA driver. Based on the table above, download the corresponding version. I installed tensorflow-2.6.0, so I downloaded CUDA11.2.

    image-20241112184002284

    image-20241112184305093

    image-20241112184338382

  4. After downloading, double-click to install. The software will first extract, then proceed to the installation program. (Since the software has been updated, the steps in the later version may differ from the earlier version.)

    image-20241112190607257

    image-20241112190659031

    image-20250130221343420

  5. In the installation options, select Custom, check CUDA, and uncheck Visual Studio Integration.

    image-20250130221415507

    image-20250130221454513

  6. In Other components and Driver components, compare the components between the new version and the current version. If the current version is higher than the new version, uncheck it.

    image-20250130221519244

  7. You can change the installation path to another drive. Make sure to remember this path, as you will need it later.

    image-20250130221555723

  8. Wait for the installation to complete, and it’s done.

    image-20250130221654654

    image-20250130222025302

    image-20250130222034162

  9. Go to the cuDNN official website and download the corresponding version of cuDNN.

    image-20241112211947067

  10. After downloading, it’s a compressed package. Extract it and copy all the files to the folder where you just installed it.

    image-20241112212909449

  11. Open System Environment Variables.

    image-20241112213104549

    image-20241112213137690

    image-20241112213356082

  12. Add four paths in the system variables, as shown in the image below

    image-20241112213931527

    image-20241112214125357

  13. In Command Prompt, enter the following (command V is uppercase), and it will display the version number if the installation is successful.

    1
    nvcc -V

    image-20241112214412028

  14. Open Command Prompt (recommended to run as administrator) and enter the following two commands: the first command updates the pip package manager, and the second command installs TensorFlow, specifying the version because TensorFlow from 2.11.0 onwards can only call CUDA in WSL, so we install version 2.10.0.

    1
    2
    pip install --upgrade pip
    pip install tensorflow==2.10.0

    If your internet speed is slow, you can use Tsinghua University’s mirror source (recommended! TensorFlow packages are large, and downloading from the official foreign source is very slow and prone to failure).

    1
    2
    pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple
    pip install tensorflow==2.10.0 -i https://pypi.tuna.tsinghua.edu.cn/simple

    image-20241112221029314

    image-20241112224943840

  15. Enter the Python console and try applying it. If it returns true, the installation is successful.

    1
    2
    import tensorflow as tf
    tf.test.is_built_with_cuda()

    image-20241112230434745