How To

Install TensorFlow on Ubuntu 18.04: Python venv Setup and Usage

TensorFlow is a free, open-source machine learning platform developed by Google. It is used by major organizations — including Twitter, PayPal, Intel, and Airbus — to build and train neural networks for image recognition, natural language processing, recommendation engines, and predictive analytics.

TensorFlow can be installed system-wide, inside a Docker container, or using Anaconda. For development and learning, installing it inside a Python virtual environment is the recommended approach. Virtual environments keep each project’s dependencies completely isolated — you can run different versions of TensorFlow on the same machine without any package conflicts between projects.

This guide shows you how to install TensorFlow on Ubuntu 18.04 inside a Python virtual environment.

<strong>Prerequisite:</strong>&nbsp;You need sudo access.

Set Up a Python Virtual Environment

Ubuntu 18.04 ships with Python 3.6 by default. Verify it is available:

bashpython3 -V

Install the python3-venv package, which provides the venv module for creating isolated environments:

bashsudo apt install python3-venv

Create a directory for your TensorFlow project and navigate into it:

bashmkdir my_tensorflowcd my_tensorflow

Create the virtual environment inside the project directory:

bashpython3 -m venv venv

This creates a venv subdirectory containing a copy of the Python binary, the Pip package manager, the standard library, and other supporting files. You can name the environment anything — venv is just a common convention. You can also create as many separate virtual environments as you need, one per project, and each will maintain its own completely independent set of packages.

Activate the virtual environment:

bashsource venv/bin/activate

After activation, the shell prompt changes to show the environment name — for example, (venv). The environment’s bin directory is added to the front of your $PATH, so every python and pip command now runs against this isolated environment rather than the system-wide installation.

TensorFlow requires pip version 19 or higher. Upgrade it before installing:

bashpip install --upgrade pip

Install TensorFlow on Ubuntu

With the virtual environment active, install TensorFlow:

bashpip install --upgrade tensorflow

Pip pulls in TensorFlow and all its required dependencies — numpy, keras, protobuf, and others — into the venv directory. None of these packages affect your system Python or any other project on the machine.

GPU users: If you have a dedicated NVIDIA GPU and want to use it for model training, install tensorflow-gpu instead of the standard package. It includes CUDA support and delivers significantly faster training times for large models:

bashpip install --upgrade tensorflow-gpu

Inside an active virtual environment, use pip instead of pip3 and python instead of python3.

Verify the Installation and Next Steps

Confirm TensorFlow installed correctly by importing it and printing the version:

bashpython -c 'import tensorflow as tf; print(tf.__version__)'

If the version number prints without errors, TensorFlow is working. When you are done with your work, deactivate the virtual environment to return to your normal shell:

bashdeactivate

To resume work later, navigate back to the project directory and run source venv/bin/activate again. Your packages will be exactly as you left them.

TensorFlow is now installed on your Ubuntu 18.04 machine inside a clean, isolated virtual environment. Visit the TensorFlow tutorials page or explore the TensorFlow Model Repository on GitHub to run your first machine learning examples. Leave a comment below if you run into any issues during setup.

Cyber Defence

Recent Posts

file Command in Linux: Identify File Types Without Extensions

The file command inspects the actual contents of a file and reports its type — regardless of…

8 hours ago

chattr Command in Linux: Set File Attributes with lsattr

chattr sets and removes special file attributes that operate at the filesystem level, separate from standard…

8 hours ago

env Command in Linux: Show and Set Environment Variables

env prints the current environment, sets or removes variables for a single command, and can start…

8 hours ago

nmap Command in Linux: Port Scanning and Host Discovery Guide

nmap (Network Mapper) discovers live hosts, identifies open ports, and detects which service is running…

9 hours ago

id Command in Linux: Display User and Group Information

The id command prints user and group identity for any account on the system. It shows the…

1 day ago

sed Delete Lines: Remove Lines by Number, Pattern, or Range

sed processes input line by line, applies your commands, and writes the result to standard output.…

1 day ago