#!/bin/bash
echo -e """\e[1;32m 以下为提示信息 \e[0m
1.这个脚本是干嘛的?
\e[31m此脚本能以源码的方式在您的主机上安装来自官方指定版本的python \e[0m
2.前提条件?
\e[31m需要能联通外网
需要给与执行权限(如无特殊直接下载到/root/下,且最好使用source运行此脚本 \e[0m
3.如何使用?
\e[1;33m此脚本采用半交互式!
注意!看到版本提示时只需输入版本号然后回车(直接回车默认版本为3.8.3)
注意!安装位置请使用绝对路径如输入/opt/然后回车(直接回车默认为/usr/local/) \e[0m
如输入错误可强制结束脚本后重新运行!
......
"""
echo -e "\e[1;31m 开始安装步骤 \e[0m"
# 从输入读取变量至version
read -rp "你想要安装什么版本的python?:" version
if [ -z "$version" ]; then
version=3.8.3
fi
# 与version类似
read -rp "你要将Python-$version安装在哪?:" install_dir
if [ -z "${install_dir}" ]; then
install_dir=/usr/local/
fi
echo -e "\e[1;34m 请确认:5s后Python-$version将被安装在$install_dir \e[0m ......"
sleep 5
cd
#安装必要组件
yum -y install wget make
#配置阿里云epel源
if ! rpm -q epel-release; then
yum install "https://mirrors.aliyun.com/epel/epel-release-latest-$(rpm -E '%{rhel}').noarch.rpm"
fi
#if [ -e "epel-release-latest-$(rpm -E '%{rhel}').noarch.rpm" ]; then
# yum -y install *.rpm
#else
# wget "https://mirrors.aliyun.com/epel/epel-release-latest-$(rpm -E '%{rhel}').noarch.rpm"
# yum -y install *.rpm
#fi
# wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
#安装依赖包
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel
#安装编译工具
yum -y install gcc gcc-c++ kernel-devel
#下载Python源码包
#wget https://www.python.org/ftp/python/$version/Python-$version.tgz
wget https://registry.npmmirror.com/-/binary/python/$version/Python-$version.tgz
#解压
tar -zxvf Python-$version.tgz
cd "Python-$version" || exit 1
#开始编译
./configure --prefix=$install_dir"python$version"
make && make install
#创建软链接
#ln -s $install_dir"python$version"/bin/python${version%\.*} /usr/bin/python${version%\.*}
#ln -s $install_dir"python$version"/bin/pip${version%\.*} /usr/bin/pip${version%\.*}
#配置环境变量
echo "export PATH=$install_dir"python$version"/bin:\$PATH" >/etc/profile.d/python.sh
if ! which "python${version%\.*}"; then
echo "python${version%\.*}安装完成"
#删除包
cd
rm -rf Python-$version.tgz Python-$version
else
echo """安装失败
也许需要手动读取python环境变量
source /etc/profile.d/python.sh
"""
fi
python安装脚本
最新推荐文章于 2024-05-16 08:20:37 发布