CentOS 下用用源代码安装Python3.10

我的购买的云服务器最近要到期了,续费费用涨了很多,于是想着把之前买的小主机收拾出来作为服务器。装上CentOS 7.9, 然后安装各种软件。。。

安装Python 的时候,以前都是下载编译好的二进制文件安装的,这次想着用源代码安装一下试试,没想到遇到很多问题,于是记录一下,希望能帮到有需要的朋友。

下载Python3.10源代码(因为目前3.10是长期支持版):

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

解压缩,并进到解压缩出来的目录:

tar -xf Python-3.11.x.tar.xz
cd Python-3.11.x

运行   ./configure   脚本来配置 Python 的安装选项:

./configure --enable-optimizations --prefix=/usr/local

         •  --enable-optimizations:启用优化,包括字节码优化和多线程编译。

        •   --prefix=/usr/local:指定安装路径为 /usr/local,这样不会覆盖系统默认的Python,通常是 Python 2.7 或其他版本,某些系统工具可能依赖它,因此建议不要覆盖安装。

make 时出现错误:

。。。。
gcc -pthread -fPIC -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -Werror=implicit-function-declaration -fvisibility=hidden -I./Include/internal -I./Include -I. -I/usr/local/include -I/root/Python-3.10.16/Include -I/root/Python-3.10.16 -c /root/Python-3.10.16/Modules/_ctypes/_ctypes.c -o build/temp.linux-x86_64-3.10/root/Python-3.10.16/Modules/_ctypes/_ctypes.o -DPy_BUILD_CORE_MODULE
/root/Python-3.10.16/Modules/_ctypes/_ctypes.c:107:17: 致命错误:ffi.h:没有那个文件或目录
 #include <ffi.h>
                 ^
编译中断。

The following modules found by detect_modules() in setup.py, have been
built by the Makefile instead, as configured by the Setup files:
_abc                  pwd                   time               


Failed to build these modules:
_ctypes               _hashlib              _ssl               


Could not build the ssl module!
Python requires a OpenSSL 1.1.1 or newer

running build_scripts
creating build/scripts-3.10
copying and adjusting /root/Python-3.10.16/Tools/scripts/pydoc3 -> build/scripts-3.10
copying and adjusting /root/Python-3.10.16/Tools/scripts/idle3 -> build/scripts-3.10
copying and adjusting /root/Python-3.10.16/Tools/scripts/2to3 -> build/scripts-3.10
changing mode of build/scripts-3.10/pydoc3 from 644 to 755
changing mode of build/scripts-3.10/idle3 from 644 to 755
changing mode of build/scripts-3.10/2to3 from 644 to 755
renaming build/scripts-3.10/pydoc3 to build/scripts-3.10/pydoc3.10
renaming build/scripts-3.10/idle3 to build/scripts-3.10/idle3.10
renaming build/scripts-3.10/2to3 to build/scripts-3.10/2to3-3.10
/usr/bin/install -c -m 644 ./Tools/gdb/libpython.py python-gdb.py
gcc -pthread -c -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall    -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -Werror=implicit-function-declaration -fvisibility=hidden  -I./Include/internal  -I. -I./Include    -DPy_BUILD_CORE -o Programs/_testembed.o ./Programs/_testembed.c
gcc -pthread     -Xlinker -export-dynamic -o Programs/_testembed Programs/_testembed.o libpython3.10.a -lcrypt -lpthread -ldl  -lutil -lm   -lm 
sed -e "s,@EXENAME@,/usr/local/bin/python3.10," < ./Misc/python-config.in >python-config.py
LC_ALL=C sed -e 's,\$(\([A-Za-z0-9_]*\)),\$\{\1\},g' < Misc/python-config.sh >python-config
[root@AE Python-3.10.16]#

从错误信息来看,编译 Python 3.10 时遇到了两个问题:

        1. 缺少 ffi.h 文件:这是因为libffi库的头文件未安装。

        2. 缺少 OpenSSL 1.1.1 或更高版本:Python 3.10 需要较新的 OpenSSL 版本来编译 ssl 模块。

先安装libffi-devel :

yum install libffi-devel

再安装OpenSSL:

