CentOS 7.9.2009上Python 3.12.3的一键安装脚本
#!/bin/bash
# 定义Python版本号和目录变量
PYTHON_VERSION="3.12.3"
PYTHON_BIN="3.12"
PYTHON_CURRENT_VERSION_DIR="/usr/local/python/current"
PYTHON_RELEASE_DIR="/usr/local/python/release/python-${PYTHON_VERSION}"
PYTHON_SOURCE="Python-${PYTHON_VERSION}.tgz"
# 判断执行用户是否为root,如果不是则退出
function check_root_user() {
if [ "$(id -u)" != "0" ]; then
echo "此脚本需要以root用户执行" 1>&2
exit 1
fi
}
# 安装操作系统依赖
function install_dependencies() {
if [ -f /etc/redhat-release ]; then
# CentOS 或 RHEL
yum update -y
yum install -y gcc zlib zlib-devel openssl-devel bzip2 bzip2-devel xz-devel libffi-devel sqlite-devel ncurses-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel mesa-libGL wget
elif [ -f /etc/debian_version ]; then
# Ubuntu 或 Debian
apt update && apt upgrade -y
apt install -y build-essential zlib1g zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget
else
echo "不支持的Linux发行版"
exit 1
fi
}
# 下载并解压Python源代码
function download_and_extract_python_source() {
local PYTHON_SOURCE="Python-${PYTHON_VERSION}.tgz"
if [ ! -f "${PYTHON_SOURCE}" ]; then
wget -O "${PYTHON_SOURCE}" "https://www.python.org/ftp/python/${PYTHON_VERSION}/${PYTHON_SOURCE}"
else
echo "Python源代码压缩包已存在,无需重新下载。"
fi
if [ -f "${PYTHON_SOURCE}" ]; then
if [ -d "Python-${PYTHON_VERSION}" ]; then
mv "Python-${PYTHON_VERSION}" "Python-${PYTHON_VERSION}-backup-$(date "+%Y.%m.%d-%H.%M.%S")"
fi
tar -xzf "${PYTHON_SOURCE}"
cd "Python-${PYTHON_VERSION}"
else
echo "Python源代码压缩包不存在,无法解压。"
exit 1
fi
}
# 编译并安装Python
function compile_and_install_python() {
mkdir -pv "${PYTHON_RELEASE_DIR}"
export CFLAGS=$(pkg-config --cflags openssl11)
export LDFLAGS=$(pkg-config --libs openssl11)
./configure --prefix="${PYTHON_RELEASE_DIR}" --with-openssl=/usr/bin/openssl #在低版本的gcc中带有?enable-optimizations参数,会报错,升级gcc至高版本,gcc 8.1.0已修复此问题
make -j$(nproc) && make install
}
# 验证Python版本
function verify_python_version() {
PYTHON_CMD="${PYTHON_RELEASE_DIR}/bin/python${PYTHON_BIN}"
${PYTHON_CMD} --version
}
# 替换系统/usr/bin/python,并修改/usr/bin/yum和/usr/libexec/urlgrabber-ext-down
function replace_system_python() {
if [[ -e /usr/bin/python ]]; then
mv /usr/bin/python /usr/bin/python-$(/usr/bin/python --version 2>&1 | cut -d' ' -f2 | tr -d '.')
fi
if [[ -e /usr/bin/yum ]]; then
first_line=$(head -n 1 /usr/bin/yum)
if [[ $first_line == "#!/usr/bin/python"* ]]; then
sed -i "1s|/usr/bin/python|/usr/bin/python2.7|" /usr/bin/yum
fi
fi
local binyum_path="/usr/bin/yum"
if [[ -f "$binyum_path" ]]; then
sed -i '1d' "$binyum_path"
sed -i '1i\#\!/usr/bin/python2.7' "$binyum_path"
else
echo "文件 $binyum_path 不存在,无法修改。"
fi
local urlgrabber_path="/usr/libexec/urlgrabber-ext-down"
if [[ -f "$urlgrabber_path" ]]; then
sed -i '1d' "$urlgrabber_path"
sed -i '1i\#\!/usr/bin/python2.7' "$urlgrabber_path"
else
echo "文件 $urlgrabber_path 不存在,无法修改。"
fi
# 创建新的Python符号链接
ln -sfv "${PYTHON_CURRENT_VERSION_DIR}/bin/python${PYTHON_BIN}" /usr/bin/python
}
# 创建Python版本的软链接
function create_python_symlink() {
cd /usr/local/python && ln -sfv "release/python-${PYTHON_VERSION}" current
}
# 设置环境变量
function set_environment_variables() {
echo "export PATH=\$PATH:${PYTHON_CURRENT_VERSION_DIR}/bin" > /etc/profile.d/python.sh
chmod +x /etc/profile.d/python.sh
source /etc/profile.d/python.sh
}
# 清理下载的源代码和压缩包
function cleanup() {
rm -rf "Python-${PYTHON_VERSION}" "${PYTHON_SOURCE}"
}
# 主执行流程
check_root_user
install_dependencies
download_and_extract_python_source
compile_and_install_python
verify_python_version
replace_system_python
create_python_symlink
set_environment_variables
#cleanup
echo "Python ${PYTHON_VERSION} 安装完成,并已设置环境变量!"