centos安装python3.1X

该文提供了在CentOS系统上安装OpenSSL1.1.1t和Python3.11.2的详细过程,包括下载源码、配置编译选项、安装依赖、优化编译以及处理pip安装mysqlclient时的错误。此外,还涉及到了环境变量配置、使用国内镜像加速以及GCC版本升级。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、先安装OpenSSL的依赖包

yum install -y gcc gcc-c++ autoconf automake zlib zlib-devel pcre-devel

二、进入到OpenSSL官网: /index.html 下载OpenSSL的安装包

mkdir -p /data/recycle
cd /data/recycle
wget https://www.openssl.org/source/openssl-1.1.1t.tar.gz --no-check-certificate

三、创建目录

mkdir -p /data/software/openssl

四、解压并安装OpenSSL

tar zxf openssl-1.1.1t.tar.gz
cd openssl-1.1.1t/
./config --prefix=/data/software/openssl
make -j 4 # 开起4线程去编译
make install

 五、备份旧版本openssl,将新版的openssl链接到/usr/bin

mv /usr/bin/openssl /usr/bin/openssl.old
ln -s /data/software/openssl/bin/openssl /usr/bin/openssl

 六、下载python安装包

cd /data/recycle
wget https://www.python.org/ftp/python/3.11.2/Python-3.11.2.tgz

七、 安装python依赖包

yum install -y ncurses-devel libuuid-devel sqlite-devel \
               readline-devel tcl-devel tk-devel lzma gdbm-devel \
               xz-devel libffi-devel bzip2-devel openssl-devel make

八、安装 tkinter模块 的依赖包,下载地址: Tcl - Browse /Tcl at SourceForge.net

# 进入目录
cd /data/recycle
# 下载两个安装包
curl -o tcl8.6.13-src.tar.gz https://newcontinuum.dl.sourceforge.net/project/tcl/Tcl/8.6.13/tcl8.6.13-src.tar.gz

curl -o tk8.6.13-src.tar.gz https://phoenixnap.dl.sourceforge.net/project/tcl/Tcl/8.6.13/tk8.6.13-src.tar.gz

# 安装tcl
tar zxf tcl8.6.13-src.tar.gz 
cd tcl8.6.13/unix/
./configure 
make -j 4
make install

# 安装tk
cd /data/recycle
tar zxf tk8.6.13-src.tar.gz 
cd tk8.6.13/unix/
./configure 
make -j 4
make install

# 配置环境变量
export LD_LIBRARY_PATH=/usr/local/lib
export TCLTK_CFLAGS="-I/usr/local/include"
export TCLTK_LIBS="-L/usr/local/lib -ltcl8.6 -L/usr/local/lib -ltk8.6"

九、配置lib库

cat > /etc/ld.so.conf.d/openssl.conf <<EOF
/data/software/openssl/lib
EOF

cat  > /etc/ld.so.conf.d/lib.conf <<EOF
/usr/local/lib
EOF

ldconfig  # 加载lib库

九、安装python,这里二选一按照

第一种方式安装,不优化 python 的代码运行速度

tar zxf Python-3.11.2.tgz
cd Python-3.11.2
./configure --prefix=/data/software/python3 \
            --with-openssl=/data/software/openssl \
            --with-openssl-rpath=auto
make -j 4 # 开起4线程去编译
make install

第二种方式安装,优化 python 的代码运行速度 (可以提高python10%-20%代码运行速度),加 --enable-optimizations 参数配置并生成可以编译的执行文件Makefile

加上这个参数需要 gcc 版本大于等于 8.1.0,不然 make 编译会报错

Rebuilding with profile guided optimizations:
rm -f profile-clean-stamp
make build_all CFLAGS_NODIST=" -fprofile-use -fprofile-correction" LDFLAGS_NODIST=""
make[1]: Entering directory `/data/recycle/Python-3.12.0rc3'
./_bootstrap_python ./Programs/_freeze_module.py abc ./Lib/abc.py Python/frozen_modules/abc.h
Fatal Python error: init_import_site: Failed to import the site module
Python runtime state: initialized
Traceback (most recent call last):
  File "/data/recycle/Python-3.12.0rc3/Lib/site.py", line 73, in <module>
    import os
  File "/data/recycle/Python-3.12.0rc3/Lib/os.py", line 29, in <module>
    from _collections_abc import _check_methods
SystemError: <built-in function compile> returned NULL without setting an exception
make[1]: *** [Python/frozen_modules/abc.h] Error 1
make[1]: Leaving directory `/data/recycle/Python-3.12.0rc3'
make: *** [profile-opt] Error 2

这里安装 gcc 9 版本

# 安装 SCLO源
yum -y install centos-release-scl

替换为阿里云源
cat > /etc/yum.repos.d/CentOS-SCLo-scl.repo <<'EOF'
[centos-sclo-sclo]
name=CentOS-7 - SCLo sclo
baseurl=http://mirrors.aliyun.com/centos/$releasever/sclo/$basearch/sclo/
        http://mirrors.aliyuncs.com/centos/$releasever/sclo/$basearch/sclo/
        http://mirrors.cloud.aliyuncs.com/centos/$releasever/sclo/$basearch/sclo/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo
EOF

cat > /etc/yum.repos.d/CentOS-SCLo-scl-rh.repo <<'EOF'
[centos-sclo-rh]
name=CentOS-7 - SCLo rh
baseurl=http://mirrors.aliyun.com/centos/$releasever/sclo/$basearch/rh/
        http://mirrors.aliyuncs.com/centos/$releasever/sclo/$basearch/rh/
        http://mirrors.cloud.aliyuncs.com/centos/$releasever/sclo/$basearch/rh/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo
