Last active
October 4, 2024 15:33
-
-
Save chrisribe/c3bbd8d8ca0e07b9f7bfee7c6ef1c3f9 to your computer and use it in GitHub Desktop.
Install latest python version from python.org / atp install
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Inspired by: https://linuxcapable.com/install-python-3-12-on-ubuntu-linux/ | |
# Function to get the latest stable Python version | |
get_latest_stable_python_version() { | |
# Fetch the HTML content and store it in a variable | |
HTML_CONTENT=$(curl -s --compressed https://www.python.org/downloads/ | tr -d '\0') | |
# Check if HTML_CONTENT is empty | |
if [ -z "$HTML_CONTENT" ]; then | |
echo "Failed to fetch the HTML content from https://www.python.org/downloads/" | |
exit 1 | |
fi | |
# Debug: Output the fetched HTML content (optional, can be commented out) | |
# echo "$HTML_CONTENT" | |
# Extract the latest stable Python version using grep, excluding pre-releases | |
echo "$HTML_CONTENT" | grep -oP 'Python \K[0-9]+\.[0-9]+(?=\.[0-9]+(?![a-zA-Z]))' | sort -V | tail -1 | |
} | |
# Get the latest stable Python version | |
LATEST_PYTHON_VERSION=$(get_latest_stable_python_version) | |
# Check if LATEST_PYTHON_VERSION is empty | |
if [ -z "$LATEST_PYTHON_VERSION" ]; then | |
echo "Failed to determine the latest stable Python version." | |
exit 1 | |
fi | |
# Display the latest stable Python version to the user | |
echo "The latest stable Python version is: $LATEST_PYTHON_VERSION" | |
# Prompt the user to enter the Python version or use the latest | |
read -p "Enter the Python version to install (or press Enter to install the latest stable version $LATEST_PYTHON_VERSION): " PYTHON_VERSION | |
if [ -z "$PYTHON_VERSION" ]; then | |
PYTHON_VERSION=$LATEST_PYTHON_VERSION | |
echo "Installing the latest stable Python version: $PYTHON_VERSION" | |
else | |
echo "Installing Python version: $PYTHON_VERSION" | |
fi | |
# Update package list and upgrade any outdated packages | |
sudo apt update | |
sudo apt upgrade -y | |
# Import the stable PPA for the specified Python version | |
sudo add-apt-repository ppa:deadsnakes/ppa -y | |
# Refresh the APT cache | |
sudo apt update | |
# Install the specified Python version | |
sudo apt install python$PYTHON_VERSION -y | |
# Confirm the installation | |
python$PYTHON_VERSION --version | |
echo "Python $PYTHON_VERSION installation is complete." | |
# Install the Python venv module for the specified version | |
sudo apt install python$PYTHON_VERSION-venv |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment