How To Install TensorFlow on Ubuntu 22.04

Updated on November 21, 2023
How To Install TensorFlow on Ubuntu 22.04 header image

TensorFlow is a popular open-source machine learning platform that helps users implement deep learning and machine learning models to solve common business problems. TensorFlow offers an ecosystem for developers and enterprises to build scalable machine learning applications. For example, it's used to train neural networks referred to as stateful dataflow graphs where each graph node represents neural network operations or multi-dimensional arrays.

This article explains how to TensorFlow in a Python virtual environment on a Ubuntu 22.04 server.

Prerequisites

Setup the Development Environment

  1. Update the server.

      $ sudo apt update
  2. Verify that the Installed Python version is 3.8 or higher.

      $ python3 --version

    Output:

      Python 3.10.6
  3. Update PIP.

      $ pip install --upgrade pip
  4. Install the Python Virtual Environment tool.

      $ sudo apt install python3-venv -y
  5. Create a new project directory.

      $ mkdir ~/example
  6. Switch to the directory.

      $ cd ~/example/
  7. Create a new virtual environment for the TensorFlow project. This creates the vultr-tensorflow project directory that contains the pip package manager, the standard python library, and a copy of all packages installed while the environment is activated.

      $ python3 -m venv vultr-tensorflow
  8. Activate the virtual environment.

      $ source vultr-tensorflow/bin/activate

    When activated, your terminal prompt should change to your virtual environment name as below.

      (vultr-tensorflow) user@hostname:~/example $

For purposes of this article, the shell prompt shortens to (vultr-tensorflow)$.

Install TensorFlow

  1. Install TensorFlow.

      (vultr-tensorflow)$ pip install --upgrade tensorflow

    Your installation output should look like the one below.

        Collecting tensorflow
      Downloading tensorflow-2.10.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (578.0 MB)
       ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 578.0/578.0 MB 3.3 MB/s eta 0:00:00
      Requirement already satisfied: setuptools in ./vultr-tensorflow/lib/python3.10/site-packages (from tensorflow) (59.6.0)

You have installed TensorFlow on the Ubuntu server, validate your installation by importing the TensorFlow package to Python.

Test

  1. Access the Python shell.

      (vultr-tensorflow)$ python3
  2. Import the TensorFlow package and make it available as the local variable vtf.

      >>> import tensorflow as vtf

    If the command returns any errors, verify that your server meets the minimum TensorFlow requirements and your virtual environment is active. If inactive, reactivate it using the following command.

      (vultr-tensorflow)$ source vultr-tensorflow/bin/activate
  3. Print the installed TensorFlow version.

      >>> print(vtf.__version__)

    Output:

      2.10.0
  4. Perform a quick mathematical operation using TensorFlow.

      >>> print(vtf.reduce_sum(vtf.random.normal([500, 500])))

    Output:

      tf.Tensor(370.10406, shape=(), dtype=float32)
  5. Exit the Python Shell.

      >>> exit()
  6. Deactivate the virtual environment.

      (vultr-tensorflow) $ deactivate

    To reactivate your environment later, switch to the project directory and run source vultr-tensorflow/bin/activate.

Conclusion

You have installed TensorFlow on a Ubuntu 22.04 server and verified that the program works by importing it to Python. For more information, please visit the official TensorFlow Programmers guide.