先看一下已有的openssl版本:

[root@AE Python-3.10.16]# openssl version
OpenSSL 1.0.2k-fips  26 Jan 2017

 是 1.0.2的,果然是太旧了。

但是CentOS 7 默认的仓库中没有 OpenSSL 1.1.1 或更高版本,因此需要从其他仓库安装。这里使用 Software Collection (SCL) 安装 OpenSSL 1.1.1:

yum install centos-release-scl

下载OpenSSL源码:Downloads | OpenSSL Library 
我选择了3.0版本(因为它是一个长期支持版):

wget https://github.com/openssl/openssl/releases/download/openssl-3.0.15/openssl-3.0.15.tar.gz

tar -xzvf openssl-3.0.15.tar.gz

cd openssl-3.0.15

 配置并编译 OpenSSL:

# 如过没有 /usr/local/ssl目录,先创建: mkdir /usr/local/ssl  

./config --prefix=/usr/local/ssl --openssldir=/usr/local/ssl shared zlib
make
sudo make install

 又出错了:

[root@AE openssl-3.0.15]# ./config --prefix=/usr/local/ssl --openssldir=/usr/local/ssl shared zlib
Can't locate IPC/Cmd.pm in @INC (@INC contains: /root/openssl-3.0.15/util/perl /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 . /root/openssl-3.0.15/external/perl/Text-Template-1.56/lib) at /root/openssl-3.0.15/util/perl/OpenSSL/config.pm line 19.
BEGIN failed--compilation aborted at /root/openssl-3.0.15/util/perl/OpenSSL/config.pm line 19.
Compilation failed in require at /root/openssl-3.0.15/Configure line 23.
BEGIN failed--compilation aborted at /root/openssl-3.0.15/Configure line 23.

 问题出在 OpenSSL 的配置脚本无法找到 Perl 的   IPC::Cmd   模块。这是一个 Perl 的标准模块,通常应该包含在默认的 Perl 安装中。

首先检查 Perl 是否安装:

[root@AE openssl-3.0.15]# perl --version

This is perl 5, version 16, subversion 3 (v5.16.3) built for x86_64-linux-thread-multi
(with 44 registered patches, see perl -V for more detail)

Copyright 1987-2012, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl".  If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.

  Perl 已经安装了,那就安装IPC::Cmd模块:

yum install perl-IPC-Cmd

 出错了:

[root@AE openssl-3.0.15]# yum install perl-IPC-Cmd
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Could not retrieve mirrorlist http://mirrorlist.centos.org?arch=x86_64&release=7&repo=sclo-rh error was
14: curl#6 - "Could not resolve host: mirrorlist.centos.org; 未知的错误"

 还得更改yum源:

# 配置CentOS-SCLo-scl.repo 和 CentOS-SCLo-scl-rh.repo
# 修改命令:vi /etc/yum.repos.d/CentOS-SCLo-scl.repo
# 修改内容:
[CentOS-SCLo-scl]
name=CentOS-SCLo-scl
baseurl=https://mirrors.aliyun.com/centos/7.9.2009/sclo/x86_64/sclo/
enabled=1
gpgcheck=0

[CentOS-SCLo-scl-rh]
name=CentOS-SCLo-scl-rh
baseurl=https://mirrors.aliyun.com/centos/7.9.2009/sclo/x86_64/rh
enabled=1
gpgcheck=0

# 然后清空缓存和创建yum源元数据缓存,命令:
yum clean all && yum makecache

然后再安装IPC::Cmd模块就没问题了。 

再次安装OpenSSL:

./config --prefix=/usr/local/ssl --openssldir=/usr/local/ssl shared zlib
make
sudo make install
[root@AE openssl-3.0.15]# openssl version
OpenSSL 3.0.15 3 Sep 2024 (Library: OpenSSL 3.0.15 3 Sep 2024)

更新系统环境变量:将OpenSSL 的路径添加到 PATH 和 LD_LIBRARY_PATH 中,为了使这些变量永久生效,可以将它们添加到/etc/profile,  ~/.bashrc 或 ~/.bash_profile文件中:

