Skip to content

Instantly share code, notes, and snippets.

@Bluscream
Last active December 30, 2023 01: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 Bluscream/a4af709e3cd548cdcc14ab17d3b96c70 to your computer and use it in GitHub Desktop.
Save Bluscream/a4af709e3cd548cdcc14ab17d3b96c70 to your computer and use it in GitHub Desktop.
Raspbian Python upgrade bash script
#!/bin/bash
# https://raw.githubusercontent.com/tvdsluijs/sh-python-installer/main/python.sh
# Function to kill all Python processes
kill_python() {
sudo pkill -f python
}
# Function to remove all old Python versions
remove_old_python() {
python3 -m pip freeze > pip_packages.txt
pip freeze | xargs python3 -m pip uninstall -y
sudo apt-get purge --auto-remove '*python*'
}
# Function to update system
update_system() {
sudo apt update && sudo apt upgrade -y
}
# Function to install Python from apt
install_python_from_apt() {
sudo apt-get install -y python3 python3-pip python3-venv
echo "export PATH=\$PATH:/usr/local/bin" >> ~/.bashrc
source ~/.bashrc
}
# Function to install Python from source
install_python_from_source() {
latest_version=$(curl -s https://www.python.org/downloads/ | grep "latest Python" | sed 's/.*Python //; s/.<.*//')
wget https://www.python.org/ftp/python/$latest_version/Python-$latest_version.tgz
tar -xf Python-$latest_version.tgz
cd Python-$latest_version
./configure --enable-optimizations
make -j 4
sudo make altinstall
echo "export PATH=\$PATH:/usr/local/bin" >> ~/.bashrc
source ~/.bashrc
}
# Function to reinstall previously installed packages
reinstall_packages() {
python3 -m pip install --upgrade pip
python3 -m pip install -r pip_packages.txt
}
# Main script
echo "Do you want to get the latest Python version from apt or compile it from source?"
select opt in "apt" "source" "uninstall"; do
case $opt in
"apt")
update_system
install_python_from_apt
reinstall_packages
break
;;
"source")
update_system
install_python_from_source
reinstall_packages
break
;;
"uninstall")
kill_python
remove_old_python
break
;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment