工作中。服务器经常需要用到python3.x的版本,但是centos的大多版本系统默认都是2.7版本,故经常需要手动安装指定版本,故写成脚本方便日常使用
此脚本用来安装python3.x的版本,会自动解决安装高版本python,yum仓库不能使用的情况!
cat install_py.sh
#!/bin/bash
echo "which python version to install,default version is 3.8.5"
echo "put in 1 to select default version"
echo "put in 2 or otehr num to select other version "
read -p "install the default version:3.8.5 ? 1 or 2..." result
echo $result
if [ $? -eq 1 ];
then
install_v="3.8.5"
else
while :
do
read -p "put in the version you want to install " version
curl https://www.python.org/ftp/python/$version/ |grep "404 Not Found"
if [ $? -eq 0 ];
echo "没有该版本"
continue
else
install_v=$version
break
fi
done
fi
echo $install_v
yum -y install libffi-devel zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make
wget https://www.python.org/ftp/python/$install_v/Python-$install_v.tgz
tar -axf Python-$install_v.tgz
cd Python-$install_v
./configure
make && make install
mv /usr/bin/python /usr/bin/python.old
#用于安装3开头的版本,如果是其他版本,下面的软连接的命令需要手动改一下
ln -s /usr/local/bin/python3 /usr/bin/python
sed -i 's#/usr/bin/python#/usr/bin/python2#g' /usr/bin/yum
sed -i 's#/usr/bin/python#/usr/bin/python2#g' /usr/libexec/urlgrabber-ext-down
python --version |grep $install_v && echo "Installation succeeded" || echo "Installation failed"
如果有需要安装的小伙伴,直接在服务器新建一个文件把内容粘贴进去运行就可以
touch install_py.sh
sh install_py.sh