Install TensorFlow (python2.7 version) on Ubuntu

**Note content: Install TensorFlow (python2.7 version) on Ubuntu **Note date: 2018-01-31


**My system environment: **

**Two versions of TensorFlow: **

The main installation forms of TensorFlow are as follows:

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

The format of the pip installation package command is as follows:

pip install package name

The format of the pip uninstall software package command is as follows:

pip uninstall package name

I will use the virtualenv virtual environment for installation first, and then I will introduce the use of pip to install to the local system. The following is the official installation document of TensorFlow:

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

  1. Install python-pip, python-dev, python-virtualenv and other packages. I installed the python2.7 version here:
$ sudo apt-get install python-pip python-dev python-virtualenv
  1. Create a Virtualenv environment:
[ zero@zero-virtual-machine:~]$ virtualenv --system-site-packages tensorflow
Running virtualenv with interpreter /usr/bin/python2
New python executable in/home/zero/tensorflow/bin/python2
Also creating executable in/home/zero/tensorflow/bin/python
Installing setuptools, pkg_resources, pip, wheel...done.
  1. Activate the Virtualenv environment:
[ zero@zero-virtual-machine:~]$ source ~/tensorflow/bin/activate(tensorflow)[zero@zero-virtual-machine:~]$
  1. Install pip:
( tensorflow)[zero@zero-virtual-machine ~]$ easy_install -U pip
  1. Install tensorflow, what I installed here is the cpu version:
( tensorflow)[zero@zero-virtual-machine ~]$ pip install --upgrade tensorflow

Tip: If your pip installation is very slow, it is because the default is to use a foreign source, we can replace it with a domestic source:

[ zero@zero-virtual-machine ~]$ vim .pip/pip.conf  #Edit to the following
[ global]
index-url = http://pypi.douban.com/simple
trusted-host = pypi.douban.com   #Without this sentence, there will be a warning
disable-pip-version-check =true   #Version not checked
timeout =120   #Timeout setting

Note: If there is no .pip/pip.conf, just create it.

  1. After installation, enter the python command line and import the tensorflow package. If there is no output, the installation is successful:
( tensorflow)[zero@zero-virtual-machine ~]$ python
Python 2.7.12(default, Dec  42017,14:50:18)[GCC 5.4.020160609] on linux2
Type "help","copyright","credits" or "license"for more information.>>>import tensorflow
>>>

Some other operations:
The deactivate command can exit the virtual environment:

( tensorflow)[zero@zero-virtual-machine ~]$ deactivate
[ zero@zero-virtual-machine ~]$ 

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

[ zero@zero-virtual-machine ~]$ rm -rf tensorflow/[zero@zero-virtual-machine ~]$

Above we demonstrated the installation of tensorflow through virtualenv, and then we will demonstrate the installation of tensorflow through local pip:

  1. First you need to install pip and dev:
[ zero@zero-virtual-machine ~]$ sudo apt-get install python-pip python-dev
  1. Then use pip to install:
[ zero@zero-virtual-machine ~]$ pip install tensorflow
  1. After installation, enter the python command line and import the tensorflow package. If there is no output, the installation is successful:
[ zero@zero-virtual-machine ~]$ python
Python 2.7.12(default, Dec  42017,14:50:18)[GCC 5.4.020160609] on linux2
Type "help","copyright","credits" or "license"for more information.>>>import tensorflow
>>>

This method is installed on the local system, while the previous installation method is installed in a virtual environment. You need to enter the virtual environment every time to use tensorflow, and it is not necessary to install it on the local system.

Uninstall tensorflow using the following command:

sudo pip uninstall tensorflow

We can use pip to install some basic Python libraries:

pip install numpy
pip install pandas
pip install matplotlib

Write the first TensorFlow program: Hello World

Above we have installed TensorFlow, then we will write the first TensorFlow program: Hello World

  1. Create the corresponding directory:
[ zero@zero-virtual-machine ~]$ mkdir TensorFlow
[ zero@zero-virtual-machine ~]$ cd !$
cd TensorFlow
[ zero@zero-virtual-machine ~/TensorFlow]$ mkdir HelloWorld
[ zero@zero-virtual-machine ~/TensorFlow]$ cd !$
cd HelloWorld
[ zero@zero-virtual-machine ~/TensorFlow/HelloWorld]$ 
  1. Edit a python file:
