File size: 2,673 Bytes
71852e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/bin/bash

set -e

echo "--------------------------------------------------------------------"
echo "IMPORTANT WARNING!"
echo "This script will configure Python 3.12 as the default system version."
echo "This MAY BREAK system tools that depend on the original"
echo "Python 3 version. Use at your own risk!"
echo "The safest alternative is to use virtual environments."
echo "--------------------------------------------------------------------"
read -p "Are you sure you want to continue? (y/N): " confirmation
if [[ ! "$confirmation" =~ ^[Yy]$ ]]; then
    echo "Aborting."
    exit 1
fi

echo ">>> [1/6] Installing dependencies and deadsnakes repository..."
apt update
apt install -y software-properties-common curl ca-certificates
add-apt-repository -y ppa:deadsnakes/ppa
apt update

echo ">>> [2/6] Installing Python 3.12 and associated packages..."
apt install -y python3.12 python3.12-venv python3.12-dev

echo ">>> [3/6] Installing pip for Python 3.12..."
curl -sS https://bootstrap.pypa.io/get-pip.py -o get-pip.py
/usr/bin/python3.12 get-pip.py
rm get-pip.py

echo ">>> [4/6] Configuring Python 3.12 as default using update-alternatives..."

CURRENT_PYTHON3_CMD=$(command -v python3 || echo "")
CURRENT_PYTHON3_PATH=""
if [[ -n "$CURRENT_PYTHON3_CMD" && -x "$CURRENT_PYTHON3_CMD" ]]; then
    CURRENT_PYTHON3_PATH=$(readlink -f $CURRENT_PYTHON3_CMD)
fi

echo "Registering /usr/bin/python3.12 with priority 100..."
update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.12 100

if [[ -n "$CURRENT_PYTHON3_PATH" && "$CURRENT_PYTHON3_PATH" != "/usr/bin/python3.12" && -x "$CURRENT_PYTHON3_PATH" ]]; then
    echo "Registering previous version found ($CURRENT_PYTHON3_PATH) with priority 50..."
    update-alternatives --install /usr/bin/python3 python3 "$CURRENT_PYTHON3_PATH" 50
else
    echo "Previous version not registered (or it was the same 3.12)."
fi

echo "Setting /usr/bin/python3.12 as the selection for python3..."
update-alternatives --set python3 /usr/bin/python3.12

echo ">>> [5/6] Cleaning apt cache..."
apt clean

echo ">>> [6/6] Final verification..."
echo "Default python3 version:"
python3 --version

echo "Pip version associated with default python3:"
python3 -m pip --version

echo "Default python3 executable path:"
command -v python3
ls -l $(command -v python3)

echo ""
echo "--------------------------------------------------------------------"
echo "Done! Python 3.12 should now be the default version."
echo "Remember the warning about potential system breakage."
echo "--------------------------------------------------------------------"

exit 0