In this blog post, we’ll walk you through the process of installing TensorFlow on Ubuntu. TensorFlow is an open-source library for machine learning and artificial intelligence, developed by Google Brain. It has become one of the most popular libraries for developers and researchers working with deep learning, computer vision, and natural language processing. So, let’s dive right in and learn how to Install TensorFlow on Ubuntu machine.
How to Install TensorFlow on Ubuntu
Table of Contents
- Prerequisites
- Step 1: Update Your System
- Step 2: Install Python and Pip
- Step 3: Install TensorFlow
- Step 4: Verify TensorFlow Installation
- Step 5: Install TensorFlow GPU Support (Optional)
- Conclusion
Prerequisites
Before we proceed with the TensorFlow installation, make sure you have the following:
- Ubuntu 18.04 or later installed on your machine. If you’re new to Ubuntu, check out our guide on how to set up a home server with Ubuntu.
- A user account with
sudo
privileges. Learn how to configure and manage network settings in Ubuntu if you need help.
Update Your System
Before installing TensorFlow, it’s essential to update your Ubuntu system to ensure you have the latest packages and security updates. Open a terminal window and run the following commands:
sudo apt update
sudo apt upgrade
Install Python and Pip
TensorFlow requires Python 3.6 or later. To check if you have Python installed on your system, run the following command:
python3 --version
If Python is not installed or you have an older version, run the following command to install Python:
sudo apt install python3
Next, install pip
, the Python package manager, by running the following command:
sudo apt install python3-pip
After installing pip
, you might want to upgrade it to the latest version using the following command:
pip3 install --upgrade pip
Install TensorFlow
Now that you have Python and pip
installed, you can proceed to install TensorFlow. Use the following command to install the latest version of TensorFlow:
pip3 install --user tensorflow
Alternatively, to install a specific version, you can use:
pip3 install --user tensorflow==VERSION
Replace VERSION
with the desired TensorFlow version, such as 2.6.0
.
Verify TensorFlow Installation
After installing TensorFlow, you can verify if the installation was successful by running a simple Python script. Create a new file named test_tensorflow.py` using your favourite text editor and add the following code:
import tensorflow as tf
# Create a constant Tensor
hello = tf.constant("Hello, TensorFlow!")
# Run a TensorFlow session
with tf.Session() as sess:
print(sess.run(hello))
Save the file and run the script using the following command:
python3 test_tensorflow.py
If the installation was successful, you should see the following output:
Hello, TensorFlow!
Install TensorFlow GPU Support
If you have an NVIDIA GPU and want to leverage its power for faster computation, you can install TensorFlow with GPU support. First, ensure that you have the NVIDIA CUDA Toolkit and cuDNN library installed on your system.
After installing the necessary GPU libraries, you can install the TensorFlow GPU package using the following command:
pip3 install --user tensorflow-gpu
To verify if TensorFlow is using your GPU, run the following Python script:
import tensorflow as tf
# Check if TensorFlow is using the GPU
if tf.test.is_gpu_available():
print("TensorFlow is using the GPU.")
else:
print("TensorFlow is not using the GPU.")
If everything is set up correctly, you should see the message “TensorFlow is using the GPU.”
Conclusion
Congratulations! You’ve successfully installed TensorFlow on your Ubuntu system. Now you’re ready to dive into the world of machine learning and deep learning.
If you’re new to TensorFlow, you might want to explore some of the official TensorFlow tutorials to get started. And if you’re looking to build a robust machine learning environment, check out our guides on deploying applications on Ubuntu with Docker and Kubernetes and how to set up a web server on Ubuntu using Apache.