Installing TensorFlow on Ubuntu 20.04
Traducciones al EspañolEstamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
TensorFlow is an open-source software library used for machine learning and to train deep neural networks. Google developed TensorFlow for both research and production use, but it is now released under the Apache license. It is available for many operating systems, including the most common Linux distributions. For learning purposes, it is best to install TensorFlow in a Python virtual environment. TensorFlow is considered a good choice for those who are new to machine learning.
This guide describes how to install TensorFlow on Ubuntu 20.04, which is fully supported by TensorFlow. However, most Linux distributions follow a similar process.
Before You Begin
If you have not already done so, create a Linode account and Compute Instance. See our Getting Started with Linode and Creating a Compute Instance guides.
Follow our Setting Up and Securing a Compute Instance guide to update your system. You may also wish to set the timezone, configure your hostname, create a limited user account, and harden SSH access.
NoteThis guide is written for a non-root user. Commands that require elevated privileges are prefixed withsudo
. If you’re not familiar with thesudo
command, see the Linux Users and Groups guide.
Advantages of TensorFlow
TensorFlow offers different levels of abstraction and complexity for different types of tasks, along with APIs which make it easier to get started.
It is well suited for production as well as research and experimentation.
TensorFlow offers high-end computational graph visualization capabilities along with library management and debugging tools.
It is stable, scalable, and offers top-notch performance with excellent community support.
System Requirements
A robust and stable hosting environment with at least 4GB of memory, such as a Linode 4GB plan.
If you are using Ubuntu, TensorFlow requires version 16.04 or later.
Prerequisites
Before you install TensorFlow, you need to install the following:
- Python 3.8 or higher, and its required libraries
- Python virtual environment - to run TensorFlow inside a virtual environment
- Latest version of
pip
(version 19 or higher)
The following section explains how to install Python (if you have not already installed it), Python virtual environment, and the latest version of pip
.
Check Python and its Required Libraries
Check your system’s current version of Python.
python3 --version
Python 3.8.5
Install pip if it is not already installed.
sudo apt install python3-pip
Install Python Virtual Environment
If you already have Python installed, upgrade
apt
, and install the Python virtual environment and its required packages.sudo apt update sudo apt install python3-dev python3-pip python3-venv
Confirm the version of both Python and
pip
.python3 --version pip3 --version
Ubuntu returns the version for each module.
Python 3.8.5 pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8)
Create Python Virtual Environment
Setting up a virtual Python environment creates an isolated environment for your TensorFlow projects. Within this virtual environment, you can have an independent set of packages. This guarantees your TensorFlow projects cannot adversely affect your other Python projects.
Create a new directory for your TensorFlow development projects.
mkdir ~/tensorflow-dev cd ~/tensorflow-dev
Create the virtual environment using the following command:
python3 -m venv --system-site-packages ./venv
The above command creates a directory named
venv
which contains the supporting Python files. You can choose any name for the virtual environment in place of./venv
.Activate your virtual environment by running the
activate
script.source ./venv/bin/activate
Note
Thissource
command works for thesh
,bash
, andzsh
shells. If you are using acsh
ortcsh
shell, activate the virtual environment withsource ./venv/bin/activate.csh
. You can determine the name of the shell you are running with the commandecho $0
.After activating your virtual environment, your shell prompt is prefaced with
(venv)
(or whatever name you selected for the virtual environment directory).(venv) example-user@example-hostname:~/tensorflow-dev$
TensorFlow installation requires
pip
version 19 or higher. So within the virtual environment, upgrade thepip
package using the following command:pip install --upgrade pip
Your output confirms the
pip
upgrade.Successfully installed pip-21.0.1
Note
You can exit the virtual environment at any time with thedeactivate
command. You can use thesource
command to reactivate it again later. We recommend remaining inside the virtual environment while you are using TensorFlow.
Install TensorFlow
Within the virtual environment, install TensorFlow using
pip
. The following command fetches the latest stable version along with all package dependencies.pip install --upgrade tensorflow
List the Python packages with the following command and confirm
tensorflow
is present.pip list | grep tensorflow
The
tensorflow
module should be listed.tensorflow 2.4.1
Note
You can follow the below steps to install TensorFlow without using virtual environment, but it is NOT recommended.
Upgrade the Python-specific
pip
module withpython -m pip install --upgrade pip
Install TensorFlow using
pip3 install --user --upgrade tensorflow
.
Be very careful not to upgrade your system’s version of pip, because this is likely to cause unwanted side effects.
Test Your TensorFlow Installation
If you have followed all the above steps and installed TensorFlow, it is fairly easy to verify the TensorFlow installation. Use the following command to print the TensorFlow version:
python -c 'import tensorflow as tf; print(tf.__version__)'
2021-04-30 10:34:32.450931: W tensorflow/stream_executor/platform/default/dso_loader.cc:60] Could not load dynamic library 'libcudart.so.11.0'; dlerror: libcudart.so.11.0: cannot open shared object file: No such file or directory 2021-04-30 10:34:32.450973: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine. 2.4.1
Note
If your Linode is not running a GPU, you might receive a warning thatlibcudart
or a similar GPU library could not be loaded. This message is expected when running a CPU powered Linode. In this case, you should expect to see aninfo
message advising you to ignore the message in a non-GPU environment.To disable the warnings or error messages, you can use the
os.environ
variable to lower the level of log warnings that you receive. The example below prints the TensorFlow version without any warnings.python -c 'import os; os.environ["TF_CPP_MIN_LOG_LEVEL"]="3"; import tensorflow as tf; print(tf.__version__)'
2.4.1
Note
You can use different log levels in place of ‘3’ as shown below:
0
= all messages are logged (default behavior)1
= INFO messages are not printed2
= INFO and WARNING messages are not printed3
= INFO, WARNING, and ERROR messages are not printedTo deactivate the virtual environment and switch back to the original non-virtual shell, run the following command:
deactivate
Note
You can always runsource ./venv/bin/activate
to enter into the virtual environment again.
For Further Reference
Machine learning is a complicated discipline, and TensorFlow is a large and complex application. To help you get started, TensorFlow provides several additional resources:
- TensorFlow tutorials for beginners and experts
- Essential TensorFlow documentation
- List of TensorFlow modules & functions
- An Introduction to machine learning
- Tools to support your TensorFlow workflows
- TensorFlow community which suggests you on how you can participate and contribute.
We recommend you go through the extensive TensorFlow site as you work through your projects.
More Information
You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.
This page was originally published on