-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_python.sh
More file actions
executable file
·464 lines (414 loc) · 12.6 KB
/
Copy pathupdate_python.sh
File metadata and controls
executable file
·464 lines (414 loc) · 12.6 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#!/bin/bash
# Universal Python Update Script for Linux
# Supports major distributions: Ubuntu/Debian, CentOS/RHEL/Fedora, Arch, openSUSE, Alpine
set -e # Exit on any error
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Logging function
log() {
echo -e "${BLUE}[$(date +'%Y-%m-%d %H:%M:%S')]${NC} $1"
}
error() {
echo -e "${RED}[ERROR]${NC} $1" >&2
}
warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
# Check if running as root
check_root() {
if [[ $EUID -eq 0 ]]; then
warning "Running as root. This is generally not recommended for Python installations."
read -p "Continue anyway? (y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit 1
fi
fi
}
# Detect Linux distribution
detect_distro() {
if [[ -f /etc/os-release ]]; then
. /etc/os-release
DISTRO=$ID
VERSION=$VERSION_ID
elif [[ -f /etc/redhat-release ]]; then
DISTRO="rhel"
elif [[ -f /etc/debian_version ]]; then
DISTRO="debian"
else
error "Cannot detect Linux distribution"
exit 1
fi
log "Detected distribution: $DISTRO $VERSION"
}
# Get current Python version
get_current_python_version() {
if command -v python3 &> /dev/null; then
CURRENT_VERSION=$(python3 --version 2>&1 | cut -d' ' -f2)
log "Current Python3 version: $CURRENT_VERSION"
else
warning "Python3 not found"
CURRENT_VERSION="Not installed"
fi
if command -v python &> /dev/null; then
PYTHON2_VERSION=$(python --version 2>&1 | cut -d' ' -f2)
log "Python2 version: $PYTHON2_VERSION"
fi
}
# Update package manager
update_package_manager() {
log "Updating package manager..."
case $DISTRO in
ubuntu|debian|pop|mint|kali)
sudo apt update && sudo apt upgrade -y
;;
fedora)
sudo dnf update -y
;;
centos|rhel|rocky|almalinux)
if command -v dnf &> /dev/null; then
sudo dnf update -y
else
sudo yum update -y
fi
;;
arch|manjaro|endeavouros)
sudo pacman -Syu --noconfirm
;;
opensuse*|sles)
sudo zypper refresh && sudo zypper update -y
;;
alpine)
sudo apk update && sudo apk upgrade
;;
*)
warning "Unknown distribution. Attempting generic update..."
;;
esac
}
# Install Python dependencies
install_dependencies() {
log "Installing Python build dependencies..."
case $DISTRO in
ubuntu|debian|pop|mint|kali)
sudo apt install -y \
build-essential \
zlib1g-dev \
libncurses5-dev \
libgdbm-dev \
libnss3-dev \
libssl-dev \
libsqlite3-dev \
libreadline-dev \
libffi-dev \
curl \
libbz2-dev \
pkg-config \
make \
wget
;;
fedora)
sudo dnf groupinstall -y "Development Tools"
sudo dnf install -y \
zlib-devel \
bzip2-devel \
openssl-devel \
ncurses-devel \
sqlite-devel \
readline-devel \
tk-devel \
gdbm-devel \
libffi-devel \
curl \
make \
wget
;;
centos|rhel|rocky|almalinux)
if command -v dnf &> /dev/null; then
sudo dnf groupinstall -y "Development Tools"
sudo dnf install -y \
zlib-devel \
bzip2-devel \
openssl-devel \
ncurses-devel \
sqlite-devel \
readline-devel \
tk-devel \
gdbm-devel \
libffi-devel \
curl \
make \
wget
else
sudo yum groupinstall -y "Development Tools"
sudo yum install -y \
zlib-devel \
bzip2-devel \
openssl-devel \
ncurses-devel \
sqlite-devel \
readline-devel \
tk-devel \
gdbm-devel \
libffi-devel \
curl \
make \
wget
fi
;;
arch|manjaro|endeavouros)
sudo pacman -S --noconfirm \
base-devel \
openssl \
zlib \
gdbm \
ncurses \
sqlite \
libffi \
tk \
bzip2 \
readline \
curl \
wget
;;
opensuse*|sles)
sudo zypper install -y \
gcc \
make \
zlib-devel \
bzip2 \
libopenssl-devel \
ncurses-devel \
sqlite3-devel \
readline-devel \
tk-devel \
gdbm-devel \
libffi-devel \
curl \
wget
;;
alpine)
sudo apk add \
build-base \
zlib-dev \
ncurses-dev \
openssl-dev \
sqlite-dev \
readline-dev \
tk-dev \
bzip2-dev \
libffi-dev \
curl \
wget \
make
;;
esac
}
# Update Python via package manager
update_python_package_manager() {
log "Updating Python via package manager..."
case $DISTRO in
ubuntu|debian|pop|mint|kali)
sudo apt install -y python3 python3-pip python3-venv python3-dev
# Try to install latest Python if available
sudo apt install -y python3.11 python3.11-venv python3.11-dev 2>/dev/null || true
sudo apt install -y python3.12 python3.12-venv python3.12-dev 2>/dev/null || true
;;
fedora)
sudo dnf install -y python3 python3-pip python3-devel
;;
centos|rhel|rocky|almalinux)
if command -v dnf &> /dev/null; then
sudo dnf install -y python3 python3-pip python3-devel
else
sudo yum install -y python3 python3-pip python3-devel
fi
;;
arch|manjaro|endeavouros)
sudo pacman -S --noconfirm python python-pip
;;
opensuse*|sles)
sudo zypper install -y python3 python3-pip python3-devel
;;
alpine)
sudo apk add python3 py3-pip python3-dev
;;
esac
}
# Get latest Python version from python.org
get_latest_python_version() {
log "Checking latest Python version..."
LATEST_VERSION=$(curl -s https://www.python.org/ftp/python/ | grep -oE 'href="[0-9]+\.[0-9]+\.[0-9]+/"' | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | sort -V | tail -1)
if [[ -z "$LATEST_VERSION" ]]; then
error "Could not fetch latest Python version"
exit 1
fi
log "Latest Python version: $LATEST_VERSION"
}
# Compile and install Python from source
install_python_from_source() {
read -p "Do you want to compile Python $LATEST_VERSION from source? This will take several minutes. (y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
return
fi
log "Downloading and compiling Python $LATEST_VERSION from source..."
# Create temporary directory
TEMP_DIR=$(mktemp -d)
cd "$TEMP_DIR"
# Download Python source
wget "https://www.python.org/ftp/python/$LATEST_VERSION/Python-$LATEST_VERSION.tgz"
tar -xzf "Python-$LATEST_VERSION.tgz"
cd "Python-$LATEST_VERSION"
# Configure with optimizations
./configure --enable-optimizations --with-ensurepip=install
# Compile (use all available cores)
make -j$(nproc)
# Install
sudo make altinstall
# Clean up
cd /
rm -rf "$TEMP_DIR"
success "Python $LATEST_VERSION installed from source"
}
# Update pip
update_pip() {
log "Updating pip..."
if command -v python3 &> /dev/null; then
# Check if we're in a virtual environment
if [[ -n "$VIRTUAL_ENV" ]]; then
log "Virtual environment detected, updating pip without --user flag"
python3 -m pip install --upgrade pip
else
python3 -m pip install --upgrade pip --user
fi
fi
# Update pip for the newly installed version if available
if [[ -n "$LATEST_VERSION" ]] && command -v "python${LATEST_VERSION%.*}" &> /dev/null; then
if [[ -n "$VIRTUAL_ENV" ]]; then
"python${LATEST_VERSION%.*}" -m pip install --upgrade pip
else
"python${LATEST_VERSION%.*}" -m pip install --upgrade pip --user
fi
fi
}
# Install pyenv (Python version manager)
install_pyenv() {
read -p "Do you want to install pyenv for managing multiple Python versions? (y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
return
fi
log "Installing pyenv..."
# Install pyenv
curl https://pyenv.run | bash
# Add to shell profile
SHELL_PROFILE=""
if [[ -f ~/.bashrc ]]; then
SHELL_PROFILE=~/.bashrc
elif [[ -f ~/.zshrc ]]; then
SHELL_PROFILE=~/.zshrc
fi
if [[ -n "$SHELL_PROFILE" ]]; then
echo '' >> "$SHELL_PROFILE"
echo '# Pyenv configuration' >> "$SHELL_PROFILE"
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> "$SHELL_PROFILE"
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> "$SHELL_PROFILE"
echo 'eval "$(pyenv init -)"' >> "$SHELL_PROFILE"
success "Pyenv installed. Restart your shell or run: source $SHELL_PROFILE"
log "Then you can install any Python version with: pyenv install <version>"
fi
}
# Summary
show_summary() {
echo
success "Python update process completed!"
echo
log "Current Python versions:"
if command -v python3 &> /dev/null; then
echo " Python3: $(python3 --version)"
fi
if command -v python &> /dev/null; then
echo " Python2: $(python --version 2>&1)"
fi
# Check for additional Python versions
for py_version in python3.{8..12}; do
if command -v "$py_version" &> /dev/null; then
echo " $py_version: $($py_version --version)"
fi
done
if command -v pip3 &> /dev/null; then
echo " pip3: $(pip3 --version)"
fi
echo
log "Recommendations:"
echo " - Use 'python3' and 'pip3' commands"
echo " - Create virtual environments: python3 -m venv myenv"
echo " - Activate virtual environments: source myenv/bin/activate"
echo " - Install packages in virtual environments to avoid conflicts"
}
# Main execution
main() {
log "Starting Python update process..."
check_root
detect_distro
get_current_python_version
# Ask what the user wants to do
echo
echo "What would you like to do?"
echo "1) Update Python via package manager (recommended)"
echo "2) Compile latest Python from source"
echo "3) Both package manager update and source compilation"
echo "4) Install pyenv only"
echo "5) Full update (package manager + dependencies + pyenv option)"
read -p "Enter your choice (1-5): " -n 1 -r
echo
case $REPLY in
1)
update_package_manager
update_python_package_manager
update_pip
;;
2)
install_dependencies
get_latest_python_version
install_python_from_source
update_pip
;;
3)
update_package_manager
install_dependencies
update_python_package_manager
get_latest_python_version
install_python_from_source
update_pip
;;
4)
install_dependencies
install_pyenv
;;
5)
update_package_manager
install_dependencies
update_python_package_manager
get_latest_python_version
install_python_from_source
update_pip
install_pyenv
;;
*)
error "Invalid choice"
exit 1
;;
esac
show_summary
}
# Run main function
main "$@"