[ zero@zero-virtual-machine ~/TensorFlow/HelloWorld]$ vi helloworld.py  #The content is as follows
# - *- coding: UTF-8-*-

# Introduce Tensorflow library
import tensorflow as tf

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

# Start a Tensorflow Session (session)
sess = tf.Session()

# Run Graph (compute graph)
print sess.run(hw)

# Close Session
sess.close()
  1. Run this file to see if the output is normal:
[ zero@zero-virtual-machine ~/TensorFlow/HelloWorld]$ python helloworld.py 
2018- 02- 0100: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![zero@zero-virtual-machine ~/TensorFlow/HelloWorld]$ 

As above, you can see that Hello Wolrd! is output normally, and some warning messages are printed. So far, our first TensorFlow program has been written.

The general meaning of this warning is: tensorflow thinks your computer cpu is OK, supports AVX (Advanced Vector Extensions), and the calculation speed can be improved, so you can open a better and faster mode, but the mode you are using may not be relatively So fast, so this is not actually an error, so if the current mode is not too slow, just ignore this warning.

If you don't want the output of this warning message, you can add these two sentences to the code:

import os

os.environ['TF_CPP_MIN_LOG_LEVEL']='2'

Then no warning message will be output.

Recommended Posts

Install TensorFlow (python2.7 version) on Ubuntu
Install Python3 on Ubuntu 14.04
Install Python3 on Ubuntu 16.04
Install Python3.7 on Ubuntu
Install the latest Python 3.6 version on Ubuntu
Install Python 3.7 on Ubuntu 18.04 LTS
ubuntu18.04 install python2
ubuntu12.04 install python3
Install Python virtual environment on Ubuntu 18.04
ubuntu18.04 install python
How to install Python2 on Ubuntu20.04 ubuntu/focal64
Use python3 to install third on ubuntu
install vscode on ubuntu18
Install Redis on Ubuntu
Install R4 on ubuntu20
Install nvtop on Ubuntu 18.04
Install postgresql-10 on Ubuntu 18.04
Install python3.6 under Ubuntu 16.04
Install docker on Ubuntu
Ubuntu16.04 switch python version
Install Docker on ubuntu18.04
Install nodejs10 on Ubuntu16
Install mysql on Ubuntu 14.04
Install Django on ubuntu
Install Pytorch+CUDA on Ubuntu 16.04
Install rJava on Ubuntu18
Install JDK10+ on Ubuntu
Install python in Ubuntu
Install KDE on Ubuntu16.04.2
Install Docker on Ubuntu18
python Tic-Tac-Toe-text version (on)
Install flashplayer on Ubuntu
Install the CPU version of Caffe on Ubuntu
[Practice] How to install python3.6 on Ubuntu system
Docker practice (1): install Docker on Ubuntu 16.04
ubuntu16.04 install caffe cpu version
Install OpenSSL 1.0.2 on Ubuntu Server 14.04
Install QQ robot on Ubuntu 16.04
Ubuntu install the latest Python 3.
ubuntu18.04 compile and install python3.8
Install Android Studio on Ubuntu
Install scrapy framework on ubuntu
Install Mysql offline on Ubuntu
Install Oracle Java8 on Ubuntu
Install Odoo 11 stack on Ubuntu 16.04
Install Go locale on Ubuntu16
Install common software on Ubuntu 16.04
Install non-portable QQ on ubuntu
Install Docker on Ubuntu 18.04 offline
How to install Ruby on Ubuntu 20.04
How to install Java on Ubuntu 20.04
How to install VirtualBox on Ubuntu 20.04
How to install Elasticsearch on Ubuntu 20.04
How to install Protobuf 3 on Ubuntu
Install Tenda u12 driver on Ubuntu
How to install Nginx on Ubuntu 20.04
How to install Apache on Ubuntu 20.04
How to install TensorFlow on CentOS 8
How to install Git on Ubuntu 20.04
How to install Node.js on Ubuntu 16.04
Install and configure MySQL on Ubuntu