centos7上一键安装python3.7

185 阅读1分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

#file name: xInstallPython375.sh
#centos7.6上自动安装python3.7.5
#使用方法:Centos命令行下执行指令bash xInstallPython375.sh
#Author:Kahn.Xiao
 
PythonVersion=Python-3.7.5
PythonURL=https://www.python.org/ftp/python/3.7.5/${PythonVersion}.tar.xz
 
CheckOSversion() {
OSseries=`awk '{print $1}' /etc/redhat-release`
OSversion=`awk '{print $4}' /etc/redhat-release |cut -f1 -d "."`
if [[ $OSseries -ne "CentOS" && $OSversion -ne "7" ]];then
	echo -e "\e[1;31m FBI Warning! OS version is not Centos7 series\e[0m"
	return 1
	exit
else
	echo "The current operating system version meets the installation requirements and the script will continue to execute."
fi
}
 
InternetIsUp() {
	ping -c4 -W2 223.5.5.5 &>/dev/null
	if [ $? -eq 0 ];then
		return 0
	else
		return 1
		echo -e "\e[1;31mInternet is down,Pls check network.\e[0m"
		exit
	fi
}
 
InstallWget() {
	which wget
	if [ $? -ne 0 ];then
		yum install -y wget
	fi
}
 
YumInstallSoftware() {
	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 kernel-devel libffi-devel
	if [ $? -eq 0 ];then
		echo "Installation of the necessary software is complete"
	else
		echo -e "\e[1;31mInstallation of the necessary software has failed\e[0m"
		echo -e "\e[1;31mPls Make sure the software is installed。(zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel kernel-devel libffi-devel)\e[0m"
		return 1
		exit
	fi
}
 
DownloadPython() {
	if [ ! -f ${PythonVersion}.tar.xz ];then
		wget $PythonURL
	fi
}
 
CheckOSversion
InternetIsUp
InstallWget
YumInstallSoftware
DownloadPython
rm -rf ./${PythonVersion}
xz -d ${PythonVersion}.tar.xz
tar xvf  ${PythonVersion}.tar
echo "------------------Change directory to$Pythonversion start-----------------------------------"
cd $PythonVersion
echo "------------------Change directory to$Pythonversion end-----------------------------------"
if [ ! -f "configure" ];then
	echo -e "\e[1;31mPython Package uncompressing failed. Pls check python package file.\e[0m"
	return 1
	exit
fi
echo "---------------------------Start compiling the source code---------------------------------------"
sleep 3
./configure prefix=/usr/local/python3
if [ $? -eq 0 ];then
	echo "----------------------------------The compiled source code is complete-----------------------"
else
	echo -e "\e[1;31mUnfortunately, compiling the source package failed\e[0m"
	return 1
	exit
fi
make && make install
if [ $? -ne 0 ];then
	echo -e "\e[1;31mAn error occurred performing the compilation installation\e[0m"
	return 1
	exit
fi
 
PythonMainProgram=/usr/local/python3/bin/python3
if [ -f  $PythonMainProgram ];then
	ln -s /usr/local/python3/bin/python3 /usr/bin/python3
	ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
	echo -e "\e[1;32mWow!!! Very good! python3 is already installed on your operating system\e[0m"
	echo "You can verify the version by executing the command (python3 --version). \
Warning: if you are already using python, you need to execute the command (python3)."
else
	echo -e "\e[1;31mPython soft like create is failed\e[0m"
	echo -e "\e[1;31mPython install is failed.\e[0m"
	return 1
	exit
fi

本代码适合在centos7所有版本上使用。贴上直接运行即可。