如果不想看过程,就直接跳到最后的总结
测试可用,但可能还会有疏漏,切勿直接在生产环境中使用,CentOS8直接就是用Python3+版本了
测试过程
- 基于CentOS 7.7 1908 minimal #好像是19年9月中旬发布的版本
[root@wuxianfeng opt]# cat /etc/redhat-release
CentOS Linux release 7.7.1908 (Core)
- 【步骤1】此处以安装Python3.8.0为例,我们直接去淘宝的开源镜像站下载对应的版本
[root@wuxianfeng ~]# cd /opt #先切换到/opt目录下
[root@wuxianfeng opt]#
[root@wuxianfeng opt]# wget https://npm.taobao.org/mirrors/python/3.8.0/Python-3.8.0.tar.xz
下载过程略
- 【步骤2】在/opt目录下解压
[root@wuxianfeng opt]# tar -xvf Python-3.8.0.tar.xz
解压过程略
[root@wuxianfeng opt]# ll #
total 17416
drwxr-xr-x. 17 1000 1000 4096 Oct 14 2019 Python-3.8.0
-rw-r--r--. 1 root root 17829824 Oct 18 2019 Python-3.8.0.tar.xz
- 进入Python-3.8.0,执行configure,配置目录为/usr/local/python38
[root@wuxianfeng Python-3.8.0]# ./configure --prefix=/usr/local/python38
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking for python3.8... no
checking for python3... no
checking for python... python
checking for --enable-universalsdk... no
checking for --with-universal-archs... no
checking MACHDEP... "linux"
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/opt/Python-3.8.0':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details
此处遇到了第一个错误:no acceptable C compiler
原因是:缺少C编译器,我们可以安装gcc
- 【步骤3】安装gcc
[root@wuxianfeng Python-3.8.0]# yum -y install gcc
Loaded plugins: fastestmirror
Determining fastest mirrors
* base: mirrors.cn99.com
* extras: mirrors.163.com
* updates: mirrors.163.com
...此处省略好多字
Installed:
gcc.x86_64 0:4.8.5-39.el7
Dependency Installed:
cpp.x86_64 0:4.8.5-39.el7 glibc-devel.x86_64 0:2.17-307.el7.1
glibc-headers.x86_64 0:2.17-307.el7.1 kernel-headers.x86_64 0:3.10.0-1127.el7
libmpc.x86_64 0:1.0.1-3.el7 mpfr.x86_64 0:3.1.1-4.el7
Dependency Updated:
glibc.x86_64 0:2.17-307.el7.1 glibc-common.x86_64 0:2.17-307.el7.1
Complete! #看到这里就结束了
- 【步骤4】再次执行configure,配置目录为/usr/local/python38
[root@wuxianfeng Python-3.8.0]# ./configure --prefix=/usr/local/python38
checking for sys/uio.h... yes
checking sys/un.h usability... yes
checking sys/un.h presence... yes
...此处省略好多字
config.status: creating pyconfig.h
creating Modules/Setup.local
creating Makefile
If you want a release build with all stable optimizations active (PGO, etc),
please run ./configure --enable-optimizations
- 编译并安装
[root@wuxianfeng Python-3.8.0]# make && make install
gcc -pthread -c -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -Werror=implicit-function-declaration -I./Include/internal -I. -I./Include -DPy_BUILD_CORE -o Programs/python.o ./Programs/python.c
gcc -pthread -c -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -Werror=implicit-function-declaration -I./In
...此处省略很多字
zipimport.ZipImportError: can't decompress data; zlib not available
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/opt/Python-3.8.0/Lib/runpy.py", line 192, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/opt/Python-3.8.0/Lib/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/opt/Python-3.8.0/Lib/ensurepip/__main__.py", line 5, in <module>
sys.exit(ensurepip._main())
File "/opt/Python-3.8.0/Lib/ensurepip/__init__.py", line 200, in _main
return _bootstrap(
File "/opt/Python-3.8.0/Lib/ensurepip/__init__.py", line 119, in _bootstrap
return _run_pip(args + [p[0] for p in _PROJECTS], additional_paths)
File "/opt/Python-3.8.0/Lib/ensurepip/__init__.py", line 27, in _run_pip
import pip._internal
File "<frozen zipimport>", line 241, in load_module
File "<frozen zipimport>", line 709, in _get_module_code
File "<frozen zipimport>", line 570, in _get_data
zipimport.ZipImportError: can't decompress data; zlib not available
make: *** [install] Error 1
此处我们遇到了第二个错误,缺少zlib
- 【步骤5】安装所有zlib相关的包
yum install zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl-devel xz xz-devel libffi-devel #这个是网络上的方法,没有验证。
或者
yum -y install zlib* #这个是根据错误信息执行的
- 【步骤6】再次编译安装,这次把错误信息保留,正确的就忽略了。但仍然有一些异常,看info应该无妨。
[root@wuxianfeng Python-3.8.0]# make && make install >/dev/null
CC='gcc -pthread' LDSHARED='gcc -pthread -shared ' OPT='-DNDEBUG -g -fwrapv -O3 -Wall' _TCLTK_INCLUDES='' _TCLTK_LIBS='' ./python -E ./setup.py build
running build
running build_ext
INFO: Can't locate Tcl/Tk libs and/or headers
Python build finished successfully!
The necessary bits to build these optional modules were not found:
_dbm _gdbm _hashlib
_ssl _tkinter _uuid
To find the necessary bits, look in setup.py in detect_modules() for the module's name.
The following modules found by detect_modules() in setup.py, have been
built by the Makefile instead, as configured by the Setup files:
_abc atexit pwd
time
Could not build the ssl module!
Python requires an OpenSSL 1.0.2 or 1.1 compatible libssl with X509_VERIFY_PARAM_set1_host().
LibreSSL 2.6.4 and earlier do not provide the necessary APIs, https://github.com/libressl-portable/portable/issues/381
running build_scripts
copying and adjusting /opt/Python-3.8.0/Tools/scripts/pydoc3 -> build/scripts-3.8
copying and adjusting /opt/Python-3.8.0/Tools/scripts/idle3 -> build/scripts-3.8
copying and adjusting /opt/Python-3.8.0/Tools/scripts/2to3 -> build/scripts-3.8
changing mode of build/scripts-3.8/pydoc3 from 644 to 755
changing mode of build/scripts-3.8/idle3 from 644 to 755
changing mode of build/scripts-3.8/2to3 from 644 to 755
renaming build/scripts-3.8/pydoc3 to build/scripts-3.8/pydoc3.8
renaming build/scripts-3.8/idle3 to build/scripts-3.8/idle3.8
renaming build/scripts-3.8/2to3 to build/scripts-3.8/2to3-3.8
/tmp/tmpdimz5_72/pip-19.2.3-py2.py3-none-any.whl/pip/_vendor/ipaddress.py:1106: SyntaxWarning: 'str' object is not callable; perhaps you missed a comma?
/tmp/tmpdimz5_72/pip-19.2.3-py2.py3-none-any.whl/pip/_vendor/ipaddress.py:1106: SyntaxWarning: 'str' object is not callable; perhaps you missed a comma?
- 【步骤7】验证安装
[root@wuxianfeng bin]# whereis python
python: /usr/bin/python /usr/bin/python2.7 /usr/lib/python2.7 /usr/lib64/python2.7 /etc/python /usr/include/python2.7 /usr/share/man/man1/python.1.gz
[root@wuxianfeng bin]# ll /usr/bin/python
lrwxrwxrwx. 1 root root 7 Apr 12 12:35 /usr/bin/python -> python2
[root@wuxianfeng bin]# python
Python 2.7.5 (default, Aug 7 2019, 00:51:29)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
从上述结果可以看出来,centos默认安装的是2.7.5,我们虽然安装了python3.8但系统中仍然是老版本。(此时可能会有人说,是不是要先卸载,不是的,因为yum依赖于python,所以卸载的话,依赖于python的程序可能都会有麻烦,所以我们是不建议卸载后再装新的的,类似于venv的意思,可以多环境共存。
我们现在要做的是把命令改到这个新版本即可
- 【步骤8】修改python的链接,并验证
[root@wuxianfeng bin]# cd /usr/bin
[root@wuxianfeng bin]#
[root@wuxianfeng bin]# ln -s /usr/local/python38/bin/python3.8 python
[root@wuxianfeng bin]# python
Python 3.8.0 (default, May 3 2020, 21:47:27)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
至此python的安装是结束了
但是yum其实是用到了python,如果这样结束,yum其实是不能用了。
[root@wuxianfeng python3.8]# yum install dos2unix
File "/usr/bin/yum", line 30
except KeyboardInterrupt, e:
^
SyntaxError: invalid syntax
原来yum调用了python2.7,这也是我们不建议删除python2.7的缘故
- 【步骤9】修复yum程序
- 修改/usr/bin/yum的第一行,以及/usr/libexec/urlgrabber-ext-down的第一行
原来是
#!/usr/bin/python
改为
#!/usr/bin/python2.7
至此,yum的环境也恢复好了。
总结
1. 下载python安装源码
2. 在某个目录下解压
cd /opt/
tar -xvf Python-3.8.0.tar.xz
3. 安装gcc
yum -y install gcc
4. 配置目录为/usr/local/python38
./configure --prefix=/usr/local/python38
5. 安装zlib相关包
yum -y install zlib*
网上也有说法是执行下面的命令,但未从考证
yum install zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl-devel xz xz-devel libffi-devel
6. 编译并安装
make && make install
7. 创建python的可执行程序链接
rm -f /usr/bin/python #删除老的python2.7的符号链接
ln -s /usr/local/python38/bin/python3.8 /usr/bin/python
8. 修复yum程序
修改/usr/bin/yum的第一行,以及/usr/libexec/urlgrabber-ext-down的第一行
原来是
#!/usr/bin/python
改为
#!/usr/bin/python2.7
说在最后
- 还是要注意,仅仅是测试,可以进行python3.8的安装,但实际生产环境中可能会有很多关联,不建议直接在生产环境中使用。
- 比如还有至少这些是可能用到python2.7的
[root@wuxianfeng bin]# grep -ni '/usr/bin/python' *
firewall-cmd:1:#!/usr/bin/python2 -Es #无妨
firewall-offline-cmd:1:#!/usr/bin/python2 -Es #无妨
msghack:1:#!/usr/bin/python #可能有问题,也要修复
pchrt:1:#! /usr/bin/python #可能有问题,也要修复
pflags:1:#! /usr/bin/python #可能有问题,也要修复
ptaskset:1:#! /usr/bin/python #可能有问题,也要修复
pydoc:1:#!/usr/bin/python2.7 #无妨
systemd-sysv-convert:1:#!/usr/bin/python #可能有问题,也要修复
urlgrabber:1:#!/usr/bin/python -t #可能有问题,也要修复
yum:1:#!/usr/bin/python2.7 #已修改