Skip to content

Instantly share code, notes, and snippets.

@olen2006
Last active January 6, 2023 07:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olen2006/1ad121707b86cccb6af2e1ff8b2ad265 to your computer and use it in GitHub Desktop.
Save olen2006/1ad121707b86cccb6af2e1ff8b2ad265 to your computer and use it in GitHub Desktop.
bash scipt that installs desired/latest python. if no value entered, default version is installed.
#!/bin/bash
########################################
# install latest python version
# into /usr/bin/
# with all neccessary packages
########################################
def_version="3.11.1"
py_dir="python_installation"
sudo yum update -y
sudo yum upgrade -y
sudo yum remove openssl-devel -y
sudo yum -y groupinstall "Development Tools"
sudo yum -y install gcc openssl11 openssl11-devel bzip2-devel libffi-devel
py_check(){
version=${1:-$1}
local version_check=$(python${version::-2} -V 2>&1 | grep -Po '(?<=Python )(.+)')
echo $version_check
! [ -z $version_check ]
}
install_py() {
mkdir $py_dir && cd $py_dir
wget -v https://www.python.org/ftp/python/$version/Python-$version.tgz
tar xzf Python-$version.tgz #v
rm -f Python-$version.tgz
cd Python-$version
./configure --enable-optimizations --with-ensurepip=install
make -j $(nproc)
sudo make altinstall
# cleanup
cd ../..
rm -rf $py_dir
echo "Python-$version is installed. Enjoy ;)"
}
echo "#################### <Start> ####################"
read -t 10 -p "Enter Python version you wish to install [default $def_version] If no value entered within 10 seconds window, default value is used: " version
# check if Python is already installed
if [[ -z $version ]]
then
#checking default version
echo "Checking if default Python $def_version is installed..."
if py_check $def_version;
then
echo "Python-$version is alredy installed"
else
echo "Installing Python-$version..."
install_py $version
fi
else
# checking non default version
echo "Checking if entered Python $version is installed..."
if py_check $version;
then
echo "Python-$version is alredy installed"
else
echo "Installing Python-$version..."
install_py $version
fi
fi
echo "#################### The End ####################"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment