一、先安装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 版本
yum -y install centos-release-scl gcc gcc-c++
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 = https://mirrors.aliyun.com/pypi/simple/
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/mysql/* /usr/include/
cp /data/software/mysql/lib/libmysqlclient.so /usr/lib/
报错信息如下
(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.