If you don’t want to see the process, just skip to the final summary

Tests are available, but there may be omissions. Do not use CentOS8 directly in a production environment. CentOS8 is directly Python3+

The testing process

  • CentOS 7.7 1908 minimal # appears to be a mid-September 19 release
[root@wuxianfeng opt]# cat /etc/redhat-release 
CentOS Linux release 7.7.1908 (Core)
Copy the code
  • [Step 1] Take Python3.8.0 as an example. We directly download the corresponding version from the open source mirror site of Taobao
[root@wuxianfeng ~]# CD /opt # Switch to /opt directory [root@wuxianfeng opt]# [root@wuxianfeng opt]# wget https://npm.taobao.org/mirrors/python/3.8.0/Python-3.8.0.tar.xz just download processCopy the code
  • [Step 2] Decompress the package in the /opt directory
[root@wuxianfeng opt]# ll # total 17416 drwxr-xr-x. 17 1000 [root@wuxianfeng opt]# ll # total 17416 drwxr-xr-x 1000 4096 Oct 14 2019 python-3.8.0-rw-r --r-- 1 root root 17829824 Oct 18 2019 python-3.8.0.tar.xzCopy the code
  • Go to python-3.8.0 and run configure to set /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 detailsCopy the code

The first error is encountered here: no acceptable C compiler

The reason: a C compiler is missing, so we can install GCC

  • [Step 3] Install GCC
[root@wuxianfeng python-3.8.0]# yum -y install Loaded plugins: fastestmirror * base: mirrors.cn99.com * extras: mirrors.163.com * updates: mirrors.163.com ... GCC. X86_64 0:4.8.5-39. El7 Dependency Installed: Cpp.x86_64 0:4.8.5-39.el7 glibc-dev. 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-1177.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! # End it hereCopy the code
  • [Step 4] Run configure again and set the directory to /usr/local/python38
/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-optimizationsCopy the code
  • Compile and install
[root @ wuxianfeng Python - 3.8.0]# make  && make installgcc -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 ... Is omitted many word zipimport ZipImportError: can't decompress data; zlib not available During handling of the above exception, another exception occurred: Traceback (most recent call last): Py ", line 192, in _run_module_as_main return _run_code(code, main_globals, None, /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 
      
        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 "
       
        ", line 241, in load_module File "
        
         ", line 709, in _get_module_code File "
         
          ", line 570, in _get_data zipimport.ZipImportError: can'
         
        
       
      t decompress data; zlib not available
make: *** [install] Error 1
Copy the code

Here we encounter the second error, the lack of Zlib

  • [Step 5] Install all zlib-related packages
yum install zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl-devel xz xz-devel libffi-devel  # This is the method on the network, no validation.Yum -y install zlib*This is executed based on an error message
Copy the code
  • [Step 6] Compile and install again, this time reserving error messages and ignoring correct ones. But there are still some exceptions, so it shouldn’t hurt to look at 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                pwdtime 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 earlierdonot 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/idle3 from 644 to 755 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?

Copy the code
  • [Step 7] Verify installation
[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(a)Copy the code

Centos 2.7.5 is installed by default. Python3.8 is still installed on the system. # yum yum yum yum yum yum yum Yum Yum Yum Yum Yum Yum Yum Yum Yum Yum Yum Yum Yum Yum Yum Yum

All we need to do now is change the command to this new version

  • [Step 8] Modify the Python link and verify
[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(a)Copy the code

The Python installation is now complete

However, yum actually uses Python, and if it ends like this, yum will not work.

[root @ wuxianfeng python3.8]# yum install dos2unix
  File "/usr/bin/yum", line 30
    except KeyboardInterrupt, e:
                            ^
SyntaxError: invalid syntax
Copy the code

It turns out that yum calls python2.7, which is why we do not recommend removing python2.7

  • [Step 9] Fix yum
  1. Modify the first line of /usr/bin/yum and the first line of /usr/libexec/urlgrabber-ext-down
Turned out to be#! /usr/bin/pythonInstead of#! The/usr/bin/python2.7
Copy the code

At this point, yum’s environment has recovered.

conclusion

1. Download the Python installation source code

Wget npm.taobao.org/mirrors/pyt…

2. Decompress the package in a directory

cd /opt/

The tar XVF Python – 3.8.0. Tar. Xz

3. Install GCC

yum -y install gcc

4. Set the directory to /usr/local/python38

./configure –prefix=/usr/local/python38

5. Install zlib packages

yum -y install zlib*

There is also a saying on the Internet is to execute the following command, but not from the textual research

yum install zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl-devel xz xz-devel libffi-devel

6. Compile and install

make && make install

7. Create python executable links

Rm -f /usr/bin/python # delete old python2.7 symlinks

Ln -s/usr/local/python38 / bin/python3.8 / usr/bin/python

Fix the yum program

Modify the first line of /usr/bin/yum and the first line of /usr/libexec/urlgrabber-ext-down

Turned out to be#! /usr/bin/pythonInstead of#! The/usr/bin/python2.7
Copy the code

Said in the last

  • Again, it is important to note that python3.8 can be installed just for testing purposes, but there may be many associations in a real production environment and direct use in a production environment is not recommended.
  • For example, there are at least some possible versions of 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 # may be a problem and should also be fixed
pchrt:1:#! /usr/bin/python # may be a problem and should also be fixed
pflags:1:#! /usr/bin/python # may be a problem and should also be fixed
ptaskset:1:#! /usr/bin/python # may be a problem and should also be fixed
pydoc:1:#! The/usr/bin/python2.7 # just as well
systemd-sysv-convert:1:#! /usr/bin/python # may be a problem and should also be fixed
urlgrabber:1:#! /usr/bin/python -t #
yum:1:#! /usr/bin/python2.7 # changed
Copy the code