linux 编译安装 python2.7

方案一:安装缺失库文件

Debian/Ubuntu系统

1sudo apt install python2.7 libpython2.7-dev

RHEL/CentOS系统

1sudo yum install python27 python27-python-libs

注意:
libpython2.7-dev 、 python27-python-libs 并不是必须的安装包。
这两个安装包是 python 的动态链接库,因为在Linux系统中运行Python 2.7相关程序时,常遇到:
error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file
错误,此时就需要安装这些共享库。

方案二:源码编译安装

下载Python 2.7源码

1wget https://www.python.org/ftp/python/2.7.18/Python-2.7.18.tar.xz
2tar -xf Python-2.7.18.tar.xz
3cd Python-2.7.18

编译配置

1./configure --enable-shared --enable-unicode=ucs4 --prefix=/app/programs/python2.7 CFLAGS="-fPIC"
2make
3sudo make install

其中:

  • –enable-openssl 启用OpenSSL支持。
  • –enable-unicode=ucs4 启用UCS-4编码的Unicode支持(默认ucs2),支持更广字符集。
  • –enable-shared 启用共享库的构建,便于其他模块链接。
  • –prefix=PATH 指定安装根目录(如/usr/local/python2),避免与系统Python冲突。
  • –enable-optimizations 启用编译优化,提升性能但增加编译时间。
  • –with-threads 启用线程支持,确保多线程程序正常运行。‌
  • –enable-ipv6 启用IPv6支持。

复制共享库文件到标准路径 (可选)

1sudo cp /app/programs/python2.7/lib/libpython2.7.so.1.0 /lib/x86_64-linux-gnu/
2sudo ln -s /lib/x86_64-linux-gnu/libpython2.7.so.1.0 /lib/x86_64-linux-gnu/libpython2.7.so