echo 'export PATH="/usr/local/ssl/bin:$PATH"' >> ~/.bashrc
echo 'export LD_LIBRARY_PATH="/usr/local/ssl/lib:$LD_LIBRARY_PATH"' >> ~/.bashrc
source ~/.bashrc

在安装了   libffi   和 OpenSSL 之后,重新配置 Python 3.10 的编译环境:

cd /root/Python-3.10.16
./configure --enable-optimizations --prefix=/usr/local \
            --with-openssl=/usr/local/ssl \
            CFLAGS="-I/usr/local/ssl/include" \
            LDFLAGS="-L/usr/local/ssl/lib"

编译并安装 Python 3.10:

make
make altinstall  # 使用make altinstall而不是make install,这样可以避免覆盖系统默认的python2.7

又出错了:

sed-parameter -Wno-missing-field-initializers -Werror=implicit-function-declaration -fvisibility=hidden -fprofile-use -fprofile-correction -I./Include/internal  -I. -I./Include    -DPy_BUILD_CORE -o Python/frozen.o Python/frozen.c
rm -f libpython3.10.a
ar rcs libpython3.10.a Modules/getbuildinfo.o Parser/token.o  Parser/pegen.o Parser/parser.o Parser/string_parser.o Parser/peg_api.o Parser/myreadline.o Parser/tokenizer.o Objects/abstract.o Objects/accu.o Objects/boolobject.o Objects/bytes_methods.o Objects/bytearrayobject.o Objects/bytesobject.o Objects/call.o Objects/capsule.o Objects/cellobject.o Objects/classobject.o Objects/codeobject.o Objects/complexobject.o Objects/descrobject.o Objects/enumobject.o Objects/exceptions.o Objects/genericaliasobject.o Objects/genobject.o Objects/fileobject.o Objects/floatobject.o Objects/frameobject.o Objects/funcobject.o Objects/interpreteridobject.o Objects/iterobject.o Objects/listobject.o Objects/longobject.o Objects/dictobject.o Objects/odictobject.o Objects/memoryobject.o Objects/methodobject.o Objects/moduleobject.o Objects/namespaceobject.o Objects/object.o Objects/obmalloc.o Objects/picklebufobject.o Objects/rangeobject.o Objects/setobject.o Objects/sliceobject.o Objects/structseq.o Objects/tupleobject.o Objects/typeobject.o Objects/unicodeobject.o Objects/unicodectype.o Objects/unionobject.o Objects/weakrefobject.o Python/_warnings.o Python/Python-ast.o Python/asdl.o Python/ast.o Python/ast_opt.o Python/ast_unparse.o Python/bltinmodule.o Python/ceval.o Python/codecs.o Python/compile.o Python/context.o Python/dynamic_annotations.o Python/errors.o Python/frozenmain.o Python/future.o Python/getargs.o Python/getcompiler.o Python/getcopyright.o Python/getplatform.o Python/getversion.o Python/hamt.o Python/hashtable.o Python/import.o Python/importdl.o Python/initconfig.o Python/marshal.o Python/modsupport.o Python/mysnprintf.o Python/mystrtoul.o Python/pathconfig.o Python/preconfig.o Python/pyarena.o Python/pyctype.o Python/pyfpe.o Python/pyhash.o Python/pylifecycle.o Python/pymath.o Python/pystate.o Python/pythonrun.o Python/pytime.o Python/bootstrap_hash.o Python/structmember.o Python/symtable.o Python/sysmodule.o Python/thread.o Python/traceback.o Python/getopt.o Python/pystrcmp.o Python/pystrtod.o Python/pystrhex.o Python/dtoa.o Python/formatter_unicode.o Python/fileutils.o Python/suggestions.o Python/dynload_shlib.o    Modules/config.o Modules/getpath.o Modules/main.o Modules/gcmodule.o Modules/posixmodule.o  Modules/errnomodule.o  Modules/pwdmodule.o  Modules/_sre.o  Modules/_codecsmodule.o  Modules/_weakref.o  Modules/_functoolsmodule.o  Modules/_operator.o  Modules/_collectionsmodule.o  Modules/_abc.o  Modules/itertoolsmodule.o  Modules/atexitmodule.o  Modules/signalmodule.o  Modules/_stat.o  Modules/timemodule.o  Modules/_threadmodule.o  Modules/_localemodule.o  Modules/_iomodule.o Modules/iobase.o Modules/fileio.o Modules/bytesio.o Modules/bufferedio.o Modules/textio.o Modules/stringio.o  Modules/faulthandler.o  Modules/_tracemalloc.o  Modules/symtablemodule.o  Modules/xxsubtype.o Python/frozen.o
gcc -pthread -L/usr/local/ssl/lib    -Xlinker -export-dynamic -o python Programs/python.o libpython3.10.a -lcrypt -lpthread -ldl  -lutil -lm   -lm 
./python -E -S -m sysconfig --generate-posix-vars ;\
if test $? -ne 0 ; then \
        echo "generate-posix-vars failed" ; \
        rm -f ./pybuilddir.txt ; \
        exit 1 ; \
