How To Install TensorFlow on Ubuntu 22.04
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
- Deploy an Ubuntu 22.04 at Vultr with at least 4GB RAM and two vCPU.
- SSH and Login to the server as a non-root user with sudo rights.
- Install Python 3.8 or higher on the server.
Setup the Development Environment
Update the server.
$ sudo apt update
Verify that the Installed Python version is 3.8 or higher.
$ python3 --version
Output:
Python 3.10.6
Update PIP.
$ pip install --upgrade pip
Install the Python Virtual Environment tool.
$ sudo apt install python3-venv -y
Create a new project directory.
$ mkdir ~/example
Switch to the directory.
$ cd ~/example/
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
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
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
Access the Python shell.
(vultr-tensorflow)$ python3
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
Print the installed TensorFlow version.
>>> print(vtf.__version__)
Output:
2.10.0
Perform a quick mathematical operation using TensorFlow.
>>> print(vtf.reduce_sum(vtf.random.normal([500, 500])))
Output:
tf.Tensor(370.10406, shape=(), dtype=float32)
Exit the Python Shell.
>>> exit()
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.