Tutorial

How To Install TensorFlow on Ubuntu 20.04

Published on June 23, 2020
English
How To Install TensorFlow on Ubuntu 20.04
Not using Ubuntu 20.04?Choose a different version or distribution.
Ubuntu 20.04

Introduction

An open-source machine learning software library, TensorFlow is used to train neural networks. Expressed in the form of stateful dataflow graphs, each node in the graph represents the operations performed by neural networks on multi-dimensional arrays. These multi-dimensional arrays are commonly known as “tensors,” hence the name TensorFlow.

In this tutorial, you’ll install TensorFlow in a Python virtual environment with virtualenv. This approach isolates the TensorFlow installation and gets things up and running quickly. Once you complete the installation, you’ll validate your installation by importing Tensorflow to ensure you have no errors.

Prerequisites

Before you begin this tutorial, you’ll need the following:

Step 1 — Creating a Programming Environment

In this step, we’ll create a virtual environment in order to install TensorFlow into it without compromising our other programming projects. If you already have a clean programming environment set up, feel free to skip this step.

First, create a project directory. We’ll call it tf-demo for demonstration purposes, but choose a directory name that is meaningful to you:

  1. mkdir ~/tf-demo

Navigate to your newly created tf-demo directory:

  1. cd ~/tf-demo

Then create a new virtual environment called tensorflow-dev, for instance. Run the following command to create the environment:

  1. python3 -m venv tensorflow-dev

This creates a new tensorflow-dev directory which will contain all of the packages that you install while this environment is activated. It also includes pip and a standalone version of Python.

Now activate your virtual environment:

  1. source tensorflow-dev/bin/activate

Once activated, your terminal prompt will reflect that you are in the virtual environment:

(tensorflow-dev)username@hostname:~/tf-demo $

At this point you can install TensorFlow in your virtual environment.

Step 2 — Installing TensorFlow

When installing TensorFlow, we want to make sure we are installing and upgrading to the newest version available in PyPi.

Therefore, we’ll be using the following command syntax with pip:

  1. pip install --upgrade tensorflow

Once you press ENTER, TensorFlow will install, and you should receive output that indicates that the install along with any dependent packages was successful.

Output
... Successfully installed absl-py-0.7.1 astor-0.7.1 gast-0.2.2 grpcio-1.19.0 h5py-2.9.0 keras-applications-1.0.7 keras-preprocessing-1.0.9 markdown-3.0.1 mock-2.0.0 numpy-1.16.2 pbr-5.1.3 protobuf-3.7.0 setuptools-40.8.0 tensorboard-1.13.1 tensorflow-1.13.1 tensorflow-estimator-1.13.0 termcolor-1.1.0 werkzeug-0.15.0 wheel-0.33.1 ... Successfully installed bleach-1.5.0 enum34-1.1.6 html5lib-0.9999999 markdown-2.6.9 numpy-1.13.3 protobuf-3.5.0.post1 setuptools-38.2.3 six-1.11.0 tensorflow-1.4.0 tensorflow-tensorboard-0.4.0rc3 werkzeug-0.12.2 wheel-0.30.0

You can deactivate your virtual environment at any time by using the following command:

  1. deactivate

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

Now that you have installed TensorFlow, let’s make sure the TensorFlow installation works.

Step 3 — Validating Installation

To validate the installation of TensorFlow, we are going to ensure that we can import the TensorFlow package.

  1. python

The following prompt will appear on your terminal:

>>>

This is the prompt for the Python interpreter, and it indicates that it’s ready for you to start entering some Python statements.

First, type this line to import the TensorFlow package and make it available as the local variable tf. Press ENTER after typing in the line of code:

  1. import tensorflow as tf

As long as you have received no errors, you have installed TensorFlow successfully. If you have received an error, you should ensure that your server is powerful enough to handle TensorFlow. You may need to resize your server, making sure it has at least 4GB of memory.

Conclusion

In this tutorial, you have installed TensorFlow in a Python virtual environment and validated that TensorFlow works by importing it.

TensorFlow’s programmer’s guide provides a useful resource and reference for TensorFlow development. You can also explore Kaggle, a competitive environment for practical application of machine learning concepts that pit you against other machine learning, data science, and statistics enthusiasts.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
2 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

if get an error after typing in “import tensorflow as tf”. Unable to figure it out

">>> import tensorflow as tf 2024-01-14 19:30:18.202052: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:9261] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered 2024-01-14 19:30:18.202267: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:607] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered 2024-01-14 19:30:18.204603: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1515] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered 2024-01-14 19:30:18.214772: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations. To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags. 2024-01-14 19:30:19.617811: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT "

preliminary step is to install latest nvidia drivers and cuda which happens at https://developer.nvidia.com/cuda-downloads

interestingly if you install from this download it will give you matching nvidia drivers, namely when you choose cuda 11.6 it install that and nvidia driver 510 which is super … thanks for this tutorial

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.