本次博客的目的是在CentOS6.X和7.X版本上源码安装Python3和人工智能深度学习框架Tensorflow,其中的部分过程是为了演示升级操作,方便以后所需,所以可能有些繁琐(或者有些没必要),但了解它是很有用处的。
[hadoop@s1 ~]$ python -V # 注意是大写的V
Python 2.6.6
[hadoop@s1 ~]$ gcc -v # 注意是小写的v
bash: gcc: command not found
[hadoop@s1 ~]$ sudo yum install -y gcc zlib* openssl-devel
主要是升级了c编译器
在编译之前需要安装一些必须的依赖,否则当报错的时候还得重新编译
[hadoop@s1 ~]$ gcc -v # 注意是小写的v
Using built-in specs.
Target: x86_64-redhat-linux
……
gcc version 4.4.7 20120313 (Red Hat 4.4.7-23) (GCC)
~/downloads
目录,如果没有该目录请使用命令创建或手动创建python3.6
~/downloads/Python-3.6.4.tgz
到家目录下的python3.6
目录:在这里插入代码片:[hadoop@s1 ~]$ tar -zxvf downloads/Python-3.6.4.tgz -C python3.6
/usr/local/python3
:cd python3.6/Python-3.6.4 #注意前一个python的p是小写后一个是大写
./configure --prefix=/usr/local/python3 --with-ssl
make //编译
sudo make install //安装,必须加sudo
[hadoop@s1 Python-3.6.4]$ cd /usr/bin
[hadoop@s1 bin]$ sudo ln -s /usr/local/python3/bin/python3.6 python3
测试:
[hadoop@s1 bin]$ python3
Python 3.6.4 (default, Aug 30 2019, 14:08:43)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-23)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
可供pip安装源:
阿里云 https://mirrors.aliyun.com/pypi/simple/
中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
豆瓣(douban) https://pypi.douban.com/simple/
清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/
中国科学技术大学 http://pypi.mirrors.ustc.edu.cn/simple/
执行以下命令升级:
[hadoop@s1 Python-3.6.4]$ cd /usr/local/python3/bin
[hadoop@s1 bin]$ sudo ./pip3 install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple/
cd /usr/local/python3/bin
sudo ./pip3 install ~/downloads/tensorflow-1.1.0rc1-cp36-cp36m-manylinux1_x86_64.whl -i https://mirrors.aliyun.com/pypi/simple/
也可以使用pip install tensorflow命令安装,这种方式会安装最新版的
升级原因:
进入python环境,导入tensorflow的包
>>> import tensorflow as tf
Traceback (most recent call last):
File "/usr/local/python3/lib/python3.6/site-packages/tensorflow/python/pywrap_tensorflow.py", line 41, in
from tensorflow.python.pywrap_tensorflow_internal import *
File "/usr/local/python3/lib/python3.6/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 28, in
_pywrap_tensorflow_internal = swig_import_helper()
File "/usr/local/python3/lib/python3.6/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 24, in swig_import_helper
_mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
File "/usr/local/python3/lib/python3.6/imp.py", line 243, in load_module
return load_dynamic(name, filename, file)
File "/usr/local/python3/lib/python3.6/imp.py", line 343, in load_dynamic
return _load(spec)
ImportError: /lib64/libc.so.6: version `GLIBC_2.14' not found (required by /usr/local/python3/lib/python3.6/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so)
通过最后一句话可以看出运行这个版本的tensorflow需要GIBC_2.14
解压到当前压缩包所在的~/downloads
目录
执行以下命令:
cd ~/downloads/glibc-2.17
mkdir build //在glibc-2.17目录下建立build文件夹
cd build
../configure --prefix=/usr --disable-profile --enable-add-ons --with-headers=/usr/include --with-binutils=/usr/bin
make
sudo make install
上述步骤安装完成后,再次进入python环境测试:
import tensorflow as tf
Traceback (most recent call last):
File "/usr/local/python3/lib/python3.6/site-packages/tensorflow/python/pywrap_tensorflow.py", line 41, in
from tensorflow.python.pywrap_tensorflow_internal import *
File "/usr/local/python3/lib/python3.6/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 28, in
_pywrap_tensorflow_internal = swig_import_helper()
File "/usr/local/python3/lib/python3.6/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 24, in swig_import_helper
_mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
File "/usr/local/python3/lib/python3.6/imp.py", line 243, in load_module
return load_dynamic(name, filename, file)
File "/usr/local/python3/lib/python3.6/imp.py", line 343, in load_dynamic
return _load(spec)
ImportError: /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.14' not found (required by /usr/local/python3/lib/python3.6/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so)
如果仍然报错,且出现的错误为:ImportError: /usr/lib64/libstdc++.so.6: version GLIBCXX_3.4.14 not found (required by /usr/local/python3/lib/python3.6/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so)
执行以下步骤:
sudo cp ~/downloads/libstdc++.so.6.0.20 /usr/lib64
cd /usr/lib64
#删除原来软连接:
sudo rm -rf libstdc++.so.6
#将默认库的软连接指向最新动态库:
sudo ln -s libstdc++.so.6.0.20 libstdc++.so.6
进入python环境,输入:
import tensorflow as tf
sess = tf.Session()
hello=tf.constant('Hello,Tensorflow!')
print(sess.run(hello))
[hadoop@s1 ~]$ python -V
Python 2.7.5
[hadoop@s1 ~]$ gcc -v
bash: gcc: command not found...
[hadoop@s1 ~]$ sudo yum install -y gcc zlib* openssl-devel
[hadoop@s1 ~]$ tar -zxvf downloads/Python-3.6.4.tgz -C python3.6
cd python3.6 #小写的p
cd Python-3.6.4 #大写的P
./configure --prefix=/usr/local/python3 --with-ssl
make //编译
sudo make install //安装,必须加sudo
[hadoop@s1 Python-3.6.4]$ cd /usr/bin
[hadoop@s1 bin]$ sudo ln -s /usr/local/python3/bin/python3.6 python3
这样就可以在任何目录下执行命令python3
:
[hadoop@s1 bin]$ python3
Python 3.6.4 (default, Aug 30 2019, 14:08:43)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-23)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
cd /usr/local/python3/bin
sudo ./pip3 install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple/
[hadoop@s1 bin]$ sudo ./pip3 install ~/downloads/tensorflow-1.1.0rc1-cp36-cp36m-manylinux1_x86_64.whl -i https://mirrors.aliyun.com/pypi/simple/
进入python3
import tensorflow as tf
sess = tf.Session()
hello=tf.constant('Hello,Tensorflow!')
print(sess.run(hello))
安装成功!!!