Logo
活死人の行知路

Linux源码编译安装Python


📅 | 📝 233 字
#python

一、安装步骤

1、安装依赖

# CentOS
sudo yum install -y openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel gcc gcc-c++ libffi-devel mariadb-devel xz-devel tk-devel tcl-devel libuuid-devel

# Debian/Ubuntu
sudo apt-get install -y zlib1g-dev libbz2-dev libssl-dev libncurses5-dev default-libmysqlclient-dev libsqlite3-dev libreadline-dev tk-dev libgdbm-dev libdb-dev libpcap-dev xz-utils libexpat1-dev liblzma-dev libffi-dev libc6-dev tcl-dev uuid-dev

2、下载

wget https://www.python.org/ftp/python/3.13.0/Python-3.13.0.tar.xz

3、解压

解压到 /tmp:

tar xvf Python-3.13.0.tar.xz -C /tmp

4、编译

安装到 /usr/local/python3.13:

cd /tmp/Python-3.13.0/
./configure --enable-optimizations --prefix=/usr/local/python3.13
make -j 4

./configure 生成编译用的 Makefile,参数含义:

参数说明
--enable-optimizations启用 PGO 和 LTO 优化
--prefix=/usr/local/python3.13指定安装路径,不覆盖系统默认 Python
-j 44 个并行编译任务,按 CPU 核数调整

报错 bash: make: command not found 是缺少编译工具:

# CentOS
sudo yum install -y gcc gcc-c++ automake autoconf libtool make
# Debian/Ubuntu
sudo apt-get install -y gcc automake autoconf libtool make

5、安装

make altinstall
...
Successfully installed pip-24.2
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager, possibly rendering your system unusable.It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv. Use the --root-user-action option if you know what you are doing and want to suppress this warning.

make altinstall 是"替代安装",不会覆盖系统默认的 /usr/bin/python3。

6、设置软链接

python3 提示找不到:

# python3
bash: python3: command not found

建立软链接:

ln -s /usr/local/python3.13/bin/python3.13 /usr/bin/python3
ln -s /usr/local/python3.13/bin/pip3.13 /usr/bin/pip3

验证:

# python3
Python 3.13.0 (main, Nov  3 2024, 17:31:54) [GCC 8.5.0 20210514 (Red Hat 8.5.0-4)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

二、更新日志

  • 2024.11.04 更新 Python 版本为 3.13