Select Page
Programming Languages - Python - school of programming - TensorFlow

Installing TensorFlow: Your Complete Tutorial

With more than 77% of Fortune 1000 organizations running artificial intelligence workloads in production as of 2021, machine learning (ML) on the rise. Many ML engineers are turning to libraries like Python’s TensorFlow to develop massive neural networks. However, ML engineers will approach the task from a variety of development contexts. With that in mind, we’ve put together this guide on how to install TensorFlow.

What is TensorFlow?

TensorFlow is an open-source software library originally developed by Google Brain researchers and developers. The team created the library to conduct research on ML and deep neural networks at Google. However, they designed TensorFlow to be applicable to a variety of use cases beyond those required by the tech giant. In fact, up-and-coming engineers can employ TensorFlow in their own personal projects with relative ease. Let’s take a look at how to install TensorFlow so you can begin your ML journey with Python.

Installing the TensorFlow Package

Installing the package itself is easy enough, but there are a few prerequisites to setting TensorFlow up properly. The steps can be found at TensorFlow’s installation guide, but we’ve broken them down here to simplify the process based on your operating system. 

We’ll present an overview of the process before getting into OS-specific instructions. First, you’ll need to install a Python development environment on your system — if you don’t already have one. Check out OS-specific resources for Windows, macOS and Linux for the steps to follow.

Next, you’ll want to activate a virtual environment (venv), which is a Python environment that lets you keep your pre-installed Python interpreter, libraries and scripts separate from those installed in other venvs. The venv module in Python is the recommended way of managing venvs, and it ships with the Python environment.

Once your venv is activated (we show the exact commands for this in the OS-specific sections below), you’ll need to install or update the latest version of pip, the standard package manager for Python. Usually, pip comes already installed with Python 3.0 or above, but it’s usually a good idea to use the latest version. No matter your OS, this step will look something like this:

# Requires the latest pip
pip install --upgrade pip

You have the option to install or upgrade pip before running your venv if you want pip installed and updated on your machine. In this tutorial, we’ll use the latest version of pip within our venv so as not to disturb your host system in any way.

Only then will you be ready for the final step of installing the TensorFlow pip package:

# Current stable release for CPU and GPU
pip install --upgrade tensorflow

Now that we have an overview of the installation process, let’s look at how to set TensorFlow up on different operating systems. We’ll learn how to install TensorFlow on Windows, macOS and Linux. Since the Python site directs you to download Python based on your OS, we’ll pick up with creating your venv and finish with running your own TensorFlow project. Make sure you have Python on your machine before continuing with the OS-specific venv instructions.

Installing TensorFlow on Windows

Let’s first go over how to install TensorFlow on Windows. With Windows, one way to create a venv is to make a .\venv directory in your Python interpreter. This directory will hold your venv:

python -m venv --system-site-packages .\venv

You can then activate the venv with:


Alternatively, you can install a venv within the global Python interpreter with the following command:

pip install --user virtualenv

If you go this route, make sure your version of pip coordinates to your OS by using the pip update flag command mentioned above:

pip install --upgrade pip

As long as you’re in your project folder, you can activate your venv with the same command listed in the first method mentioned a few lines above. You’re now ready to install TensorFlow with pip install tensorflow with the optional –upgrade flag.

Now that you know how to install TensorFlow, you’re ready to use it as a package. You’ll first need to import it in your IDE or text editor: 

import tensorflow as tf

With our first project, we just want to create a simple example. Instead of “Hello, World” let’s print “Hello, TensorFlow!” To do that, we’re going to create something called a constant op, which is basically a constant added as a node in a graph. The constant constructor creates this op:

tf.constant('Hello, TensorFlow')

But we don’t just want to run this, we want to save the output of this op to a variable: 

greeting = tf.constant('Hello, TensorFlow')

All we want to do now is to start a TensorFlow session and then run the op:

sess = tf.Session()

You’ll see Hello, Tensorflow printed to your console.

Installing TensorFlow on macOS

If you’re working with macOS, you’ll choose a Python interpreter and create a ./venv directory with the following command:

python3 -m venv --system-site-packages ./venv

When it comes to activating your venv, your command will depend on your shell. If you’re using sh, bash or zsh, your command will look like this:

source ./venv/bin/activate

With csh or tcsh, you’ll just concatenate .csh at the end of activate:

source ./venv/bin/activate

You’ll know your venv is activated when your shell prompt begins with (venv). 

You can use pip to install packages in your venv without affecting the host system setup. Start by upgrading pip and then use it to install TensorFlow and any other packages you need:

pip install --upgrade tensorflow

After this step, you can start using TensorFlow by importing it into your program:

import tensorflow as tf

For our first project, we’ll create a program that prints “Hello, TensorFlow!” To do so, simply put this code in your TensorFlow file below the import: 

greeting = tf.constant('Hello, TensorFlow')
sess = tf.Session()

This creates something called a constant op, or a constant added as a node in a graph, and saves its output to a variable greeting. The last line starts a TensorFlow session and then runs the op’s output. That means you’ll see Hello, Tensorflow printed to your console.

Installing TensorFlow on Linux

Finally, let’s look at how to install TensorFlow on Linux. Although there are many types of Linux distributions, we’ll focus on Ubuntu as that’s one of the most popular Linux distros for personal use. As long as pip and Python are available, the installation experience will be similar on other apt-based Linux distributions like Debian and, with minimal tweaks, on rpm-based distributions like Scientific Linux and OpenSUSE. 

You’ll first go to your Python interpreter and create a ./venv directory with the following command:

python3 -m venv --system-site-packages ./venv

How you activate your venv depends on the shell you’re using. If you’re using sh, bash or zsh, you’ll run:

source ./venv/bin/activate

If you’re using csh or tcsh, use the same command and concatenate .csh at the end of activate:

source ./venv/bin/activate.csh

As with other OSs, with Linux you can install packages in your venv with pip. Installing once your venv is activated allows you to use packages without changing your host system setup. Once you have an upgraded version of pip, use it to install TensorFlow:

pip install --upgrade tensorflow

Now we can use TensorFlow. Start by importing it into your program:

pip install --upgrade tensorflow

We’ll create a straightforward program that prints “Hello, TensorFlow” to the console. To get started, save the output of a constant op to a variable:

greeting = tf.constant('Hello, TensorFlow')

A constant op is a constant tensor added as a node in a graph. We’ll now start a session, which allows us to run the output of our constant op and see what happens.

sess = tf.Session()

After running this, you’ll see Hello, Tensorflow printed to your console.

Virtual Environment Tips

When you’re working in your venv, you can use pip list to list all the Python packages installed there. This is a handy way to remember what you’ve used without trying to reinstall or look up packages one by one.

Just as you activated your venv, you’ll also want to deactivate it with a simple command common to all OSs: 


Just a word of caution: Don’t run this command unless you’ve finished with your TensorFlow project.

Master TensorFlow With Udacity

Now that you know how to install TensorFlow in your venv, you’ll need to learn how to use it. If you have Python experience but lack training in TensorFlow, we’ve got you covered. By taking our Intro to Machine Learning with TensorFlow Nanodegree, you’ll get valuable TensorFlow experience to help you land your dream ML role.

Start Learning