How to Install TensorFlow on Ubuntu 16.04 or 18.04

This post will guide you how to install and use TensorFlow on your Ubuntu Linux 18.04 or 16.04. How do I install TensorFlow Machine Learning Library in Python Virtual Environment or in a Docker container on Ubuntu or Debian system.

What is TensorFlow?


TensorFlow is an end-to-end open source platform for machine learning. And it can be used for machine learning applications such as neural networks. It has a comprehensive, flexible ecosystem of tools, libraries and community resources that lets researchers push the state-of-the-art in ML and developers easily build and deploy ML powered applications. It is developed by Google to do search ranking in their machine learning system.

TensorFlow can be installed in a Python virtual environment, or in a Docker container.

Prerequisites


  • You need to have a non-root user with sudo privileges so that you can install packages in your Ubuntu system.
  • Python 3.3 or higher version
  • Git installed

Method1: Installing TensorFlow with Python3 Virtual Environment


Step1: You need to make sure that your default APT index is up to date by running the following command:

$ sudo apt update
$ sudo apt upgrade -y

Step2: install Python3 and Python venv with the following command:

$ sudo apt install python3
$ sudo apt install python3-pip
$ sudo apt install python3-venv

Outputs:

devops@devops-VirtualBox:~/tensorflow_env$ sudo apt install python3-pip python3-venv
[sudo] password for devops:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
linux-headers-4.18.0-15 linux-headers-4.18.0-15-generic linux-image-4.18.0-15-generic linux-modules-4.18.0-15-generic
linux-modules-extra-4.18.0-15-generic
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
dh-python libexpat1-dev libpython3-dev libpython3.6-dev python-pip-whl python3-dev python3-distutils python3-lib2to3
python3-setuptools python3-wheel python3.6-dev python3.6-venv
Suggested packages:
python-setuptools-doc
The following NEW packages will be installed:
dh-python libexpat1-dev libpython3-dev libpython3.6-dev python-pip-whl python3-dev python3-distutils python3-lib2to3
python3-pip python3-setuptools python3-venv python3-wheel python3.6-dev python3.6-venv
0 upgraded, 14 newly installed, 0 to remove and 9 not upgraded.
Need to get 47.8 MB of archives.
After this operation, 84.4 MB of additional disk space will be used.
Do you want to continue? [Y/n]Y
Unpacking python3-wheel (0.30.0-0.2) ...
Setting up python-pip-whl (9.0.1-2.3~ubuntu1.18.04.1) ...
Setting up python3.6-venv (3.6.8-1~18.04.1) ...
Setting up python3-wheel (0.30.0-0.2) ...
Setting up libexpat1-dev:amd64 (2.2.5-3) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Setting up python3-lib2to3 (3.6.8-1~18.04) ...
Setting up python3-distutils (3.6.8-1~18.04) ...
Setting up python3-venv (3.6.7-1~18.04) ...
Setting up libpython3.6-dev:amd64 (3.6.8-1~18.04.1) ...
Setting up python3-pip (9.0.1-2.3~ubuntu1.18.04.1) ...
Setting up python3-setuptools (39.0.1-2) ...
Setting up python3.6-dev (3.6.8-1~18.04.1) ...
Setting up dh-python (3.20180325ubuntu2) ...
Setting up libpython3-dev:amd64 (3.6.7-1~18.04) ...
Setting up python3-dev (3.6.7-1~18.04) ...

Once Python3 is installed on your system, you can verify what version of Python is installed with the following command:

$ python3 -V

Outputs:

devops@devops-VirtualBox:~$ python3 -V
Python 3.6.8

Step3: Create a Python Virtual Environment called tensorflow_env with the following command:

$ mkdir tensorflow_env
$ cd tensorflow_env
$ python3 -m venv my_tensorflow

The above command will create a new my_tensorflow directory which will contain all of the packages that you install while this python virtual environment is activated.
Outputs:

devops@devops-VirtualBox:~/tensorflow_env$ python3 -m venv my_tensorflow
devops@devops-VirtualBox:~/tensorflow_env$ ls
my_tensorflow
devops@devops-VirtualBox:~/tensorflow_env$ ls my_tensorflow/
bin include lib lib64 pyvenv.cfg share

Then you need to activate this virtual environment to use it by running the following activate script, type:

$ source my_tensorflow/bin/activate

Once your virtual environment is activated, you should see something similar to the below:

devops@devops-VirtualBox:~/tensorflow_env$ source my_tensorflow/bin/activate
(my_tensorflow) devops@devops-VirtualBox:~/tensorflow_env$

Step4: you can install TensorFlow on your virtual environment, just run the following command to install and upgrade to the latest version of TensorFlow, type:

$ pip3 install –upgrade tensorflow

Outputs:

(my_tensorflow) devops@devops-VirtualBox:~/tensorflow_env$ pip3 install --upgrade tensorflow
Collecting tensorflow
Downloading https://files.pythonhosted.org/packages/de/f0/96fb2e0412ae9692dbf400e5b04432885f677ad6241c088ccc5fe7724d69/tensorflow-1.14.0-cp36-cp36m-manylinux1_x86_64.whl (109.2MB)
100% |████████████████████████████████| 109.2MB 11kB/s
……
Successfully installed absl-py-0.7.1 astor-0.8.0 gast-0.2.2 google-pasta-0.1.7 grpcio-1.21.1 h5py-2.9.0 keras-applications-1.0.8 keras-preprocessing-1.1.0 markdown-3.1.1 numpy-1.16.4 protobuf-3.8.0 setuptools-41.0.1 six-1.12.0 tensorboard-1.14.0 tensorflow-1.14.0 tensorflow-estimator-1.14.0 termcolor-1.1.0 werkzeug-0.15.4 wheel-0.33.4 wrapt-1.11.2

