#!/bin/bash # Script to automatically install Python 3.11 on macOS using Homebrew # Exit on any error set -e # Check if Homebrew is installed if ! command -v brew &> /dev/null; then echo "Homebrew not found. Installing Homebrew..." /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" # Add Homebrew to PATH for the current session eval "$(/opt/homebrew/bin/brew shellenv)" else echo "Homebrew already installed." fi # Update Homebrew echo "Updating Homebrew..." brew update # Install Python 3.11 echo "Installing Python 3.11..." brew install python@3.11 # Verify installation echo "Verifying Python 3.11 installation..." python3.11 --version # Add Python 3.11 to PATH if not already present PYTHON_PATH="/opt/homebrew/opt/python@3.11/bin" if [[ ":$PATH:" != *":$PYTHON_PATH:"* ]]; then echo "Adding Python 3.11 to PATH..." echo "export PATH=\"$PYTHON_PATH:\$PATH\"" >> ~/.zshrc source ~/.zshrc fi ln -sf /opt/homebrew/opt/python@3.11/bin/python3.11 /usr/local/bin/python ln -sf /opt/homebrew/opt/python@3.11/bin/pip3.11 /usr/local/bin/pip echo "Python 3.11 has been successfully installed!"