Guide to Install TensorFlow On Ubuntu

If you’re into machine learning, you might need to utilize TensorFlow, if not PyTorch.

Assuming that you’re using Linux, I am going to show you how to install Tensorflow on Ubuntu. I have used Ubuntu 18.04 but the steps should be valid for other versions as well.

Do note that while you can try building it from source for other platforms/distributions, TensorFlow primarily supports Ubuntu Linux. So, I’ll stick to that in this article.

Installing TensorFlow on Ubuntu Linux

Before you start setting up TensorFlow, you need to enable the Universe Repository on Ubuntu. You can do that using this command:

sudo add-apt-repository universe

Or go to the Software & Updates options and enable it from there:

Step 1: Get Python development environment

First, you have to set up a Python development environment to proceed. By default, you should have Python 3.6.x installed on Ubuntu. You can check Python version using:

python3 --version

Make sure you have Python 3.5–3.7 (as instructed officially). If you do not have it or need to upgrade it, install it using the following command in the terminal:

sudo apt install python3-dev

Step 2: Get Pip

Next, you need to check if you have Python’s pip package manager.

pip3 --version

If you do not have it installed, you can install it by typing this:

sudo apt install python3-pip

However, you may not have the latest version onboard.

Note: TensorFlow requires pip version 19.0 or above. Fret not, you can upgrade pip once you set up the virtual environment in the next set of steps.

Step 3: Set up Python virtual environment

Now, that you’re done with setting up Python and pip, you need to set up a virtual environment for Python development. This way, you won’t affect the host system with whatever you are doing with Python setup.

To get started, install virtualenv:

sudo -H pip3 install -U virtualenv

The -H flag sets the HOME environment variable to the home directory.

You would want to create the Python virtual environment by specifying a directory and choosing a Python interpreter. Here’s what you have to type in order to do that:

virtualenv --system-site-packages -p python3 ./venv

With that completed, you just need to activate the virtual environment, for that, use the source command in following fashion:

source ./venv/bin/activate

While your virtual environment is active, you will observe that your shell prompt will be prefixed with (venv) as shown in the image below.

Python Virtual Environment

Next, you need to upgrade the pip version in the virtual environment without affecting the host setup (if its already up-to-date you will be notified of that):

pip install --upgrade pip

If you are curious, you can also check out the list of packages installed in the virtual environment by typing this:

pip list

Step 4: Install TensorFlow

Finally you are about to install TensorFlow.

Make sure that you are using the virtual environment. Type in the following command to install TensorFlow:

pip install --upgrade tensorflow

You can also choose to verify the installation by typing in the following command in the virtual environment:

python -c "import tensorflow as tf;print(tf.reduce_sum(tf.random.normal([1000, 1000])))"

At last, you have installed TensorFlow successfully on Ubuntu!

Installing TensorFlow on Pop!_OS

Unlike Ubuntu, if you have Pop!_OS, you do not need to follow all these steps but a single command to utilize your base system python.

You just need to enter the following command in the terminal:

sudo apt install tensorflow-cuda-latest

For more info on how to use it, you can refer to the official Pop!_OS resources.

Wrapping Up

Keep in mind that whenever you want to use TensorFlow on Ubuntu, you have to enter the specified Python virtual environment first.

If you’re new to TensorFlow, you might want to check out the official resources available to learn.

I hope this helps you out. It is also worth noting that I performed the steps to install TensorFlow on a fresh Ubuntu installation – so if you have modified something on your installation, you might need troubleshoot a bit while installing TensorFlow on Ubuntu.

Feel free to let me know your thoughts in the comments below.