文章

Linux下编译安装Python

环境准备

本文将采用ubuntu:latest的Docker镜像作为编译环境,理论上适用于所有ubuntu系统,另外其他的Linux系统可能也只有安装编译依赖所用的指令有所区别,大差不差。

启动docker镜像

1
2
docker run -itd --name python-compile ubuntu:latest /bin/bash
docker exec -it python-compile /bin/bash

更换apt源

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
cat <<'EOF' > /etc/apt/sources.list
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb http://mirrors.cernet.edu.cn/ubuntu/ bionic main restricted universe multiverse
# deb-src http://mirrors.cernet.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.cernet.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
# deb-src http://mirrors.cernet.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.cernet.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
# deb-src http://mirrors.cernet.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
# 以下安全更新软件源包含了官方源与镜像站配置,如有需要可自行修改注释切换
# deb http://mirrors.cernet.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
# # deb-src http://mirrors.cernet.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb http://security.ubuntu.com/ubuntu/ bionic-security main restricted universe multiverse
# deb-src http://security.ubuntu.com/ubuntu/ bionic-security main restricted universe multiverse
# 预发布软件源,不建议启用
# deb http://mirrors.cernet.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
# # deb-src http://mirrors.cernet.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
EOF

更新apt源

1
apt update

安装基本的编译依赖

1
apt install curl build-essential libssl-dev libffi-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev libncursesw5-dev libgdbm-dev libnss3-dev liblzma-dev -y

下载Python源码

这里以Python 3.11.9为例,其余版本从Python官网获取Gzipped source tarball文件

下载源码到本地

1
curl -O https://www.python.org/ftp/python/3.11.9/Python-3.11.9.tgz

解压文件

1
tar -xf Python-*.tgz

编译

进入Python源码目录

这里目录改成自己实际的目录

1
cd Python-3.9.19

设置编译参数

1
./configure --enable-optimizations --enable-shared

编译

别问j8是什么参数,我也不知道,肯定不是j8的意思

1
make -j8

不出意外的话,就不会出意外,可以看到类似下面的编译完成输出。 img.png

安装

1
make altinstall

Clip_2024-07-31_13-58-10.png 这个指令会自动将Python安装到当前系统中。 此时运行python3.11会发现提示了错误: Clip_2024-07-31_14-10-02.png

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

这是因为Python默认安装到了/usr/local/bin目录下,而系统的LD_LIBRARY_PATH没有包含这个目录,所以需要手动添加。

1
2
3
4
mkdir -p /usr/local/python3/lib
cp libpy* /usr/local/python3/lib
echo "/usr/local/python3/lib" /etc/ld.so.conf.d/py3.conf
ldconfig

验证

1
python3.11 --version

img.png

增加软链接

1
2
3
4
ln -sf /usr/local/bin/python3.11 /usr/bin/python
ln -sf /usr/local/bin/python3.11 /usr/bin/python3
ln -sf /usr/local/bin/pip3.11 /usr/bin/pip
ln -sf /usr/local/bin/pip3.11 /usr/bin/pip3

为pip更换镜像源

1
2
pip config set global.index-url https://mirrors.cernet.edu.cn/pypi/web/simple
python -m pip install --upgrade pip

清理编译环境

1
2
3
4
rm -rf Python-*.tgz Python-*
apt remove curl build-essential libssl-dev libffi-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev libncursesw5-dev libgdbm-dev libnss3-dev liblzma-dev -y
apt autoremove -y
apt clean
本文由作者按照 CC BY 4.0 进行授权