If you want to verify the installation if it is correct, just run the following command to print the TensorFlow version:

$ python -c 'import tensorflow as tf; print(tf.__version__)'

Outputs:

(my_tensorflow) devops@devops-VirtualBox:~/tensorflow_env$ python -c 'import tensorflow as tf; print(tf.__version__)'
1.14.0

That’s it! You have installed TensorFlow successfully on your Ubuntu system.

Method2: Installing TensorFlow with Docker


You can also install TensorFlow in a docker container. And you need to install docker firstly on your Ubuntu system. Then download the TensorFlow image file in your Docker container. just do the following steps:

Step1: Downloading TensorFlow Docker image with the following command:

$ sudo docker pull tensorflow/tensorflow

Step2: once TensorFlow docker image is downloaded, and you can run this image with the following command:

$ docker run -it -p 8888:8888 tensorflow/tensorflow

If you want to learn more about running TensorFlow in Docker from its official website.

Method3: Installing TensorFlow with Native Pip3


You can also install the TensorFlow with native pip3, just run the following command:

$ pip3 install tensorflow

Outputs:

devops@devops-VirtualBox:~$ pip3 install tensorflow
Collecting tensorflow
Using cached https://files.pythonhosted.org/packages/de/f0/96fb2e0412ae9692dbf400e5b04432885f677ad6241c088ccc5fe7724d69/tensorflow-1.14.0-cp36-cp36m-manylinux1_x86_64.whl
Collecting keras-preprocessing>=1.0.5 (from tensorflow)
Using cached https://files.pythonhosted.org/packages/28/6a/8c1f62c37212d9fc441a7e26736df51ce6f0e38455816445471f10da4f0a/Keras_Preprocessing-1.1.0-py2.py3-none-any.whl
Collecting tensorboard<1.15.0,>=1.14.0 (from tensorflow)
……
Building wheels for collected packages: wrapt, absl-py, termcolor, gast
Running setup.py bdist_wheel for wrapt ... done
Stored in directory: /home/devops/.cache/pip/wheels/d7/de/2e/efa132238792efb6459a96e85916ef8597fcb3d2ae51590dfd
Running setup.py bdist_wheel for absl-py ... done
Stored in directory: /home/devops/.cache/pip/wheels/ee/98/38/46cbcc5a93cfea5492d19c38562691ddb23b940176c14f7b48
Running setup.py bdist_wheel for termcolor ... done
Stored in directory: /home/devops/.cache/pip/wheels/7c/06/54/bc84598ba1daf8f970247f550b175aaaee85f68b4b0c5ab2c6
Running setup.py bdist_wheel for gast ... done
Stored in directory: /home/devops/.cache/pip/wheels/5c/2e/7e/a1d4d4fcebe6c381f378ce7743a3ced3699feb89bcfbdadadd
Successfully built wrapt absl-py termcolor gast
Installing collected packages: six, numpy, keras-preprocessing, setuptools, protobuf, markdown, wheel, grpcio, werkzeug, absl-py, tensorboard, wrapt, google-pasta, termcolor, gast, astor, tensorflow-estimator, h5py, keras-applications, tensorflow
Successfully installed absl-py-0.7.1 astor-0.8.0 gast-0.2.2 google-pasta-0.1.7 grpcio-1.21.1 h5py-2.9.0 keras-applications-1.0.8 keras-preprocessing-1.1.0 markdown-3.1.1 numpy-1.16.4 protobuf-3.8.0 setuptools-41.0.1 six-1.12.0 tensorboard-1.14.0 tensorflow-1.14.0 tensorflow-estimator-1.14.0 termcolor-1.1.0 werkzeug-0.15.4 wheel-0.33.4 wrapt-1.11.2

Creating Simple TensorFlow Program


Once the installation is completed, you can check if your TensorFlow is in running condition or not. So you can write a simple hello world code called testTensorFlow.py with vim text editor. Like below:

$ sudo vim testTensorFlow.py
#!/bin/python3
import tensorflow as tf
hello = tf.constant("Hello, world!")
session = tf.Session()
print(session.run(hello))

Save and close the file, then executing this python file with the following commad:

$ python3 testTensorFlow.py

Outputs:

devops@devops-VirtualBox:/$ python3 testTensofFlow.py
WARNING: Logging before flag parsing goes to stderr.
W0620 11:17:07.181209 140157137254208 deprecation_wrapper.py:119] From testTensofFlow.py:5: The name tf.Session is deprecated. Please use tf.compat.v1.Session instead.

2019-06-20 11:17:07.221507: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
2019-06-20 11:17:07.228039: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 1800000000 Hz
2019-06-20 11:17:07.228397: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x31f2990 executing computations on platform Host. Devices:
2019-06-20 11:17:07.228558: I tensorflow/compiler/xla/service/service.cc:175] StreamExecutor device (0): ,
b'Hello, world!'

Conclusion


You should know that how to install TensorFlow on your Ubuntu 18.04 or Debian Linux. If you want to see more detailed information about TensorFlow, you can directly go to its official web site.

You might also like:

Sidebar



back to top