說明
接上一篇。
【Linux】非root安裝Python3及其包管理
上一篇雖然成功安裝了Python3及一些常用的模塊,但因為一直裝不上SSL模塊,導致一些包無法安裝,嘗試了不少方法都失敗了(網上好多教程都是錯誤的,或者是只針對有root權限的用戶)。這次重新編譯Python3,並一次性解決SSL的問題。
升級Open SSL
首先刪除上次安裝好的python。上篇我們已知SSL模塊安裝失敗是因為系統自帶的OpenSSL版本太低,因此需要更新OpenSSL或者用LibreSSL來代替(后者我簡單嘗試了下,沒有加入環境變量失敗了,后續沒有再深究),所以我這里還是升級OpenSSL(官網:https://www.openssl.org/source/)。
wget -c https://www.openssl.org/source/openssl-1.1.1d.tar.gz
tar -xvf openssl-1.1.1d.tar.gz
cd openssl-1.1.1d
./config --prefix=/my/path/penssl-1.1.1d no-zlib #注意添加no-zlib
make && make install
常規的源碼編譯安裝方法,但需要特別注意配置環境的時候記得加no-zlib參數。
Python3安裝
wget -c https://www.python.org/ftp/python/3.8.1/Python-3.8.1.tgz
tar -xvf Python-3.8.1.tgz
cd Python-3.8.1
./configure --prefix=/my/path/python/
# 配置環境后先別急着編譯
配置環境后先別急着編譯,需要修改Modules/Setup文件,將以下四行取消注釋,並將SSL路徑修改。
SSL=/my/path/openssl #改為剛安裝的ssl路徑
_ssl _ssl.c \
-DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
-L$(SSL)/lib -lssl -lcrypto
修改完成后,此時如果直接make編譯,仍會報如下類似錯誤:
./python: error while loading shared libraries: libssl.so.1.1: cannot open shared object file: No such file or directory
generate-posix-vars failed
make: *** [pybuilddir.txt] Error 1
缺少庫文件,說明libssl.so.1.1這個庫沒有讀取到,網上其他回答都是通過建立軟鏈接來解決,但我沒有root權限,這些回答於我是浮雲。怎么辦?首先查看下openssl/lib下是否存在這個庫文件,然后將openssl庫加入環境變量即可:
export LD_LIBRARY_PATH=/my/path/openssl-1.1/lib:$LD_LIBRARY_PATH
所以,在我們修改Modules/Setup文件,並將openssl加入環境變量后,再進行編譯:
make && make install
安裝順利。試下SSL模塊是否可行:
Python 3.8.1 (default, Feb 13 2020, 10:49:45)
[GCC 4.9.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl
>>>
說明已經解決了,接下來安裝包等管理可參考上一篇。
