Ubuntu 16.04 installs TensorFlow (CPU only, Python 3.5) from source code

Keywords: Python Attribute github pip

What a big pit! Run quickly! Let's jump over!

TensorFlow Source Download https://github.com/tensorflow/tensorflow
By the way, poke your hand into the link of the installation guide: https://www.tensorflow.org/install/
Select the corresponding system, using Ubuntu 16.04: https://www.tensorflow.org/install/install_linux
In view of the previous use of three-dimensional reconstruction software to choose GPU acceleration is always stuck, this choice only CPU installation, and from the source code installation, the next official start!

  • Source download
  • Dependent Library Installation
  • Environmental configuration
  • TensorFlow Compilation and Installation

- 1. Source Download

Please go straight ahead. https://github.com/tensorflow/tensorflow download the cross section, do not use the GIT clone command, because some files from git clone to local have not been updated.
After downloading, extract the folder and place it in the home directory. Type the following command:

cd tensorflow
git checkout r1.0

- 2. Dependent Library Installation

  • bazel
  • python correlation

- 2.1 bazel Library Installation

Follow the instructions on this website. https://docs.bazel.build/versions/master/install-compile-source.html
bazel is still installed from the source code.

1. Download bazel: https://github.com/bazelbuild/bazel/releases
Select the following version (up to date):

Download is very slow. It is recommended to download at the right time (e.g. in the morning).

2.bazel dependency Libraries

sudo apt-get install build-essential openjdk-8-jdk python zip unzip

3. Compiling bazel
Open the terminal, enter the bazel directory, and type bash. / compile. sh
When you're done, there's a thoughtful little tip to tell you where the binary Bazel file you get is (wow, the warmest library has been installed!)
The binary file is in the output folder in the Bazel directory, and we want to move it to the / usr/local/bin directory.
Use root privileges to achieve.

su root
mv output/bazel /usr/local/bin

- 2.2 python related library installation

sudo apt-get install python3-numpy python3-dev python3-pip python3-wheel

Note here that since I have two versions of Python 2 and Python 3 in my system, the default Python 2 does not work properly after installing pip (even though I installed pip under 2 and 3).
Error display ImportError: cannot import name'main'Barabara
Looking at the Internet has given me many other complex solutions, which have no effect on me.
I'm going to change the default python version of the system to 3.5 and install only python 3-pip.
There are many ways to modify the default python version. I choose to modify it directly at the system level.
P for S: python --version to see the default Python version

su root
update-alternatives --list python

Error prompt: update-alternatives: error: no alternatives for python
This is because the alternative version of Python has not yet been identified by the update-alternatives command. We can update the alternative list and put Python 2 and python 3 in it.

update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1 

update-alternatives --install /usr/bin/python python /usr/bin/python3.5 2

Every time you type a command, you are prompted: update-alternatives: use / usr / bin / Python 2.7 to provide / usr / bin / Python (python) in auto mode

Priority is higher than 2, so the default version is changed to Python 3.5
Exit root again, python --version checks the default version and finds that it has been switched to 3.5-

- 3. TensorFlow environment configuration

cd tensorflow  
$ ./configure

Then there will be a lot of questions for you to choose Yes or No
I don't install GPU modules here, and I don't want all kinds of complex cloud platforms or anything, just the first Yes, and then I choose No.
Maybe it's as follows. Wrong can exit the reconfiguration, so don't panic!
To use the default value, return directly.

Please specify the location of python. [Default is /usr/bin/python]: /usr/bin/python3.5
Found possible Python library paths:
  /usr/local/lib/python3.5/dist-packages
  /usr/lib/python3.5/dist-packages
Please input the desired Python library path to use.  Default is [/usr/lib/python3.5/dist-packages]

Using python library path: /usr/local/lib/python3.5/dist-packages
Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native]:
Do you wish to use jemalloc as the malloc implementation? [Y/n]
jemalloc enabled
Do you wish to build TensorFlow with Google Cloud Platform support? [y/N]

Do you wish to build TensorFlow with Hadoop File System support? [y/N]

Do you wish to build TensorFlow with the XLA just-in-time compiler (experimental)? [y/N]

Do you wish to build TensorFlow with VERBS support? [y/N]

Do you wish to build TensorFlow with OpenCL support? [y/N]

Do you wish to build TensorFlow with CUDA support? [y/N] Y

Configuration finished

- 4. TensorFlow Compilation and Installation

4.1 compilation

bazel build -c opt //tensorflow/tools/pip_package:build_pip_package 

The process was slow, and I compiled it for several hours. You can type and eat.

When I came back from dinner, I found that I had compiled and made no mistakes (stealing a picture).

4.2 Generating Executable Files

bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg 

4.3 Installation of TensorFlow

cd /tmp/tensorflow_pkg
sudo pip install tensorflow-1.9.0rc0-cp35-cp35m-linux_x86_64.whl 

Note that by typing tensorflow - and then pressing TAB, you will see your version of tensorflow, which is error-free.

However, the installation process did not go smoothly, and the following mistakes occurred. (I don't know how long the installation process lasted. I slept anyway and missed a message from a favorite boy in the process of falling asleep. When I went back, the other side said it was all right. Hum! Pig hoof! You don't need to be single any longer if you take the initiative!

I have to say that I have become a little expert in Bug search. (Although I can't understand those red errors TAT)
In google search: response.py ", line 226, in_error_catcher yield
The solution was found. ( https://www.cnblogs.com/xianhan/p/9084838.html)
Estimate is the source of the problem, let's use mirrors to install it.
Replace the above command with the following: uuuuuuuuuuuu

 sudo pip install tensorflow-1.9.0rc0-cp35-cp35m-linux_x86_64.whl  -i https://pypi.douban.com/simple


So far, TensorFlow has been installed.
Test it:

$ python

Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
>>> sess.run(hello)
'Hello, TensorFlow!'
>>> a = tf.constant(10)
>>> b = tf.constant(32)
>>> sess.run(a + b)
42
>>> sess.close()

References:
https://github.com/tensorflow/tensorflow
https://www.tensorflow.org/install/
https://blog.csdn.net/Linux1s1s/article/details/77720846
https://www.cnblogs.com/xianhan/p/9084838.html

Posted by sy-co on Sun, 16 Dec 2018 19:18:03 -0800