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 PHP on Ubuntu 26.04: Apache, Nginx, and Multiple Versions

PHP 8.5 is included in Ubuntu 26.04's default repositories and is the recommended version for…

14 hours ago

Upgrade to Ubuntu 26.04 from 25.10 and 24.04 LTS: Complete Guide

Ubuntu 26.04 LTS "Resolute Raccoon" arrived on April 23, 2026 with Linux kernel 7.0, GNOME 50,…

14 hours ago

Install Kubernetes on Ubuntu 26.04 with kubeadm and containerd

Kubernetes is the standard platform for running containerized workloads across multiple servers with self-healing, rolling…

14 hours ago

Install Ubuntu 26.04: Bootable USB, Partitioning, and First Steps

Ubuntu 26.04 LTS "Resolute Raccoon" was released on April 23, 2026 with Linux kernel 7.0, GNOME desktop, and standard security support until April 2031. A clean install gives you a known-good starting point on new hardware, when replacing another operating system, or when an upgrade path is not practical. This guide walks through how to install Ubuntu 26.04: downloading and verifying the ISO, writing a bootable USB drive, completing the installer, and doing the initial setup after the first boot. Before you start: You need a USB drive with at least 12 GB of free space. Back up any existing data on the target machine — the installer can erase the entire disk. Install Ubuntu 26.04: Download the ISO…

14 hours ago

Change Timezone on Ubuntu: timedatectl and Desktop GUI Guide

The correct timezone affects more than the clock on your screen. It drives cron job scheduling, systemd timer execution, log file timestamps, database record timing, and SSL certificate validity checks. A mismatched timezone can cause scheduled jobs to fire at the wrong hour and make log timestamps impossible to match with real-world events. This guide shows how to change timezone on Ubuntu using the timedatectl command (the recommended approach for servers and remote machines) and through the graphical Date & Time settings on desktop systems. The steps apply to all current Ubuntu releases including 24.04 and 26.04. <strong>Prerequisite:</strong>&nbsp;Only&nbsp;the&nbsp;root&nbsp;user&nbsp;or&nbsp;a&nbsp;user&nbsp;with&nbsp;sudo&nbsp;access&nbsp;can&nbsp;change&nbsp;the&nbsp;system&nbsp;timezone. Check the Current Timezone Before You Change It Run timedatectl with no arguments to see the active timezone and clock status: bashtimedatectl Sample output: Local…

14 hours ago

Install Atom on Ubuntu 18.04: GitHub’s Code Editor APT Setup

Atom is a free, open-source, cross-platform code editor developed by GitHub. Built on Electron, it uses…

1 day ago