天天看点

CentOS 7上安装Python 3.9CentOS 7上安装Python 3.9

CentOS 7上安装Python 3.9

最近一不小心把开发环境的虚机玩坏了,索性重新搭一套CentOS7上的新环境,顺便记录归档。

安装要求

  1. CentOS 7.9
  2. Python 3.9

安装步骤

下载文件

wget https://www.openssl.org/source/openssl-1.1.1d.tar.gz 
wget https://www.python.org/ftp/python/3.9.0/Python-3.9.0.tar.xz
           

准备环境

Centos 7可使用yum直接升级openssl-devel至1.0.2,正常可以直接装python3.9,如果无法导入ssl可采用下列编译安装openssl。

安装openssl

  1. 编译安装openssl
tar -zxvf openssl-1.1.1d.tar.gz
cd openssl-1.1.1d
#./config --prefix=/usr/local/openssl no-zlib      #不需要zlib
./config --prefix=/usr/local/openssl
           

显示结果

Operating system: x86_64-whatever-linux2
Configuring OpenSSL version 1.1.1d (0x1010104fL) for linux-x86_64
Using os-specific seed configuration
Creating configdata.pm
Creating Makefile

**********************************************************************
***                                                                ***
***   OpenSSL has been successfully configured                      ***
***                                                                ***
***   If you encounter a problem while building, please open an    ***
***   issue on GitHub <https://github.com/openssl/openssl/issues>  ***
***   and include the output from the following command:           ***
***                                                                ***
***       perl configdata.pm --dump                                 ***
***                                                                ***
***   (If you are new to OpenSSL, you might want to consult the    ***
***   'Troubleshooting' section in the INSTALL file first)           ***
***                                                                ***
**********************************************************************
           
make && make install && make clean
           
  1. 配置openssl
mv /usr/bin/openssl /usr/bin/openssl.bak
ln -s /usr/local/openssl/include/openssl /usr/include/openssl
ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl
ln -s /usr/local/openssl/lib/libssl.so.1.1 /usr/lib64/libssl.so.1.1
ln -s /usr/local/openssl/lib/libcrypto.so.1.1 /usr/lib64/libcrypto.so.1.1
# 直接添加link,否则装包的时候经常找不到库,比如装uwsgi
ln -s /usr/local/openssl/lib/libssl.so.1.1 /usr/local/lib64/libssl.so
ln -s /usr/local/openssl/lib/libcrypto.so.1.1 /usr/local/lib64/libcrypto.so
           
#  pip安装uwsgi时的报错信息。
    /usr/bin/ld: 找不到 -lssl
    /usr/bin/ld: 找不到 -lcrypto
    collect2: 错误:ld 返回 1
    *** error linking uWSGI ***
           
  1. 修改系统配置
#写入openssl库文件的搜索路径
echo "/usr/local/openssl/lib" >> /etc/ld.so.conf.d/openssl-x86_64.conf 
#使修改后的/etc/ld.so.conf生效
ldconfig -v
#查看所需的动态库
ldd /usr/local/openssl/bin/openssl
           
  1. 验证版本
openssl version -a
           
# 显示结果
OpenSSL 1.1.1d  10 Sep 2019
           
  1. 卸载系统安装openssl-devel以免版本冲突(如果系统已安装了)
yum remove openssl-devel
           

安装依赖包

yum -y install gcc gcc-c++ zlib-devel bzip2-devel  ncurses-devel sqlite-devel readline-devel tk-devel libffi-devel  expat-devel gdbm-devel  make
           

安装Python

  1. 准备文件
xz -d Python-3.9.0.tar.xz
tar -xf Python-3.9.0.tar
cd Python-3.9.0
           
  1. 编译安装
# 如果是编译安装了openssl,需要指定新版路径
./configure --prefix=/usr/local/python3.9 --with-openssl=/usr/local/openssl 
# 编译并安装 altinstall 不自动创建链接,需要手动创建,建议使用,保障多个版本共存。
make && make  altinstall 
make clean
make distclean
           
  1. 建立链接
ln -s /usr/local/python3.9/bin/python3.9 /usr/local/bin/
ln -s /usr/local/python3.9/bin/pip3.9 /usr/local/bin/
           
  1. 验证结果

显示结果

Python 3.9.0 (default, Nov 26 2020, 18:11:35) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl
>>> 
           

5、补充说明

之前在Centos6.8上安装Python3.8遇到过死活不能import ssl

参考了https://blog.csdn.net/weixin_30951743/article/details/99891139 .最后成功安装,留做备用信息。

# cd openssl-1.1.1d
# ./config --prefix=$HOME/openssl shared zlib   # 这里不同,装了shared zlib 和 $HOME路径
# make && make install

# echo "export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$HOME/openssl/lib" >> $HOME/.bash_profile
# source $HOME/.bash_profile
# cat $HOME/.bash_profile          

# cd Python-3.8.3
# ./configure --prefix=/usr/local/python3.8 --with-openssl=$HOME/openssl
# make && make  altinstall 
# make clean
# make distclean
           

修改PIP源

指定公司内部源(生产环境不能访问外网)

pip3.9 config set global.trusted-host 10.100.224.65 
pip3.9 config set global.index-url http://10.100.224.65/root/pypi/+simple/
pip3.9 config set search.index http://10.100.224.65/root/pypi/
           

或者直接修改文件也可以.

参考: https://www.cnblogs.com/cou1d/p/12403097.html.

mkdir ~/.pip
vim ~/.pip/pip.conf
           

增加以下内容

[global]
timeout = 6000  
index-url = https://pypi.douban.com/simple 
trusted-host = pypi.douban.com 
           

临时指定源安装(以豆瓣源为例)

pip3.9 install pygame -i https://pypi.doubanio.com/simple