1
+ #! /bin/bash
2
+
3
+ # Script to check and install Python 3.12 or higher
4
+ # Supports: macOS, Ubuntu, Arch Linux
5
+
6
+ set -e
7
+
8
+ REQUIRED_MAJOR=3
9
+ REQUIRED_MINOR=12
10
+
11
+ # Colors for output
12
+ RED=' \033[0;31m'
13
+ GREEN=' \033[0;32m'
14
+ YELLOW=' \033[1;33m'
15
+ BLUE=' \033[0;34m'
16
+ NC=' \033[0m' # No Color
17
+
18
+ print_info () {
19
+ echo -e " ${BLUE} [INFO]${NC} $1 "
20
+ }
21
+
22
+ print_success () {
23
+ echo -e " ${GREEN} [SUCCESS]${NC} $1 "
24
+ }
25
+
26
+ print_warning () {
27
+ echo -e " ${YELLOW} [WARNING]${NC} $1 "
28
+ }
29
+
30
+ print_error () {
31
+ echo -e " ${RED} [ERROR]${NC} $1 "
32
+ }
33
+
34
+ # Function to check Python version
35
+ check_python_version () {
36
+ local python_cmd=$1
37
+
38
+ if ! command -v " $python_cmd " & > /dev/null; then
39
+ return 1
40
+ fi
41
+
42
+ local version_output
43
+ version_output=$( $python_cmd --version 2>&1 )
44
+
45
+ # Extract version numbers
46
+ local version
47
+ version=$( echo " $version_output " | grep -oE ' [0-9]+\.[0-9]+\.[0-9]+' | head -1)
48
+
49
+ if [[ -z " $version " ]]; then
50
+ return 1
51
+ fi
52
+
53
+ local major minor patch
54
+ IFS=' .' read -r major minor patch <<< " $version"
55
+
56
+ echo " Found $python_cmd version: $version "
57
+
58
+ if [[ $major -gt $REQUIRED_MAJOR ]] ||
59
+ [[ $major -eq $REQUIRED_MAJOR && $minor -ge $REQUIRED_MINOR ]]; then
60
+ return 0
61
+ else
62
+ return 1
63
+ fi
64
+ }
65
+
66
+ # Function to detect OS
67
+ detect_os () {
68
+ if [[ " $OSTYPE " == " darwin" * ]]; then
69
+ echo " macos"
70
+ elif [[ -f /etc/arch-release ]]; then
71
+ echo " arch"
72
+ elif [[ -f /etc/debian_version ]] || [[ -f /etc/ubuntu-release ]]; then
73
+ echo " ubuntu"
74
+ else
75
+ echo " unknown"
76
+ fi
77
+ }
78
+
79
+ # Function to install Python on macOS
80
+ install_python_macos () {
81
+ print_info " Installing Python on macOS..."
82
+
83
+ if ! command -v brew & > /dev/null; then
84
+ print_error " Homebrew is not installed. Please install Homebrew first:"
85
+ echo " /bin/bash -c \"\$ (curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)\" "
86
+ exit 1
87
+ fi
88
+
89
+ print_info " Updating Homebrew..."
90
+ brew update
91
+
92
+ print_info " Installing Python 3.12..."
93
+
94
+
95
+ # Create symlinks if needed
96
+ if [[ ! -L /usr/local/bin/python3.12 ]]; then
97
+
98
+ fi
99
+ }
100
+
101
+ # Function to install Python on Ubuntu
102
+ install_python_ubuntu () {
103
+ print_info " Installing Python on Ubuntu..."
104
+
105
+ print_info " Updating package list..."
106
+ sudo apt update
107
+
108
+ # Add deadsnakes PPA for newer Python versions
109
+ print_info " Adding deadsnakes PPA..."
110
+ sudo apt install -y software-properties-common
111
+ sudo add-apt-repository -y ppa:deadsnakes/ppa
112
+ sudo apt update
113
+
114
+ print_info " Installing Python 3.12..."
115
+ sudo apt install -y python3.12 python3.12-venv python3.12-pip python3.12-dev
116
+
117
+ # Set up alternatives
118
+ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.12 1
119
+ }
120
+
121
+ # Function to install Python on Arch Linux
122
+ install_python_arch () {
123
+ print_info " Installing Python on Arch Linux..."
124
+
125
+ print_info " Updating package database..."
126
+ sudo pacman -Sy
127
+
128
+ print_info " Installing Python..."
129
+ sudo pacman -S --noconfirm python python-pip
130
+ }
131
+
132
+ # Main function
133
+ main () {
134
+ print_info " Checking Python version requirements (>= ${REQUIRED_MAJOR} .${REQUIRED_MINOR} )"
135
+
136
+ # Check various Python commands
137
+ local python_found=false
138
+ local python_commands=(" python3.12" " python3.13" " python3.14" " python3" " python" )
139
+
140
+ for cmd in " ${python_commands[@]} " ; do
141
+ if check_python_version " $cmd " ; then
142
+ print_success " Python requirement satisfied with $cmd "
143
+ python_found=true
144
+ break
145
+ fi
146
+ done
147
+
148
+ if [[ " $python_found " == " true" ]]; then
149
+ print_success " Python version check passed!"
150
+ exit 0
151
+ fi
152
+
153
+ print_warning " Python ${REQUIRED_MAJOR} .${REQUIRED_MINOR} + not found or version is too old"
154
+
155
+ # Detect OS
156
+ local os
157
+ os=$( detect_os)
158
+ print_info " Detected OS: $os "
159
+
160
+ # Ask user for permission to install
161
+ echo
162
+ print_warning " Do you want to install/update Python? (y/N)"
163
+ read -r response
164
+
165
+ if [[ ! " $response " =~ ^[Yy]$ ]]; then
166
+ print_info " Installation cancelled by user"
167
+ exit 0
168
+ fi
169
+
170
+ # Install based on OS
171
+ case $os in
172
+ " macos" )
173
+ install_python_macos
174
+ ;;
175
+ " ubuntu" )
176
+ install_python_ubuntu
177
+ ;;
178
+ " arch" )
179
+ install_python_arch
180
+ ;;
181
+ * )
182
+ print_error " Unsupported operating system: $os "
183
+ print_info " Please install Python ${REQUIRED_MAJOR} .${REQUIRED_MINOR} + manually"
184
+ exit 1
185
+ ;;
186
+ esac
187
+
188
+ # Verify installation
189
+ print_info " Verifying installation..."
190
+ sleep 2
191
+
192
+ python_found=false
193
+ for cmd in " ${python_commands[@]} " ; do
194
+ if check_python_version " $cmd " ; then
195
+ print_success " Installation successful! Python requirement satisfied with $cmd "
196
+ python_found=true
197
+ break
198
+ fi
199
+ done
200
+
201
+ if [[ " $python_found " == " false" ]]; then
202
+ print_error " Installation completed but Python ${REQUIRED_MAJOR} .${REQUIRED_MINOR} + still not found"
203
+ print_info " You may need to restart your terminal or add Python to your PATH"
204
+ exit 1
205
+ fi
206
+ }
207
+
208
+ # Run main function
209
+ main " $@ "
0 commit comments