EOF

yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
echo "source /opt/rh/devtoolset-9/enable" >> /etc/profile
source /etc/profile
./configure --prefix=/data/software/python3 \
            --with-openssl=/data/software/openssl \
            --with-openssl-rpath=auto \
            --enable-optimizations

make -j 4 # 开起4线程去编译
make install

十、配置python环境

cat > /etc/profile.d/python_home.sh <<'EOF'
PYTHON_HOME=/data/software/python3
export PATH=$PATH:$PYTHON_HOME/bin

EOF

source /etc/profile

十一、改为国内源

mkdir -p /root/.config/pip/
cat > /root/.config/pip/pip.conf<<'EOF'
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com

EOF

十二、升级pip 

pip3 install --upgrade pip

十三、防止 pip 安装 mysqlclient 报错,需要把

cp /data/software/mysql/lib/pkgconfig/mysqlclient.pc /usr/lib64/pkgconfig/
cp -r /data/software/mysql/include/* /usr/include/
cp /data/software/mysql/lib/libmysqlclient.so /usr/lib/
ln -s /usr/lib/libmysqlclient.so /usr/lib/libmysqlclient.so.20
cp /usr/lib/libmysqlclient.so* /usr/lib64/

报错信息如下

(py3) [root@test 16:09:01/root ]# pip install mysqlclient==2.2.0
Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
Collecting mysqlclient==2.2.0
  Downloading https://mirrors.aliyun.com/pypi/packages/de/9c/b176826e8994551ce826404dab97e305a4bb76c8b0a4e016fabda2901c71/mysqlclient-2.2.0.tar.gz (89 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 89.5/89.5 kB 1.0 MB/s eta 0:00:00
  Installing build dependencies ... done
  Getting requirements to build wheel ... error
  error: subprocess-exited-with-error
  
  × Getting requirements to build wheel did not run successfully.
  │ exit code: 1
  ╰─> [25 lines of output]
      Trying pkg-config --exists mysqlclient
      Command 'pkg-config --exists mysqlclient' returned non-zero exit status 1.
      Trying pkg-config --exists mariadb
      Command 'pkg-config --exists mariadb' returned non-zero exit status 1.
      Traceback (most recent call last):
        File "/opt/py3/lib/python3.11/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 353, in <module>
          main()
        File "/opt/py3/lib/python3.11/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 335, in main
          json_out['return_val'] = hook(**hook_input['kwargs'])
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/opt/py3/lib/python3.11/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 118, in get_requires_for_build_wheel
          return hook(config_settings)
                 ^^^^^^^^^^^^^^^^^^^^^
        File "/tmp/pip-build-env-7klkujj2/overlay/lib/python3.11/site-packages/setuptools/build_meta.py", line 325, in get_requires_for_build_wheel
          return self._get_build_requires(config_settings, requirements=['wheel'])
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/tmp/pip-build-env-7klkujj2/overlay/lib/python3.11/site-packages/setuptools/build_meta.py", line 295, in _get_build_requires
          self.run_setup()
        File "/tmp/pip-build-env-7klkujj2/overlay/lib/python3.11/site-packages/setuptools/build_meta.py", line 311, in run_setup
          exec(code, locals())
        File "<string>", line 154, in <module>
        File "<string>", line 48, in get_config_posix
        File "<string>", line 27, in find_package_name
      Exception: Can not find valid pkg-config name.
      Specify MYSQLCLIENT_CFLAGS and MYSQLCLIENT_LDFLAGS env vars manually
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
error: subprocess-exited-with-error

× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> See above for output.

note: This error originates from a subprocess, and is likely not a problem with pip.

### 安装和配置 Python 3.1 的方法 要在 CentOS安装和配置 Python 3.1,可以按照以下方式操作: #### 下载并解压源码包 首先需要下载 Python 3.1 的源码包。可以通过 `wget` 命令从官方站点获取对应的版本。以下是具体命令: ```bash wget https://www.python.org/ftp/python/3.1.5/Python-3.1.5.tar.bz2 tar jxvf Python-3.1.5.tar.bz2 cd Python-3.1.5 ``` 上述命令会完成下载、解压缩以及进入目录的操作[^2]。 #### 配置编译环境 在解压完成后,需运行 `./configure` 脚本来检测系统的依赖项,并设置构建参数。此过程可能需要一些额外的开发工具支持,例如 gcc 和 make 工具链。 ```bash ./configure --prefix=/usr/local/python3.1 ``` 这里通过 `--prefix` 参数指定了自定义安装路径 `/usr/local/python3.1`,以便与其他已有的 Python 版本共存而不发生冲突[^1]。 #### 编译与安装 一旦配置成功,则可继续执行实际的编译和安装工作: ```bash make && make install ``` 这一步可能会花费一定时间来完成整个编译流程。如果遇到错误提示缺少某些库或者头文件,请先确认系统已经安装必要的软件集合,比如 `gcc`, `zlib-devel`, 或者其他相关组件[^3]。 #### 创建软链接方便调用 为了简化后续使用体验,通常建议创建一个指向新安装 Python 可执行程序的符号链接到全局 PATH 中的一个位置,这样可以直接通过简单的命令名启动它而无需指定全路径。 ```bash ln -s /usr/local/python3.1/bin/python3.1 /usr/bin/python3.1 ``` 至此,您应该能够在终端里输入 `python3.1` 来验证是否正确设置了新的解释器版本。 ```python import sys print(sys.version) ``` 以上脚本可以帮助测试当前使用的 python 解释器及其确切版本号。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值