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

Install Minecraft Server on Ubuntu 18.04: Systemd Setup Guide

Minecraft is one of the most popular games ever made — a sandbox game where players…

1 hour ago

Install CouchDB on Ubuntu 18.04: Setup and Configuration Guide

CouchDB is a free, open-source NoSQL database maintained by the Apache Software Foundation. Unlike traditional relational…

3 hours ago

Install Docker Compose on Ubuntu 18.04: Setup and Usage Guide

Docker Compose is a tool that lets you define and run multi-container Docker applications using a…

3 hours ago

Install PHP Composer on Ubuntu 18.04: Setup and Getting Started

Composer is a dependency manager for PHP. It works similarly to npm for Node.js or pip…

3 hours ago

Install and Configure Squid Proxy on Ubuntu 18.04: Setup Guide

Squid is a full-featured caching proxy server that supports HTTP, HTTPS, and FTP. It is most…

1 day ago

Install Chromium on Ubuntu 18.04: The Open-Source Browser Guide

Chromium is a fast, lightweight, open-source web browser developed primarily by Google. It serves as the…

1 day ago