尝试通过源码编译安装 python 的指定版本,本次安装的是 python3.8.5 版本

下载

官网的下载地址:https://www.python.org/downloads/release/python-385/

下载:Gzipped source tarball 源码压缩包

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

配置

解压后添加配置

1
tar -zxvf Python-3.8.5.tgz && cd Python-3.8.5/

修改配置文件,启用 SSL 支持

1
vim Modules/Setup

去掉如下几行注释

1
2
3
4
5
6
# Socket module helper for SSL support; you must comment out the other
# socket line above, and possibly edit the SSL variable:
SSL=/usr/local/ssl
_ssl _ssl.c \
-DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
-L$(SSL)/lib -lssl -lcrypto

提前配置好编需要的工具和依赖

1
apt update && apt install gcc make zlib1g-dev libssl-dev libsqlite3-dev  -y

指定安装位置,指定安装共享库,启用配置文件引导优化 (PGO) 和链接时间优化 (LTO)

(–enable-optimizations)会影响编译速度

1
./configure --prefix=/usr/local/python3.8.5 --enable-shared --enable-optimizations

编译

-j 当前主机的 cpu 核心数

1
make -j 4 && make install

建立软链

1
2
ln -s /usr/local/python3.8.5/bin/python3.8 /usr/bin/python3
ln -s /usr/local/python3.8.5/bin/pip3.8 /usr/bin/pip3

查看效果

1
2
root@debian:~# python3 --version
Python 3.8.5

遇到的问题

1
root@debian:~# python3

python3: error while loading shared libraries: libpython3.8.so.1.0: cannot open shared object file: No such file or directory

解决方法

1
cp /usr/local/python3.8.5/lib/libpython3.8.so.1.0 /usr/lib