Programster's Blog

Tutorials focusing on Linux, programming, and open-source

Install TensorFlow For Python3

I sat down tonight after having set myself the goal of installing Tensorflow, thinking it might take me a few hours. It's actually its dead easy, so I wanted to post this tutorial to show just how easy it is. The docs list a million-and-one ways to install the tool but this just covers setting up with native Python3 on Ubuntu 16.04.

Steps

CPU-only Installation

Run the following commands/script to get tensorflow (CPU only) installed on your Ubuntu 16.04 server.

# Install necessary python packages.
sudo apt-get install python3-pip python3-dev -y

# Upgrade pipo this is 
pip3 install --upgrade pip

# Install tensorflow (cpu only)
pip3 install tensorflow

GPU Installation

If you want to use an NVIDIA GPU for tensorflow then run this instead:

# Install necessary python packages.
sudo apt-get install python3-pip python3-dev -y

# Upgrade pip
pip3 install --upgrade pip

# Install tensorflow with GPU support
pip3 install tensorflow-gpu

Testing Installation Successful

That seemed a little too easy, so let's just write a small script that uses the tensorflow library to test that we have installed it correctly:

#!/usr/bin/python3
import tensorflow as tf
hello = tf.constant("Hello, TensorFlow!")
sess = tf.Session()
print(sess.run(hello))

If you execute it you should get the following output :

b'Hello, TensorFlow!'

Conclusion

Now you have tensorflow installed. I will post more tutorials soon on using Tensorflow, but for now you can read the Python API docs.

Uninstall

If you ever need to uninstall tensorflow just run:

sudo pip3 uninstall tensorflow

References

Last updated: 25th March 2021
First published: 16th August 2018

This blog is created by Stuart Page

I'm a freelance web developer and technology consultant based in Surrey, UK, with over 10 years experience in web development, DevOps, Linux Administration, and IT solutions.

Need support with your infrastructure or web services?

Get in touch