参考
http://xiejava.ishareread.com/posts/57cef505/
https://hujiekang.top/2019/11/01/centos-python3/

环境的搭建是进行开发的第一步,python因为存在python2和python3两个版本,让在建立python环境时造成不便,并且由于在Linux环境下不像Window环境安装那么友好,存在一些小坑。本教程记录了CentOS7下安装python3.8的过程和注意事项。

一、查看系统版本

  1. [root@localhost ~]# cat /etc/centos-release
  2. CentOS Linux release 7.2.1511 (Core)
  1. [root@localhost ~]# uname -a
  2. Linux localhost.localdomain 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

查看python版本

  1. [root@localhost ~]# python -V
  2. Python 2.7.5

系统默认安装了Python 2.7.5

二、安装依赖

  1. yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make

如果有提示一路选择Y就可以

三、下载python源码包

python官网https://www.python.org/ 目前python最新版本是python3.10
CentOS7下安装python3.8 - 图1

下载稳定版本3.8版

  1. wget https://www.python.org/ftp/python/3.8.12/Python-3.8.12.tgz

四、解压安装python源码包

1、解压

  1. tar -zxvf Python-3.8.12.tgz

2、安装

进入解压后的目录进行编译和安装

  1. cd Python-3.8.12/
  2. [root@localhost Python-3.8.12]#
  1. # --enable-shared 启用共享,方便其他依赖python的一些内置库的资源的正常安装
  2. # --enable-optimizations 优化性能有10%
  3. [root@localhost Python-3.8.12]# ./configure --enable-shared --enable-optimizations
  4. [root@localhost Python-3.8.12]# make altinstall

打开上述选项在编译的时候会启动下面两项最佳化:

  • Profile guided optimization (PGO):又称之为profile-directed feedback (PDF)或是feedback-directed optimization (FDO)。像是Windows用的Firefox或是google Chrome在编译的时候都有加入这个最佳化选项以提高执行时的效能
  • Link Time Optimization (LTO):在将object档案组合而成程式的时候做得最佳化

看网路上面的说法是Python code可以加快大概10-20%,代价是要花约莫半小时在编译上。

执行完后显示安装成功
CentOS7下安装python3.8 - 图2

ssl 支持

  1. I encountered the problem when installing 3.8.11. I tried the above posts but they didn't work.
  2. This is how I finally made it compile:
  3. 1) Install openssl 1.1.1g;
  4. 2) untar the tarball;
  5. 3) enter the source directory;
  6. 4) run ./config --prefix=/usr/local/openssl_1.1 --openssldir=/usr/local/openssl_1.1
  7. to install to /usr/local/openssl_1.1.
  8. make & make install.
  9. You can customize the directory.
  10. 5)Download python 3.8 source tarball;
  11. 6)untar the tarball. Enter the source/Modules;
  12. 7) edit the file Setup;
  13. 8) find the lines:
  14. SSL=/usr/local/ssl
  15. ssl _ssl.c \
  16. -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
  17. -L$(SSL)/lib -lssl -lcrypto
  18. change SSL=/usr/local/ssl to SSL=/usr/local/openssl_1.1
  19. 9) Switch to the source directory. run
  20. ./configure --prefix=/usr/local/python3.8 --with-openssl=/usr/local/openssl_1.1
  21. 10) export LD_LIBRARY_PATH=/usr/local/openssl_1.1/lib:$LD_LIBRARY_PATH
  22. 11) make & make install. If you need sudo, also export LD_LIBRARY_PATH in sudo

3、建立命令软链接

python

虽然python3.8.12安装成功了,但默认输入python还是显示是2.7版本的。如果要用python3.8.12需要输入python3即可,有时候不太方便。可以通过修改软链接的方式将默认的python指向python3.8.12。
先看一下默认的python及新安装的python3都安装在哪里

  1. [root@localhost]# which python
  2. /usr/bin/python
  3. [root@localhost]# which python3
  4. /usr/local/bin/python3
  5. [root@localhost]# ln -s /usr/local/python3/bin/python3.8 /usr/bin/python3
  6. [root@localhost]# ln -s /usr/local/python3/bin/pip3.8 /usr/bin/pip3
  7. [root@localhost]# python --verison
  8. [root@localhost]# pip -V

提示动态库问题

  1. [root@localhost]# python3 --version
  2. python3: error while loading shared libraries: libpython3.8.so.1.0:cannot open shared object file: No such file or directory
  3. [root@localhost]# find / -name libpython3.8.so.1.0
  4. /usr/local/python3/lib/libpython3.8.so.1.0
  5. [root@localhost]# cp /usr/local/python3/lib/libpython3.6m.so.1.0 /usr/lib/
  6. or
  7. [root@localhost]# cp /usr/local/python3/lib/libpython3.6m.so.1.0 /usr/lib64/

五、配置yum

