setup_env/install_python3.sh
Paul Trowbridge abaf266904 improve install scripts with modern practices and transparency
- Use modern signed-by method for PostgreSQL GPG keys
- Auto-detect latest Python 3 version from deadsnakes PPA
- Add command tracing (set -x) to show all commands as they run
- Display sudo commands upfront before execution
- Add user confirmation prompts (y/N) before installation
- Improve error handling with set -euo pipefail
- Add proper cleanup and verification steps
- Use pipx for VisiData installation
- Better output formatting with clear section headers

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-17 23:28:44 -05:00

84 lines
2.3 KiB
Bash
Executable File

#!/bin/bash
set -euo pipefail
echo "============================================"
echo "Python 3 Latest Installation Script"
echo "============================================"
echo ""
echo "This script will run the following commands with sudo:"
echo " - apt-get update"
echo " - apt-get install -y software-properties-common"
echo " - add-apt-repository -y ppa:deadsnakes/ppa"
echo " - apt-get update"
echo " - apt-get install -y python3.X python3.X-venv python3.X-dev python3.X-distutils"
echo " - <latest-python> (to install pip via get-pip.py)"
echo ""
read -p "Continue with installation? (y/N) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "Installation cancelled."
exit 0
fi
echo ""
echo "Starting installation (commands will be shown as they run)..."
echo ""
# Enable command tracing
set -x
# Install prerequisites
sudo apt-get update
sudo apt-get install -y software-properties-common
# Add deadsnakes PPA
sudo add-apt-repository -y ppa:deadsnakes/ppa
sudo apt-get update
# Disable tracing temporarily to find latest version cleanly
set +x
# Find the latest Python 3 version available
echo "Finding latest Python 3 version..."
LATEST_PYTHON=$(apt-cache search --names-only '^python3\.[0-9]+$' | \
grep -oP 'python3\.\d+' | \
sort -V | \
tail -1)
if [ -z "$LATEST_PYTHON" ]; then
echo "Error: Could not determine latest Python 3 version" >&2
exit 1
fi
echo "Installing $LATEST_PYTHON..."
set -x
sudo apt-get install -y \
"$LATEST_PYTHON" \
"$LATEST_PYTHON-venv" \
"$LATEST_PYTHON-dev" \
"$LATEST_PYTHON-distutils"
# Install pip for the new Python version
curl -sS https://bootstrap.pypa.io/get-pip.py | sudo "$LATEST_PYTHON"
# Disable command tracing for cleaner output
set +x
echo ""
echo "============================================"
# Verify installation
if command -v "$LATEST_PYTHON" &> /dev/null; then
echo "$LATEST_PYTHON installed successfully!"
"$LATEST_PYTHON" --version
"$LATEST_PYTHON" -m pip --version
else
echo "Error: $LATEST_PYTHON installation failed" >&2
exit 1
fi
echo ""
echo "Note: Use '$LATEST_PYTHON' to run this version"
echo "To make it the default python3, run:"
echo " sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/$LATEST_PYTHON 1"
echo "============================================"