Spaces:
Sleeping
Sleeping
File size: 3,850 Bytes
4fa92ae | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | #!/bin/bash
# Install Python 3.12 on Amazon Linux (without sudo)
# This script attempts to install Python 3.12 in user space
set -e
echo "π Installing Python 3.12 for AWS Lambda Layers..."
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
print_status() {
echo -e "${GREEN}[INFO]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Check if we already have Python 3.12
if command -v python3.12 &> /dev/null; then
print_status "Python 3.12 is already available: $(python3.12 --version)"
exit 0
fi
# Try to install Python 3.12 using package manager first
print_status "Attempting to install Python 3.12 using package manager..."
# For Amazon Linux 2023
if [ -f /etc/os-release ]; then
source /etc/os-release
if [[ "$NAME" == *"Amazon Linux"* ]]; then
print_status "Detected Amazon Linux"
# Try with dnf (Amazon Linux 2023)
if command -v dnf &> /dev/null; then
print_status "Using dnf to install Python 3.12..."
dnf install -y python3.12 python3.12-pip python3.12-devel || {
print_warning "Failed to install Python 3.12 via dnf"
}
# Try with yum (Amazon Linux 2)
elif command -v yum &> /dev/null; then
print_status "Using yum to install Python 3.12..."
yum install -y python3.12 python3.12-pip python3.12-devel || {
print_warning "Failed to install Python 3.12 via yum"
}
fi
fi
fi
# Check if installation was successful
if command -v python3.12 &> /dev/null; then
print_status "β
Python 3.12 installed successfully: $(python3.12 --version)"
# Create symlinks if they don't exist
if [ ! -f /usr/bin/python3.12 ]; then
which python3.12 > /dev/null && ln -sf $(which python3.12) /usr/local/bin/python3.12 || true
fi
# Set alternatives
if command -v alternatives &> /dev/null; then
alternatives --install /usr/bin/python3 python3 /usr/bin/python3.12 1 || true
alternatives --install /usr/bin/pip3 pip3 /usr/bin/pip3.12 1 || true
fi
exit 0
fi
# If package manager installation failed, try building from source
print_warning "Package manager installation failed. Attempting to build from source..."
# Create a local installation directory
PYTHON_HOME="$HOME/.local/python3.12"
mkdir -p $PYTHON_HOME
# Download Python 3.12 source
PYTHON_VERSION="3.12.2"
PYTHON_URL="https://www.python.org/ftp/python/${PYTHON_VERSION}/Python-${PYTHON_VERSION}.tgz"
print_status "Downloading Python ${PYTHON_VERSION}..."
cd /tmp
wget $PYTHON_URL
tar -xzf Python-${PYTHON_VERSION}.tgz
cd Python-${PYTHON_VERSION}
# Configure and build
print_status "Configuring Python build..."
./configure --prefix=$PYTHON_HOME \
--enable-optimizations \
--enable-shared \
--with-ensurepip=install
print_status "Building Python (this may take a while)..."
make -j$(nproc)
print_status "Installing Python to ${PYTHON_HOME}..."
make install
# Create symlinks
ln -sf $PYTHON_HOME/bin/python3.12 $HOME/.local/bin/python3.12
ln -sf $PYTHON_HOME/bin/pip3.12 $HOME/.local/bin/pip3.12
# Add to PATH
echo "export PATH=$HOME/.local/bin:$PATH" >> $HOME/.bashrc
export PATH=$HOME/.local/bin:$PATH
# Verify installation
if command -v python3.12 &> /dev/null; then
print_status "β
Python 3.12 built and installed successfully!"
print_status "Version: $(python3.12 --version)"
print_status "Location: $(which python3.12)"
else
print_error "β Failed to install Python 3.12"
exit 1
fi
# Clean up
cd $HOME
rm -rf /tmp/Python-${PYTHON_VERSION}*
print_status "π Python 3.12 is ready to use!"
print_status "Run 'source ~/.bashrc' or restart your shell to update PATH" |