编译安装Python

下载解压python对应版本源码

wget https://www.python.org/ftp/python/3.7.8/Python-3.7.8.tgz
# 解压源码
tar -zxvf Python-3.7.8.tgz

依赖库安装


# Debian依赖库安装
sudo apt install git-core gnupg flex bison gperf build-essential zip curl zlib1g-dev gcc-multilib g++-multilib libc6-dev-i386 lib32ncurses5-dev x11proto-core-dev libx11-dev lib32z-dev ccache libgl1-mesa-dev libxml2-utils xsltproc unzip m4 -y
sudo apt-get install --reinstall zlibc zlib1g zlib1g-dev -y
sudo apt install libssl-dev libncurses5-dev libsqlite3-dev libreadline-dev libtk8.5 libgdm-dev libdb4o-cil-dev libpcap-dev libffi-dev libncursesw5-dev libgdbm-dev tk-dev openssl -y

# Termux ubuntu依赖库安装
sudo apt install git-core gnupg flex bison gperf build-essential zip curl zlib1g-dev    x11proto-core-dev libx11-dev ccache libgl1-mesa-dev libxml2-utils xsltproc unzip m4 -y
sudo apt-get install --reinstall zlibc zlib1g zlib1g-dev -y
sudo apt install libssl-dev libncurses5-dev libsqlite3-dev libreadline-dev libgdm-dev libdb4o-cil-dev libpcap-dev libffi-dev libncursesw5-dev libgdbm-dev tk-dev openssl -y

# CentOS依赖库安装
yum -y groupinstall "Development tools"
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
yum install -y libffi-devel zlib1g-dev
yum install zlib* -y

编译安装

cd Python-3.7.8/
# 使用系统库的ssl更便捷
sudo ./configure --prefix=/usr/local/python3.7.8 --with-ssl --enable-shared
# 编译安装
make install

添加 python 库配置信息

cd /etc/ld.so.conf.d
sudo vim python3.conf

# 加入下边这句
/usr/local/python3.7.8/lib/

# 更新一下
ldconfig

# 切换源为国内
python3 -m pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/

linux下的共享库机制采用了类似于高速缓存的机制,将库信息保存在/etc/ld.so.cache里边,程序连接的时候首先从这个文件里边查找,然后再到ld.so.conf的路径里边去详细找,这就是为什么修改了conf文件要重新运行一下ldconfig的原因。

创建软连接

sudo rm -rf /usr/bin/python3 /usr/bin/python
sudo ln -s /usr/local/python3.7.8/bin/python3 /usr/bin/python3
sudo ln -s /usr/local/python3.7.8/bin/python3 /usr/bin/python
sudo rm -rf /usr/bin/pip3 /usr/bin/pip
sudo ln -s /usr/local/python3.7.8/bin/pip3 /usr/bin/pip3
sudo ln -s /usr/local/python3.7.8/bin/pip3 /usr/bin/pip

找不到lsb_release模块

报错如下:

subprocess.CalledProcessError: Command '('lsb_release', '-a')' returned non-zero exit status 1.

找到lsb_release.py文件和CommandNotFound目录,把它们拷贝到报的错误中subprocess.py所在文件夹

sudo find / -name 'lsb_release.py'
sudo cp /usr/lib/python3/dist-packages/lsb_release.py /usr/local/python3.7.8/lib/python3.7/

   转载规则


《编译安装Python》 锦泉 采用 知识共享署名 4.0 国际许可协议 进行许可。
  目录