安装python3改完软链接以后发现yum命令报错了,yum是依赖python2.7的,你把python改成了3.8了,所以报错了。

  1. [root@localhost Python-3.8.12]# yum
  2. File "/usr/bin/yum", line 30
  3. except KeyboardInterrupt, e:
  4. ^
  5. SyntaxError: invalid syntax

可以修改yum里对python2的依赖即可。虽然安装了python3但是系统里python2依旧还在系统里,可以通过python2来指定用python2.7的命令,首先来看下python2的命令在哪里

  1. [root@localhost ~]# which python2
  2. /usr/bin/python2

可以cd到/usr/bin目录下 通过ls -alh|grep python查看python命令的详细情况。

  1. [root@localhost bin]# ls -alh|grep python

CentOS7下安装python3.8 - 图3
可以看到python软连接是执行的python3命令,python2是执行的python2.7的命令

  1. vi /usr/libexec/urlgrabber-ext-down

修改对python的依赖,修改成python2或python2.7都可以。
CentOS7下安装python3.8 - 图4

  1. vi /usr/bin/yum

CentOS7下安装python3.8 - 图5

修改完这两个文件后,再敲yum命令就不会报错了。

至此CentOS7环境下python3.8.12已经成功安装!

作者博客:http://xiejava.ishareread.com/

自动安装脚本

原文 https://blog.slogra.com/post-767.html

  1. #!/bin/bash
  2. PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
  3. export PATH
  4. version="3.8.13"
  5. echo "Please enter the version number you need:"
  6. read -p "(Default version: 3.8.13):" version
  7. if [ "$version" = "" ]; then
  8. version="3.8.13"
  9. fi
  10. name="Python"
  11. pyfile="$name-$version.tgz"
  12. check_ver=$(echo $version | awk '{printf ("%3.1f\n",$1)}')
  13. py3_dir="/usr/local/python3"
  14. function check_login_user() {
  15. # Check if user is root
  16. if [ "$(id -u)" != "0" ]; then
  17. echo "Error: You must be root to run this script, please use root to initialization OS."
  18. exit 1
  19. fi
  20. }
  21. function install_dev_tools() {
  22. rpm=$(rpm -qa libffi-devel | awk -F "-" '{print $1}')
  23. if [ -z "$rpm" ]; then
  24. yum -y install wget gcc gcc-c++ make openssl-devel bzip2-devel libffi-devel
  25. else
  26. echo -e "\033[40;31m libffi [found]\033[40;37m"
  27. fi
  28. }
  29. function uninstall_python3() {
  30. if [ -e "${py3_dir}" ]; then
  31. #卸载pyhton3
  32. rpm -qa | grep python3 | xargs rpm -ev --allmatches --nodeps
  33. #删除所有残余文件
  34. whereis python3 | xargs rm -frv
  35. else
  36. echo -e "\033[40;31m python [no found]\033[40;37m"
  37. fi
  38. }
  39. function install_python3() {
  40. if [ -s "${pyfile}" ]; then
  41. echo -e "\033[40;31m $pyfile [found]\033[40;37m"
  42. else
  43. # 使用华为镜像
  44. # https://mirrors.huaweicloud.com/python/3.8.13/Python-3.8.13.tgz
  45. wget https://mirrors.huaweicloud.com/python/$version/$pyfile -O /tmp/$pyfile
  46. cd /tmp && tar zxf $pyfile
  47. fi
  48. cd /tmp/${name}-${version} || exit
  49. ./configure --prefix=/usr/local/python3 --enable-optimizations
  50. make altinstall
  51. if [ "$check_ver" = "3.7" ]; then
  52. ln -s /usr/local/python3/bin/python3.7 /usr/bin/python3
  53. ln -s /usr/local/python3/bin/pip3.7 /usr/bin/pip3
  54. elif [ "$check_ver" = "3.8" ]; then
  55. ln -s /usr/local/python3/bin/python3.8 /usr/bin/python3
  56. ln -s /usr/local/python3/bin/pip3.8 /usr/bin/pip3
  57. fi
  58. }
  59. function set_pip_config() {
  60. if [ -d /root/.pip ]; then
  61. echo -e "\033[40;31m file is [found]\033[40;37m"
  62. else
  63. mkdir ~/.pip
  64. cat >~/.pip/pip.conf <<EOF
  65. [global]
  66. index-url = https://mirrors.aliyun.com/pypi/simple/
  67. [install]
  68. trusted-host=mirrors.aliyun.com
  69. disable-pip-version-check = true
  70. timeout = 6000
  71. EOF
  72. fi
  73. }
  74. function upgrade_pip3() {
  75. pip3 install --upgrade pip
  76. echo -e "\nInstalled Python and pip version is ... "
  77. python3 -V && pip3 -V
  78. echo -e "\033[32m \nInstall Successfully! \033[0m"
  79. }
  80. function main() {
  81. check_login_user
  82. install_dev_tools
  83. uninstall_python3
  84. install_python3
  85. set_pip_config
  86. upgrade_pip3
  87. }
  88. main