[Python] 在 Ubuntu/CentOS 上編譯支援 ctypes 與 ssl 的 Python 3.9

[Python] 在 Ubuntu/CentOS 上編譯支援 ctypes 與 ssl 的 Python 3.9

今天想建立一個基於 Ubuntu 16.04 的 Docker 映象檔,

並且在裡面安裝 Python 3.9,

想說就從 Python 原始碼來編譯:

wget https://www.python.org/ftp/python/3.9.2/Python-3.9.2.tgz
tar xvf Python-3.9.2.tgz
cd Python-*
./configure --enable-optimizations
make -j2
make install

 

結果在 make 的最後階段,出現了無法編譯 ctypes 和 ssl 模組的訊息:

Python build finished successfully!
......
Failed to build these modules:
_ctypes

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 earlier do not provide the necessary APIs, https://github.com/libressl-portable/portable/issues/381

 

這樣的 Python 3 會無法使用 ctypes,

還會因為缺少 ssl,導致 pip3 無法建立 HTTPS 連線來下載新的 Python 模組…

 

查了一下,在 Ubuntu 上要:

  1. 安裝 libffi-dev 來編譯 ctypes 模組
  2. 安裝 libssl-dev 來編譯 ssl 模組
sudo apt install libffi-dev libssl-dev

 

這樣子編譯出來的 Python 3 就可以使用 ctypes 與 ssl 模組了:

root@22ae96119706:/Python-3.9.2# python3
Python 3.9.2 (default, Feb 24 2021, 16:23:22)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ctypes
>>> import ssl
>>>

 

備註:如果是在 CentOS 系列,改為安裝 libffi-devel 與 openssl-devel 即可。

 

參考資料:

如何在Ubuntu上安裝OpenSSL庫? – Ubuntu問答

Failed to build these modules: _ctypes – Python tracker

(本頁面已被瀏覽過 601 次)

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料