Skip to content

Instantly share code, notes, and snippets.

@mtesseracted
Last active April 9, 2024 18:26
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 mtesseracted/3a953febf3925a5462b1cda170100e98 to your computer and use it in GitHub Desktop.
Save mtesseracted/3a953febf3925a5462b1cda170100e98 to your computer and use it in GitHub Desktop.
install a specific python version from bash
#!/bin/bash
pyver=3.9
if [ -n "$1" ]; then
pyver="$1"
fi
pyroot=$HOME/opt/pythonidae
pyftp="https://www.python.org/ftp/python"
quitexit(){
if [ -z "$BASH_SOURCE" ]; then
return
else
return
fi
}
#sudo apt-get install libssl-dev openssl make gcc
# assumes: awk, sed, grep, tar
# libffi-dev zlib1g-dev libjpeg-dev libbz2-dev # numpy, pip, matplotlib, bzip2, lzma, autocomplete
bash_deps=(libssl-dev libffi-dev zlib1g-dev libjpeg-dev libbz2-dev liblzma-dev libncurses-dev openssl make gcc)
needed=""
for dep in ${bash_deps[@]}; do
dpkg -s $dep &> /dev/null
dep_yes=$?
if [ $dep_yes = 1 ]; then
needed="$needed $dep"
fi
#echo "$dep :: $dep_yes"
done
if [ -n "$needed" ]; then
echo "Missing packages, please run :> sudo apt-get install $needed"
exit 1
fi
pyftp_html="$(wget -qO- $pyftp)"
html_body="$(echo "$pyftp_html" | sed -n '/^<a href/{p; :loop n; /binaries/q; p; b loop}')" # scrub header and footer
pyvers="$(echo "$html_body" | grep -o -P '(?<=">).*(?=/<)')" # get python version nums
#pydates="$(echo "$html_scrub" | awk '{print $3}')" # get python version dates
num_vers=$(echo -e "$pyvers" |wc -l)
extras=$(echo -e "$pyvers\n$pyver" | sort -u | wc -l)
if [ $num_vers = $extras ]; then
get_ver=$pyver
echo "Found requested version: $get_ver"
else
last_ver_num=$(echo $pyver | awk -F . '{print $NF}') # get last digit of requested version
inc_ver=${pyver/%$last_ver_num/$((last_ver_num+1))} # increment the version number
extras="$(echo -e "$pyvers\n$inc_ver" | sort -uV)" # get a list with the incremented ver num
get_ver=$(echo "$extras" | sed -n "/$inc_ver/{x;p;q;}; x") # find the latest version less than inc_ver
ver_date=$(echo "$html_body" |awk "/$get_ver/ {print \$3}")
echo "Requested version: $pyver, newest match ($ver_date): $get_ver"
read -p "Continue? (Y/n): " confirm && [[ -z "$confirm" || ${confirm::1} == [yY] ]] || quitexit
fi
ftp_ver="$pyftp/$get_ver/Python-$get_ver.tgz"
#wget https://www.python.org/ftp/python/3.9.2/Python-3.9.2.tgz
echo "Installing python in $pyroot"
mkdir -p $pyroot
cd $pyroot
wget $ftp_ver
tar xzvf Python-$get_ver.tgz
cd Python-$get_ver
mkdir install
cd install
../configure --prefix=$PWD #--enable-optimizations
make
make install
cd ..
ln -s install/python python
#sudo ln -fs /opt/Python-3.9.2/Python /usr/bin/python3.9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment