Skip to content

Instantly share code, notes, and snippets.

@PapyrusThePlant
Last active March 28, 2018 16:04
Embed
What would you like to do?
A script to download, compile and install Python on Debian/Ubuntu distribs
#!/bin/bash
# -----------------------------------------
# DCIPy (Download, Compile, Install Python)
# -----------------------------------------
# This script was written in order to download, compile and install Python on a Raspberry Pi.
# So this should work on most Debian/Ubuntu distribs.
# Use at your own risks, because idk wtf I'm doing most of the time.
# If you notice any issue, please let me know.
# Usage :
# DCIPy <python version>
# TODO : Check already installed python versions:
# sudo find / -type f -executable -iname 'python*' -exec file -i '{}' \; | awk -F: '/x-executable; charset=binary/ {print $1}' | xargs readlink -f | sort -u | xargs -I % sh -c 'echo -n "%: "; % -V'
# The version numbers comparison function
# The helper function below will usually be the one to use, as it's more user friendly
# Parameters :
# * $1 : a version number delimited by dots
# * $2 : a version number delimited by dots
# Returns :
# * 0 : $1 and $2 are equal
# * 1 : $1 is bigger than $2
# * 2 ! $1 is lower than $2
version_compare_impl () {
#ver1 == ver2 ?
if [[ $1 == $2 ]]
then
return 0
fi
local IFS=.
local i ver1=($1) ver2=($2)
# Fill empty fields in ver1 with zeros
for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
do
ver1[i]=0
done
for ((i=0; i<${#ver1[@]}; i++))
do
if [[ -z ${ver2[i]} ]]
then
# Fill empty fields in ver2 with zeros
ver2[i]=0
fi
# ver1 > ver2 ?
if ((10#${ver1[i]} > 10#${ver2[i]}))
then
return 1
fi
# ver1 < ver2 ?
if ((10#${ver1[i]} < 10#${ver2[i]}))
then
return 2
fi
done
# Then ver1 == ver2
return 0
}
# Version numbers comparison helper
# Parameters :
# * $1 : a version number delimited by dots
# * $2 : the comparison operator, '<', '=' or '>'
# * $3 : a version number delimited by dots
# Returns :
# * 0 : the comparison is False
# * 1 : the comparison is True
version_compare () {
version_compare_impl $1 $3
case $? in
0) op='=';;
1) op='>';;
2) op='<';;
esac
if [[ $op != $2 ]]
then
return 1
else
return 0
fi
}
# ---------------------- #
# Let's start the work !
# ---------------------- #
# Check args
if [ $# != 1 ]
then
echo -e "Usage :\n $0 <python version number>"
exit 1
fi
version=$1
remote_directory="https://www.python.org/ftp/python/$version"
IFS=. read -r major minor micro <<< "$version"
bin_ver=$major.$minor
cmd_ver=$major
# Check version's existence
if ! wget -S --spider $remote_directory 2>&1 | grep -q 'HTTP[^\n\0]\{1,\} 200 OK'
then
echo Python $version not found.
exit 0
fi
# Update system
sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get dist-upgrade
# Install components needed for the build
sudo apt-get install -y build-essential libncursesw5-dev libgdbm-dev libc6-dev
sudo apt-get install -y zlib1g-dev libsqlite3-dev tk-dev
sudo apt-get install -y libssl-dev openssl
original_dir=$(pwd)
cd ~
# Retreive and unpack sources
wget $remote_directory/Python-$version.tgz
sudo tar -zxvf Python-$version.tgz -C /usr/src
rm Python-$version.tgz
# Configure, build and install Python
cd /usr/src/Python-$version
sudo ./configure
sudo make
sudo make install
# Check if we need to instal pip (py2.4.9, py3.4 and higher come with it)
if version_compare $version '<' 2.4.9 || \
version_compare $version '=' 3 ||
(version_compare $version '>' 3 && version_compare $version '<' 3.4)
then
sudo wget https://bootstrap.pypa.io/get-pip.py
sudo python$bin_ver get-pip.py
fi
cd $original_dir
# Version checks
echo
echo python$bin_ver: $(python$bin_ver --version)
echo python$cmd_ver: $(python$cmd_ver --version)
echo
echo pip$bin_ver: $(pip$bin_ver --version)
echo pip$cmd_ver: $(pip$cmd_ver --version)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment