天天看點

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