Install TensorFlow in Python2.7 in Ubuntu 16.04

Source: Internet
Author: User
Tags virtual environment virtualenv install pandas install matplotlib

Install TensorFlow in Python2.7 in Ubuntu 16.04

My system environment:

  • Ubuntu 16.04 LTS
  • Python 1, 2.7
  • Python 1, 3.5

Two TensorFlow versions:

TensorFlow is installed in the following ways:

  • Virtualenv
  • Pip
  • Docker
  • Anaconda
  • Source code compilation

Pip is the Python software package management system: Pip Install Packages recursive abbreviation

The Command Format of pip installation package is as follows:

Pip install package name

The Command Format of pip uninstall package is as follows:

Pip uninstall package name

I will first use the virtualenv virtual environment for installation. I will introduce the installation of pip to the local system later. The following is the official TensorFlow installation documentation:

Https://tensorflow.google.cn/install/install_linux

1. Install python-pip, python-dev, python-virtualenv, and other packages. Here I am installing python 2.7:

$ Sudo apt-get install python-pip python-dev python-virtualenv

2. Create a Virtualenv environment:

[Bkjia @ zero-virtual-machine: ~] $ Virtualenv -- system-site-packages tensorflow
Running virtualenv with interpreter/usr/bin/python2
New python executable in/home/bkjia/tensorflow/bin/python2
Also creating executable in/home/bkjia/tensorflow/bin/python
Installing setuptools, pkg_resources, pip, wheel... done.

3. Activate the Virtualenv environment:

[Bkjia @ zero-virtual-machine: ~] $ Source ~ /Tensorflow/bin/activate
(Tensorflow) [bkjia @ zero-virtual-machine: ~] $

4. Install pip:

(Tensorflow) [bkjia@bkjia.net ~] $ Easy_install-U pip

5. Install tensorflow. Here I install the cpu version:

(Tensorflow) [bkjia@bkjia.net ~] $ Pip install -- upgrade tensorflow

Tip: If your pip installation is slow, it is because it is a foreign source by default. We can change it to a domestic source:

Bkjia@bkjia.net ~ $ Vim. pip/pip. conf # edit the following content:
[Global]
Index-url = http://pypi.douban.com/simple
Trusted-host = pypi.douban.com # without this statement, the package is warning.
Disable-pip-version-check = true # version not checked
Timeout = 120 # timeout settings

NOTE: If. pip/pip. conf does not exist, create it.

6. After the installation is complete, go to the python command line and import the tensorflow package. If there is no output, the installation is successful:

(Tensorflow) [bkjia@bkjia.net ~] $ Python
Python 2.7.12 (default, Dec 4 2017, 14:50:18)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> Import tensorflow
>>>

Other operations:
The deactivate command can exit the virtual environment:

(Tensorflow) [bkjia@bkjia.net ~] $ Deactivate
Bkjia@bkjia.net ~ $

To delete a virtual environment, you only need to delete the generated directory:

Bkjia@bkjia.net ~ $ Rm-rf tensorflow/
Bkjia@bkjia.net ~ $

The above demonstrates how to install tensorflow through virtualenv, and then demonstrates how to install tensorflow through local pip:

1. First install pip and dev:

Bkjia@bkjia.net ~ $ Sudo apt-get install python-pip python-dev

2. Then use pip for installation:

Bkjia@bkjia.net ~ $ Pip install tensorflow

3. After the installation is complete, enter the python command line to import the tensorflow package. If there is no output, the installation is successful:

Bkjia@bkjia.net ~ $ Python
Python 2.7.12 (default, Dec 4 2017, 14:50:18)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> Import tensorflow
>>>

This method is installed on the local system, and the previous installation method is installed in a virtual environment. tensorflow can be used only when you enter the virtual environment every time, you do not need to install it on the local system.

Run the following command to uninstall tensorflow:

Sudo pip uninstall tensorflow

We can use pip to install some basic Python class libraries:

Pip install numpy
Pip install pandas
Pip install matplotlib

Write the first TensorFlow program: Hello World

We have installed TensorFlow above, so we will compile the first TensorFlow program: Hello World.

1. Create the corresponding directory:

Bkjia@bkjia.net ~ $ Mkdir TensorFlow
Bkjia@bkjia.net ~ $ Cd! $
Cd TensorFlow
Bkjia@bkjia.net ~ /TensorFlow] $ mkdir HelloWorld
Bkjia@bkjia.net ~ /TensorFlow] $ cd! $
Cd HelloWorld
Bkjia@bkjia.net ~ /TensorFlow/HelloWorld] $

2. edit a python file:

Bkjia@bkjia.net ~ /TensorFlow/HelloWorld] $ vi helloworld. py # The content is as follows:
#-*-Coding: UTF-8 -*-

# Introduce the Tensorflow Library
Import tensorflow as tf

# Create a constant Operation (Operation)
Hw = tf. constant ("Hello Wolrd! ")

# Start A Tensorflow Session)
Sess = tf. Session ()

# Run Graph (computing Graph)
Print sess. run (hw)

# Closing a Session)
Sess. close ()

3. Run this file to check whether the output is normal:

Bkjia@bkjia.net ~ /TensorFlow/HelloWorld] $ python helloworld. py
00:22:43. 680173: I tensorflow/core/platform/cpu_feature_guard.cc: 137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
Hello Wolrd!
Bkjia@bkjia.net ~ /TensorFlow/HelloWorld] $

As shown above, we can see Hello Wolrd! It is output normally, and some warning messages are printed. So far our first TensorFlow program has been compiled.

This warning roughly means that tensorflow thinks your computer's cpu is okay, supports AVX (Advanced Vector Extensions), and the computing speed can be improved, so you can enable a better and faster mode, however, the current mode may not be so fast, so this is not an error. If you do not suspect that the current mode is slow, just ignore this warning.

If you do not want the output of this warning message, you can add the following two sentences to the Code:

Import OS

OS. environ ['tf _ CPP_MIN_LOG_LEVEL '] = '2'

Then, no warning information is output.

This article permanently updates link: https://www.bkjia.com/Linux/2018-03/151188.htm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.