fi
Could not import runpy module
Traceback (most recent call last):
  File "/root/Python-3.10.16/Lib/runpy.py", line 15, in <module>
    import importlib.util
  File "/root/Python-3.10.16/Lib/importlib/util.py", line 14, in <module>
    from contextlib import contextmanager
  File "/root/Python-3.10.16/Lib/contextlib.py", line 4, in <module>
    import _collections_abc
SystemError: <built-in function compile> returned NULL without setting an exception
generate-posix-vars failed
make[1]: *** [pybuilddir.txt] 错误 1
make[1]: 离开目录“/root/Python-3.10.16”
make: *** [profile-opt] 错误 2

 查看gcc版本:

[root@AE yum.repos.d]# gcc --version
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44)
Copyright © 2015 Free Software Foundation, Inc.
本程序是自由软件;请参看源代码的版权声明。本软件没有任何担保;
包括没有适销性和某一专用目的下的适用性担保。

 gcc版本4.8.5,尝试升级gcc:

[root@AE yum.repos.d]# yum install devtoolset-7-gcc-c++

[root@AE yum.repos.d]# scl enable devtoolset-7 bash

[root@AE yum.repos.d]# gcc --version
gcc (GCC) 7.3.1 20180303 (Red Hat 7.3.1-5)
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 重新编译安装:

make
make altinstall 

验证 Python 3.10 是否安装成功:

[root@AE Python-3.10.16]# python3.10 --version
Python 3.10.16

总算是成功了。

最后,建个软连接:

cd /usr/bin
ln -s /usr/local/bin/python3.10 python3

### 安装 Python 3.10 的步骤 #### 准备工作 为了确保顺利安装 Python 3.10,在开始之前需要确认系统已更新并安装必要的开发工具和库。 ```bash sudo yum update -y sudo yum groupinstall "Development Tools" -y sudo yum install openssl-devel bzip2-devel libffi-devel zlib-devel -y ``` #### 下载 Python 源码包 通过wget命令从官方服务器获取Python 3.10版本的压缩文件,并将其保存到本地磁盘中[^1]。 ```bash cd /tmp/ wget https://www.python.org/ftp/python/3.10.15/Python-3.10.15.tgz tar -xf Python-3.10.15.tgz ``` #### 编译与安装 进入解压后的源代码目录,设置自定义前缀来指定新的Python解释器存放位置,接着执行`make && make altinstall`完成编译过程而不覆盖现有的默认Python版本。 ```bash cd Python-3.10.15 ./configure --prefix=/usr/local/python3.10 --enable-optimizations make altinstall ``` #### 设置环境变量 为了让新安装Python可以在任何地方被调用,需将该路径加入系统的PATH环境中。编辑用户的`.bashrc`或全局配置文件`/etc/profile.d/python.sh`添加如下内容: ```bash export PATH=$PATH:/usr/local/python3.10/bin ``` 使更改立即生效可以运行 `source ~/.bashrc` 或者重启终端会话。 #### 创建符号链接 (可选) 如果希望使用更简洁的方式调用新版本,则可以通过创建软连接实现: ```bash ln -s /usr/local/python3.10/bin/python3.10 /usr/bin/python3.10 ``` 此时应该能够成功安装Python 3.10CentOS Linux之上,并可通过命令行输入`python3.10 --version`验证安装情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值