-
-
Notifications
You must be signed in to change notification settings - Fork 193
Expand file tree
/
Copy pathtermux-linux-setup.sh
More file actions
903 lines (790 loc) · 32.5 KB
/
Copy pathtermux-linux-setup.sh
File metadata and controls
903 lines (790 loc) · 32.5 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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
#!/data/data/com.termux/files/usr/bin/bash
#######################################################
# Termux Linux Desktop Setup Script
#
# Features:
# - Choice of Desktop Environment (XFCE4, LXQt, MATE, KDE)
# - Smart GPU acceleration detection (Turnip/Zink)
# - Conflict-safe package installer (no more silent exits)
# - SSH server pre-installed and configured
# - Productivity & Media tools (VLC, Firefox)
# - Python 3 + pip
# - Optional: Windows App Support (Wine/Hangover + Box64)
#
# Tested on: LineageOS (Android 9+), arm64 devices
#######################################################
# Intentionally avoiding set -e (exit on error) and set -u (nounset):
# - set -e silently kills the script on any failed package install
# - set -u crashes on variables that are legitimately empty in Termux
# pipefail is kept so piped commands still report failures.
set -o pipefail
# ============== DYNAMIC PATH DETECTION ==============
# Supports standard and custom Termux installs (e.g. secondary user profiles)
TERMUX_PREFIX="${PREFIX:-/data/data/com.termux/files/usr}"
TERMUX_HOME="${HOME:-/data/data/com.termux/files/home}"
# ============== CONFIGURATION ==============
INSTALL_WINE="no"
DE_CHOICE="1"
GPU_STACK="stock"
DE_NAME="XFCE4"
CURRENT_STEP=0
# TOTAL_STEPS is set after user choices are made
LOG_FILE="$TERMUX_HOME/termux-setup.log"
# ============== COLORS ==============
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
WHITE='\033[1;37m'
GRAY='\033[0;90m'
NC='\033[0m'
BOLD='\033[1m'
# ============== LOGGING ==============
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" >> "$LOG_FILE"
}
# ============== PROGRESS BAR ==============
update_progress() {
CURRENT_STEP=$((CURRENT_STEP + 1))
local PERCENT=$((CURRENT_STEP * 100 / TOTAL_STEPS))
local FILLED=$((PERCENT / 5))
local EMPTY=$((20 - FILLED))
local BAR="${GREEN}"
for ((i=0; i<FILLED; i++)); do BAR+="█"; done
BAR+="${GRAY}"
for ((i=0; i<EMPTY; i++)); do BAR+="░"; done
BAR+="${NC}"
echo ""
echo -e "${WHITE}────────────────────────────────────────────────────────────${NC}"
echo -e "${CYAN} PROGRESS: ${WHITE}Step ${CURRENT_STEP}/${TOTAL_STEPS}${NC} ${BAR} ${WHITE}${PERCENT}%${NC}"
echo -e "${WHITE}────────────────────────────────────────────────────────────${NC}"
echo ""
}
# ============== SPINNER ==============
spinner() {
local pid=$1
local message=$2
local spin=('⠋' '⠙' '⠸' '⠴' '⠦' '⠇')
local i=0
while kill -0 "$pid" 2>/dev/null; do
printf "\r ${CYAN}${spin[$i]}${NC} %s " "$message"
i=$(( (i + 1) % 6 ))
sleep 0.1
done
wait "$pid"
local exit_code=$?
if [ "$exit_code" -eq 0 ]; then
printf "\r ${GREEN}✔${NC} %-55s\n" "$message"
log "OK: $message"
else
printf "\r ${RED}✘${NC} %-55s ${RED}(failed — see $LOG_FILE)${NC}\n" "$message"
log "FAILED: $message"
fi
return "$exit_code"
}
# Same as spinner, but a failure is reported as a warning rather than an error.
# For packages the desktop still works without.
spinner_optional() {
local pid=$1
local message=$2
local spin=('⠋' '⠙' '⠸' '⠴' '⠦' '⠇')
local i=0
while kill -0 "$pid" 2>/dev/null; do
printf "\r ${CYAN}${spin[$i]}${NC} %s " "$message"
i=$(( (i + 1) % 6 ))
sleep 0.1
done
wait "$pid"
local exit_code=$?
if [ "$exit_code" -eq 0 ]; then
printf "\r ${GREEN}✔${NC} %-55s\n" "$message"
log "OK: $message"
else
printf "\r ${YELLOW}⚠${NC} %-55s ${YELLOW}(optional — skipped)${NC}\n" "$message"
log "OPTIONAL FAILED: $message"
fi
return 0
}
# ============== DPKG / APT HELPERS ==============
# dpkg -s also succeeds for packages that were removed but left their config
# behind, so ask for the real status instead.
pkg_installed() {
[ "$(dpkg-query -W -f='${db:Status-Status}' "$1" 2>/dev/null)" = "installed" ]
}
pkg_version() {
dpkg-query -W -f='${Version}' "$1" 2>/dev/null
}
# apt-cache show prints one stanza per available version, highest first.
# We only care about the candidate, so cut at the first blank line.
apt_field() {
apt-cache show "$1" 2>/dev/null \
| sed -n '1,/^$/p' \
| grep -i "^$2:" \
| head -1 \
| sed "s/^[^:]*:[[:space:]]*//"
}
# Strip version constraints and whitespace from a dependency list, one per line.
# "libmesa, ndk-sysroot (<< 23b-6), mesa" → "libmesa\nndk-sysroot\nmesa"
dep_names() {
echo "$1" | tr ',' '\n' | sed 's/(.*//' | tr -d ' \t' | grep -v '^$'
}
# Is a single Conflicts entry actually violated right now?
#
# This is the bit the old version got wrong. Termux's mesa-zink declares
# Conflicts: libmesa, ndk-sysroot (<< 23b-6), mesa
# Treating "ndk-sysroot (<< 23b-6)" as a plain name means any modern
# ndk-sysroot looks like a conflict and mesa-zink gets skipped, even though the
# constraint only covers versions older than 23b-6. (issue #4)
conflict_is_real() {
local item="$1"
local cname cop cver iver
cname=$(echo "$item" | sed 's/(.*//' | tr -d ' \t')
[ -z "$cname" ] && return 1
pkg_installed "$cname" || return 1
# No version constraint → any installed version conflicts.
case "$item" in
*"("*")"*) ;;
*) return 0 ;;
esac
cop=$(echo "$item" | sed -n 's/.*(\([<>=]\{1,2\}\)[[:space:]]*[^)]*).*/\1/p')
cver=$(echo "$item" | sed -n 's/.*([<>=]\{1,2\}[[:space:]]*\([^)]*\)).*/\1/p')
iver=$(pkg_version "$cname")
if [ -z "$cop" ] || [ -z "$cver" ] || [ -z "$iver" ]; then
return 0
fi
case "$cop" in
"<<") cop="lt" ;;
"<="|"<") cop="le" ;;
"=") cop="eq" ;;
">="|">") cop="ge" ;;
">>") cop="gt" ;;
*) return 0 ;;
esac
dpkg --compare-versions "$iver" "$cop" "$cver"
}
# Name of an installed package that already Provides "$1", if any.
provider_of() {
dpkg-query -W -f='${db:Status-Status}|${Package}|${Provides}\n' 2>/dev/null \
| awk -F'|' -v want="$1" '
$1 == "installed" {
n = split($3, a, ",")
for (i = 1; i <= n; i++) {
p = a[i]; sub(/\(.*/, "", p); gsub(/[ \t]/, "", p)
if (p == want) { print $2; exit }
}
}'
}
# ============== CONFLICT-SAFE PACKAGE INSTALLER ==============
#
# Why this exists:
# Termux has packages that hard-conflict with each other
# (e.g. vulkan-loader-android vs vulkan-loader-generic).
# Raw apt-get errors out on conflicts and — combined with set -e — would
# silently kill the entire script.
#
# What this does:
# 1. Skips if the package is already installed, or already provided by
# something installed (vulkan-loader-generic Provides vulkan-loader-android).
# 2. Reads the package's declared Conflicts and evaluates each one properly,
# version constraints included.
# 3. Ignores a conflict the package also Replaces/Provides — that is a
# supported swap (mesa → mesa-zink), not a breakage, and apt handles it.
# 4. Only then skips, with a warning, on a conflict that is genuinely unsafe.
#
# Usage: safe_install_pkg <pkg> [display name] [optional]
# Passing "optional" as the third argument downgrades a failed install from a
# red error to a yellow warning, for packages the desktop can live without.
#
safe_install_pkg() {
local pkg=$1
local name=${2:-$pkg}
local optional=${3:-}
if pkg_installed "$pkg"; then
printf " ${GRAY}~${NC} %-55s ${GRAY}(already installed)${NC}\n" "$name"
log "SKIP (already installed): $pkg"
return 0
fi
# Already satisfied by a virtual provider?
local provider
provider=$(provider_of "$pkg")
if [ -n "$provider" ]; then
printf " ${GRAY}~${NC} %-55s ${GRAY}(provided by %s)${NC}\n" "$name" "$provider"
log "SKIP (provided by $provider): $pkg"
return 0
fi
if ! apt-cache show "$pkg" >/dev/null 2>&1; then
printf " ${YELLOW}⚠${NC} %-55s ${YELLOW}(not in any enabled repo)${NC}\n" "$name"
log "SKIP (unavailable): $pkg"
return 1
fi
# Conflicts the package can legitimately take over are not blockers.
local swappable
swappable=$(dep_names "$(apt_field "$pkg" Replaces)
$(apt_field "$pkg" Provides)")
local item cname
while IFS= read -r item; do
[ -z "$(echo "$item" | tr -d ' \t')" ] && continue
if conflict_is_real "$item"; then
cname=$(echo "$item" | sed 's/(.*//' | tr -d ' \t')
if echo "$swappable" | grep -qx "$cname"; then
log "CONFLICT OK (${pkg} replaces ${cname}): apt will swap them"
else
printf " ${YELLOW}⚠${NC} %-55s ${YELLOW}(skipped — conflicts with: %s)${NC}\n" \
"$name" "$cname"
log "SKIP (conflict with $cname): $pkg"
return 1
fi
fi
done <<< "$(apt_field "$pkg" Conflicts | tr ',' '\n')"
(DEBIAN_FRONTEND=noninteractive apt-get install -y \
-o Dpkg::Options::="--force-confold" \
"$pkg" >> "$LOG_FILE" 2>&1) &
if [ "$optional" = "optional" ]; then
spinner_optional $! "Installing ${name}..."
else
spinner $! "Installing ${name}..."
fi
}
# ============== BANNER ==============
show_banner() {
clear
echo -e "${CYAN}"
cat << 'BANNER'
╔══════════════════════════════════════════════════════╗
║ ║
║ Termux Linux Desktop Setup Script ║
║ Run full Linux on your Android ║
║ ║
╚══════════════════════════════════════════════════════╝
BANNER
echo -e "${NC}"
echo -e "${GRAY} Detailed logs → $LOG_FILE${NC}"
echo ""
}
# ============== ENVIRONMENT & USER SELECTION ==============
setup_environment() {
log "=== Setup started ==="
echo -e "${PURPLE}[*] Detecting your device...${NC}"
echo ""
DEVICE_MODEL=$(getprop ro.product.model 2>/dev/null || echo "Unknown")
DEVICE_BRAND=$(getprop ro.product.brand 2>/dev/null || echo "Unknown")
ANDROID_VERSION=$(getprop ro.build.version.release 2>/dev/null || echo "Unknown")
CPU_ABI=$(getprop ro.product.cpu.abi 2>/dev/null || echo "arm64-v8a")
# Use actual EGL/hardware properties — NOT brand name.
# Brand-based detection is wrong: Samsung ships both Adreno (Snapdragon)
# and Mali (Exynos) phones, so checking "samsung" tells you nothing.
GPU_RENDERER=$(getprop ro.hardware.egl 2>/dev/null || echo "")
GPU_VENDOR_PROP=$(getprop ro.hardware 2>/dev/null || echo "")
echo -e " ${CYAN}Device :${NC} ${WHITE}${DEVICE_BRAND} ${DEVICE_MODEL}${NC}"
echo -e " ${CYAN}Android:${NC} ${WHITE}Android ${ANDROID_VERSION}${NC}"
echo -e " ${CYAN}CPU ABI:${NC} ${WHITE}${CPU_ABI}${NC}"
if [[ "$GPU_RENDERER" == *"adreno"* ]] || \
[[ "$GPU_RENDERER" == *"Adreno"* ]] || \
[[ "$GPU_VENDOR_PROP" == *"adreno"* ]] || \
[[ "$GPU_VENDOR_PROP" == *"msm"* ]] || \
[[ "$GPU_VENDOR_PROP" == *"qcom"* ]]; then
GPU_DRIVER="freedreno"
echo -e " ${CYAN}GPU :${NC} ${WHITE}Adreno / Qualcomm — Turnip hardware acceleration ✔${NC}"
else
GPU_DRIVER="zink_native"
echo -e " ${CYAN}GPU :${NC} ${YELLOW}Non-Adreno (Mali / PowerVR / other)${NC}"
echo -e " ${YELLOW}Falling back to Zink/Vulkan. Use XFCE4 or LXQt for best results.${NC}"
fi
echo ""
# ---- Desktop Environment ----
echo -e "${CYAN}Choose your Desktop Environment:${NC}"
echo ""
echo -e " ${WHITE}1) XFCE4${NC} — Fast, customizable, macOS-style dock. ${GREEN}(Recommended)${NC}"
echo -e " ${WHITE}2) LXQt${NC} — Ultra-lightweight. Best for low-RAM phones."
echo -e " ${WHITE}3) MATE${NC} — Classic look. Moderate resource use."
echo -e " ${WHITE}4) KDE Plasma${NC} — Modern Windows-style UI. Requires 4 GB+ RAM."
echo ""
while true; do
read -rp " Enter number (1-4) [default: 1]: " DE_INPUT
DE_INPUT=${DE_INPUT:-1}
if [[ "$DE_INPUT" =~ ^[1-4]$ ]]; then
DE_CHOICE="$DE_INPUT"; break
else
echo -e " ${RED}Invalid — enter 1, 2, 3, or 4.${NC}"
fi
done
case $DE_CHOICE in
1) DE_NAME="XFCE4";;
2) DE_NAME="LXQt";;
3) DE_NAME="MATE";;
4) DE_NAME="KDE Plasma";;
esac
echo -e "\n ${GREEN}✔ Selected: ${BOLD}${DE_NAME}${NC}"
# ---- Optional: Wine ----
echo ""
echo -e "${CYAN}Optional: Install Windows app support (Wine + Box64/Hangover)?${NC}"
echo -e " ${GRAY}Adds ~500 MB. Runs some Windows x86 apps on ARM.${NC}"
echo ""
while true; do
read -rp " Install Wine? (y/n) [default: n]: " WINE_INPUT
WINE_INPUT=${WINE_INPUT:-n}
case "$WINE_INPUT" in
[Yy]*) INSTALL_WINE="yes"; echo -e " ${GREEN}✔ Wine will be installed.${NC}"; break;;
[Nn]*) INSTALL_WINE="no"; echo -e " ${GRAY} Wine skipped.${NC}"; break;;
*) echo -e " ${RED}Please enter y or n.${NC}";;
esac
done
TOTAL_STEPS=10
[ "$INSTALL_WINE" == "yes" ] && TOTAL_STEPS=11
log "DE=$DE_NAME, GPU=$GPU_DRIVER, Wine=$INSTALL_WINE"
sleep 1
}
# ============== STEP 1: UPDATE SYSTEM ==============
step_update() {
update_progress
echo -e "${PURPLE}[Step ${CURRENT_STEP}/${TOTAL_STEPS}] Updating system packages...${NC}"
echo ""
(DEBIAN_FRONTEND=noninteractive apt-get update -y >> "$LOG_FILE" 2>&1) &
spinner $! "Updating package lists..."
# Run pkg upgrade in FOREGROUND (not backgrounded via &) so we can catch
# the libpcre/libandroid-selinux crash that kills binaries like 'sleep'
# mid-session when core libraries are replaced. If it fails, we tell the
# user exactly what to do rather than dying silently.
echo -e " ${CYAN}⠿${NC} Upgrading packages (may take a few minutes)..."
if ! DEBIAN_FRONTEND=noninteractive pkg upgrade -y \
-o Dpkg::Options::="--force-confold" >> "$LOG_FILE" 2>&1; then
echo ""
echo -e " ${YELLOW}⚠ Upgrade hit a library conflict — this is common on first run.${NC}"
echo ""
echo -e " ${WHITE}To fix:${NC}"
echo -e " 1. Close Termux completely (swipe away from recents)"
echo -e " 2. Reopen Termux and run: ${GREEN}pkg upgrade -y${NC}"
echo -e " 3. Then re-run this script"
echo ""
echo -e " ${GRAY}Full error: $LOG_FILE${NC}"
exit 1
fi
echo -e " ${GREEN}✔${NC} Packages upgraded."
}
# ============== STEP 2: REPOSITORIES ==============
step_repos() {
update_progress
echo -e "${PURPLE}[Step ${CURRENT_STEP}/${TOTAL_STEPS}] Adding package repositories...${NC}"
echo ""
safe_install_pkg "x11-repo" "X11 Repository"
safe_install_pkg "tur-repo" "TUR Repository (Firefox, extra apps)"
# MUST refresh after adding repos — otherwise packages from x11-repo
# and tur-repo (termux-x11-nightly, firefox, etc.) won't be found.
echo ""
(DEBIAN_FRONTEND=noninteractive apt-get update -y >> "$LOG_FILE" 2>&1) &
spinner $! "Refreshing package lists (post-repo)..."
}
# ============== STEP 3: TERMUX-X11 ==============
step_x11() {
update_progress
echo -e "${PURPLE}[Step ${CURRENT_STEP}/${TOTAL_STEPS}] Installing Termux-X11 display server...${NC}"
echo ""
safe_install_pkg "termux-x11-nightly" "Termux-X11 Display Server"
safe_install_pkg "xorg-xrandr" "XRandR (Display Settings)"
}
# ============== STEP 4: DESKTOP ENVIRONMENT ==============
step_desktop() {
update_progress
echo -e "${PURPLE}[Step ${CURRENT_STEP}/${TOTAL_STEPS}] Installing ${DE_NAME} Desktop...${NC}"
echo ""
# None of the Termux DE metapackages depend on dbus, but every one of them
# needs a session bus to reach its settings daemon. Without it XFCE greets
# you with "Unable to contact settings server". (issue #5)
safe_install_pkg "dbus" "D-Bus (session message bus)"
case $DE_CHOICE in
1)
safe_install_pkg "xfce4" "XFCE4 Desktop"
safe_install_pkg "xfce4-terminal" "XFCE4 Terminal"
safe_install_pkg "xfce4-whiskermenu-plugin" "Whisker Menu Plugin"
safe_install_pkg "plank-reloaded" "Plank Dock"
safe_install_pkg "thunar" "Thunar File Manager"
safe_install_pkg "mousepad" "Mousepad Text Editor"
;;
2)
safe_install_pkg "lxqt" "LXQt Desktop"
safe_install_pkg "qterminal" "QTerminal"
safe_install_pkg "pcmanfm-qt" "PCManFM-Qt File Manager"
safe_install_pkg "featherpad" "FeatherPad Text Editor"
;;
3)
safe_install_pkg "mate" "MATE Desktop"
safe_install_pkg "mate-tweak" "MATE Tweak"
safe_install_pkg "plank-reloaded" "Plank Dock"
safe_install_pkg "mate-terminal" "MATE Terminal"
;;
4)
safe_install_pkg "plasma-desktop" "KDE Plasma Desktop"
safe_install_pkg "konsole" "Konsole Terminal"
safe_install_pkg "dolphin" "Dolphin File Manager"
;;
esac
}
# ============== STEP 5: GPU DRIVERS ==============
#
# Termux's own "mesa" package has shipped the Zink Gallium driver
# (lib/dri/zink_dri.so) since Mesa 23, so GALLIUM_DRIVER=zink works against the
# stock, current Mesa. The TUR "mesa-zink" package is Mesa 22.0.5 from 2022:
# installing it REMOVES mesa and drops you back four years, and its companion
# mesa-zink-vulkan-icd-freedreno ships the same libvulkan_freedreno.so as the
# main-repo Turnip driver, so dpkg aborts with a file-overwrite error if both
# land on the device. That was issue #4.
#
# So: prefer the stock stack, and only use the TUR one on installs whose Mesa
# genuinely has no Zink, or that already have mesa-zink from an earlier run.
step_gpu() {
update_progress
echo -e "${PURPLE}[Step ${CURRENT_STEP}/${TOTAL_STEPS}] Installing GPU acceleration...${NC}"
echo ""
GPU_STACK="stock"
if pkg_installed "mesa-zink"; then
# Don't rip out a working legacy setup on re-run — the two stacks
# overwrite each other's files.
GPU_STACK="tur"
printf " ${GRAY}~${NC} %-55s ${GRAY}(keeping TUR stack)${NC}\n" "Existing mesa-zink install detected"
log "GPU stack: tur (mesa-zink already installed)"
else
safe_install_pkg "mesa" "Mesa (OpenGL + Zink)"
if [ ! -f "${TERMUX_PREFIX}/lib/dri/zink_dri.so" ]; then
GPU_STACK="tur"
echo -e " ${YELLOW}⚠${NC} This Mesa build has no Zink — falling back to TUR mesa-zink."
log "GPU stack: tur (no zink_dri.so in stock mesa)"
else
log "GPU stack: stock (zink_dri.so present)"
fi
fi
# vulkan-loader-generic is the modern loader and is what every
# mesa-vulkan-icd-* depends on. Do NOT ask for vulkan-loader-android:
# vulkan-loader-generic both Provides AND Conflicts with it, so requesting
# it on a normal install aborts the apt transaction for zero benefit.
safe_install_pkg "vulkan-loader-generic" "Vulkan Loader"
if [ "$GPU_STACK" == "tur" ]; then
safe_install_pkg "mesa-zink" "Mesa Zink (OpenGL on Vulkan)"
if [ "$GPU_DRIVER" == "freedreno" ]; then
safe_install_pkg "mesa-zink-vulkan-icd-freedreno" "Freedreno Vulkan ICD (Turnip)"
else
safe_install_pkg "mesa-zink-vulkan-icd-swrast" "SwRast Vulkan ICD (software)"
fi
else
if [ "$GPU_DRIVER" == "freedreno" ]; then
safe_install_pkg "mesa-vulkan-icd-freedreno" "Freedreno Vulkan ICD (Turnip)"
else
safe_install_pkg "mesa-vulkan-icd-swrast" "SwRast Vulkan ICD (software)"
fi
fi
# Verify here rather than letting a missing ICD show up as a black screen
# ten minutes from now.
echo ""
if [ "$GPU_DRIVER" == "freedreno" ]; then
if [ -f "${TERMUX_PREFIX}/share/vulkan/icd.d/freedreno_icd.aarch64.json" ]; then
echo -e " ${GREEN}✔${NC} Turnip (Adreno) Vulkan driver in place."
else
echo -e " ${YELLOW}⚠${NC} Turnip ICD missing — the desktop will fall back to software rendering."
log "WARNING: freedreno ICD json not found"
fi
fi
log "GPU stack final: $GPU_STACK"
}
# ============== STEP 6: AUDIO ==============
step_audio() {
update_progress
echo -e "${PURPLE}[Step ${CURRENT_STEP}/${TOTAL_STEPS}] Installing audio server...${NC}"
echo ""
safe_install_pkg "pulseaudio" "PulseAudio"
}
# ============== STEP 7: APPS ==============
step_apps() {
update_progress
echo -e "${PURPLE}[Step ${CURRENT_STEP}/${TOTAL_STEPS}] Installing applications...${NC}"
echo ""
safe_install_pkg "firefox" "Firefox Browser"
safe_install_pkg "vlc" "VLC Media Player"
safe_install_pkg "git" "Git Version Control"
safe_install_pkg "wget" "Wget"
safe_install_pkg "curl" "cURL"
safe_install_pkg "openssh" "OpenSSH (SSH server + client)"
}
# ============== STEP 8: PYTHON ==============
step_python() {
update_progress
echo -e "${PURPLE}[Step ${CURRENT_STEP}/${TOTAL_STEPS}] Installing Python...${NC}"
echo ""
safe_install_pkg "python" "Python 3"
safe_install_pkg "python-pip" "pip (Python Package Manager)"
}
# ============== STEP 9 (OPTIONAL): WINE ==============
step_wine() {
update_progress
echo -e "${PURPLE}[Step ${CURRENT_STEP}/${TOTAL_STEPS}] Installing Windows support (Wine + Box64)...${NC}"
echo ""
(pkg remove wine-stable -y >> "$LOG_FILE" 2>&1 || true) &
spinner $! "Removing old Wine versions (if any)..."
safe_install_pkg "hangover-wine" "Hangover Wine"
safe_install_pkg "hangover-wowbox64" "Box64 Wrapper"
local WINE_BIN="${TERMUX_PREFIX}/opt/hangover-wine/bin"
if [ -f "${WINE_BIN}/wine" ]; then
ln -sf "${WINE_BIN}/wine" "${TERMUX_PREFIX}/bin/wine"
ln -sf "${WINE_BIN}/winecfg" "${TERMUX_PREFIX}/bin/winecfg"
echo -e " ${GREEN}✔ Wine binaries linked to PATH.${NC}"
log "Wine symlinks created."
else
echo -e " ${YELLOW} Wine binary not found at expected path — symlinks skipped.${NC}"
log "WARNING: Wine binary not found at $WINE_BIN"
fi
}
# ============== STEP 10: LAUNCHER SCRIPTS ==============
step_launchers() {
update_progress
echo -e "${PURPLE}[Step ${CURRENT_STEP}/${TOTAL_STEPS}] Creating startup scripts...${NC}"
echo ""
mkdir -p ~/.config
XDG_INJECT="export XDG_DATA_DIRS=${TERMUX_PREFIX}/share:\${XDG_DATA_DIRS:-}\nexport XDG_CONFIG_DIRS=${TERMUX_PREFIX}/etc/xdg:\${XDG_CONFIG_DIRS:-}\nexport XDG_RUNTIME_DIR=\${TMPDIR:-${TERMUX_PREFIX}/tmp}"
if [ "$DE_CHOICE" == "4" ]; then
mkdir -p ~/.config/plasma-workspace/env
{
echo "#!${TERMUX_PREFIX}/bin/bash"
echo -e "$XDG_INJECT"
} > ~/.config/plasma-workspace/env/xdg_fix.sh
chmod +x ~/.config/plasma-workspace/env/xdg_fix.sh
fi
cat > ~/.config/linux-gpu.sh << EOF
#!${TERMUX_PREFIX}/bin/bash
# GPU & Mesa environment — sourced by start-linux.sh
export MESA_NO_ERROR=1
export MESA_GL_VERSION_OVERRIDE=4.6
export MESA_GLES_VERSION_OVERRIDE=3.2
export GALLIUM_DRIVER=zink
export MESA_LOADER_DRIVER_OVERRIDE=zink
export TU_DEBUG=noconform
export MESA_VK_WSI_PRESENT_MODE=immediate
export ZINK_DESCRIPTORS=lazy
EOF
if [ "$DE_CHOICE" == "4" ]; then
echo "export KWIN_COMPOSE=O2ES" >> ~/.config/linux-gpu.sh
else
echo -e "$XDG_INJECT" >> ~/.config/linux-gpu.sh
fi
if [ "$GPU_DRIVER" == "freedreno" ]; then
echo "export VK_ICD_FILENAMES=${TERMUX_PREFIX}/share/vulkan/icd.d/freedreno_icd.aarch64.json" \
>> ~/.config/linux-gpu.sh
fi
if [ "$DE_CHOICE" == "1" ] || [ "$DE_CHOICE" == "3" ]; then
mkdir -p ~/.config/autostart
cat > ~/.config/autostart/plank.desktop << 'PLANKEOF'
[Desktop Entry]
Type=Application
Name=Plank
Exec=plank
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
PLANKEOF
else
rm -f ~/.config/autostart/plank.desktop 2>/dev/null || true
fi
case $DE_CHOICE in
1)
EXEC_CMD="exec startxfce4"
KILL_CMD="pkill -9 xfce4-session 2>/dev/null; pkill -9 plank 2>/dev/null"
;;
2)
EXEC_CMD="exec startlxqt"
KILL_CMD="pkill -9 lxqt-session 2>/dev/null"
;;
3)
EXEC_CMD="exec mate-session"
KILL_CMD="pkill -9 mate-session 2>/dev/null; pkill -9 plank 2>/dev/null"
;;
4)
EXEC_CMD="(sleep 5 && pkill -9 plasmashell && plasmashell) >/dev/null 2>&1 &\nexec startplasma-x11"
KILL_CMD="pkill -9 startplasma-x11 2>/dev/null; pkill -9 kwin_x11 2>/dev/null"
;;
esac
cat > ~/start-linux.sh << LAUNCHEREOF
#!${TERMUX_PREFIX}/bin/bash
echo ""
echo "[*] Starting ${DE_NAME} on Termux-X11..."
echo ""
source ~/.config/linux-gpu.sh 2>/dev/null
echo "[*] Cleaning up old sessions..."
pkill -9 -f "termux.x11" 2>/dev/null || true
${KILL_CMD} || true
pkill -9 -f "dbus-daemon" 2>/dev/null || true
sleep 0.5
# ---- D-Bus session bus ----
# XFCE, MATE and KDE all reach their settings daemon over the session bus.
# If no bus is running -- or if a previous run was SIGKILLed and left a dead
# socket plus a stale address in ~/.dbus behind -- the desktop comes up with
# "Unable to contact settings server" and
# "Failed to connect to socket .../dbus-XXXX: Connection refused".
# Clearing the stale state and starting the bus at a fixed, predictable
# address fixes both. (issue #5)
echo "[*] Starting D-Bus session bus..."
export XDG_RUNTIME_DIR="\${TMPDIR:-${TERMUX_PREFIX}/tmp}"
mkdir -p "\$XDG_RUNTIME_DIR" 2>/dev/null || true
chmod 700 "\$XDG_RUNTIME_DIR" 2>/dev/null || true
DBUS_DIR="${TERMUX_PREFIX}/var/run/dbus"
rm -rf "\$HOME/.dbus" 2>/dev/null || true
rm -f "${TERMUX_PREFIX}/tmp/dbus-"* 2>/dev/null || true
mkdir -p "\$DBUS_DIR" "${TERMUX_PREFIX}/var/lib/dbus"
rm -f "\$DBUS_DIR/session_bus_socket"
dbus-uuidgen --ensure >/dev/null 2>&1 || true
export DBUS_SESSION_BUS_ADDRESS="unix:path=\$DBUS_DIR/session_bus_socket"
if command -v dbus-daemon >/dev/null 2>&1; then
dbus-daemon --session --address="\$DBUS_SESSION_BUS_ADDRESS" --nopidfile --fork
else
echo "[!] dbus-daemon not found. Run: pkg install dbus"
unset DBUS_SESSION_BUS_ADDRESS
fi
echo "[*] Starting PulseAudio..."
unset PULSE_SERVER
pulseaudio --kill 2>/dev/null || true
sleep 0.3
pulseaudio --start --exit-idle-time=-1
sleep 1
pactl load-module module-native-protocol-tcp auth-ip-acl=127.0.0.1 auth-anonymous=1 2>/dev/null || true
export PULSE_SERVER=127.0.0.1
echo "[*] Starting Termux-X11 display server..."
termux-x11 :0 -ac &
sleep 3
export DISPLAY=:0
# Daemons the bus starts on demand (xfconfd, xfsettingsd, ...) inherit the
# bus's environment, not this shell's -- so push DISPLAY across explicitly.
dbus-update-activation-environment DISPLAY XAUTHORITY PULSE_SERVER XDG_DATA_DIRS XDG_CONFIG_DIRS XDG_RUNTIME_DIR >/dev/null 2>&1 || true
echo ""
echo "─────────────────────────────────────────────────"
echo " ✔ Desktop launching! Open the Termux-X11 app."
echo "─────────────────────────────────────────────────"
echo ""
${EXEC_CMD}
LAUNCHEREOF
chmod +x ~/start-linux.sh
echo -e " ${GREEN}✔ Created ~/start-linux.sh${NC}"
cat > ~/stop-linux.sh << STOPEOF
#!${TERMUX_PREFIX}/bin/bash
echo "[*] Stopping ${DE_NAME}..."
pkill -9 -f "termux.x11" 2>/dev/null || true
pkill -9 -f "pulseaudio" 2>/dev/null || true
${KILL_CMD} || true
pkill -9 -f "dbus-daemon" 2>/dev/null || true
# Leave no dead socket for the next start to trip over.
rm -f "${TERMUX_PREFIX}/var/run/dbus/session_bus_socket" 2>/dev/null || true
rm -rf "\$HOME/.dbus" 2>/dev/null || true
echo "[✔] Desktop stopped."
STOPEOF
chmod +x ~/stop-linux.sh
echo -e " ${GREEN}✔ Created ~/stop-linux.sh${NC}"
}
# ============== STEP 11: DESKTOP SHORTCUTS ==============
step_shortcuts() {
update_progress
echo -e "${PURPLE}[Step ${CURRENT_STEP}/${TOTAL_STEPS}] Creating desktop shortcuts...${NC}"
echo ""
mkdir -p ~/Desktop
cat > ~/Desktop/Firefox.desktop << 'EOF'
[Desktop Entry]
Name=Firefox
Exec=firefox
Icon=firefox
Type=Application
Categories=Network;WebBrowser;
EOF
cat > ~/Desktop/VLC.desktop << 'EOF'
[Desktop Entry]
Name=VLC Media Player
Exec=vlc
Icon=vlc
Type=Application
Categories=Video;AudioVideo;Player;
EOF
local term_cmd term_icon
case $DE_CHOICE in
1) term_cmd="xfce4-terminal"; term_icon="xfce4-terminal";;
2) term_cmd="qterminal"; term_icon="qterminal";;
3) term_cmd="mate-terminal"; term_icon="mate-terminal";;
4) term_cmd="konsole"; term_icon="utilities-terminal";;
esac
cat > ~/Desktop/Terminal.desktop << EOF
[Desktop Entry]
Name=Terminal
Exec=${term_cmd}
Icon=${term_icon}
Type=Application
Categories=System;TerminalEmulator;
EOF
if [ "$INSTALL_WINE" == "yes" ]; then
cat > ~/Desktop/Wine_Config.desktop << 'EOF'
[Desktop Entry]
Name=Wine Config
Exec=wine winecfg
Icon=wine
Type=Application
Categories=Utility;
EOF
fi
chmod +x ~/Desktop/*.desktop 2>/dev/null || true
echo -e " ${GREEN}✔ Desktop shortcuts created.${NC}"
}
# ============== COMPLETION ==============
show_completion() {
echo ""
echo -e "${GREEN}"
cat << 'COMPLETE'
╔══════════════════════════════════════════════════════╗
║ ║
║ ✔ INSTALLATION COMPLETE! ║
║ ║
╚══════════════════════════════════════════════════════╝
COMPLETE
echo -e "${NC}"
echo -e " ${WHITE}Desktop : ${GREEN}${DE_NAME}${NC}"
echo -e " ${WHITE}GPU : ${GREEN}${GPU_DRIVER}${NC}"
echo -e " ${WHITE}Wine : ${GREEN}${INSTALL_WINE}${NC}"
echo ""
echo -e "${YELLOW} ─────────────────────────────────────────────────${NC}"
echo -e " ${WHITE}▶ START DESKTOP:${NC} ${GREEN}bash ~/start-linux.sh${NC}"
echo -e " ${WHITE}■ STOP DESKTOP: ${NC} ${GREEN}bash ~/stop-linux.sh${NC}"
echo -e "${YELLOW} ─────────────────────────────────────────────────${NC}"
echo ""
echo -e " ${CYAN}SSH into this device from another machine:${NC}"
echo -e " 1. Start SSH server: ${GREEN}sshd${NC}"
echo -e " 2. Set a password: ${GREEN}passwd${NC}"
echo -e " 3. Find your IP: ${GREEN}ip addr show wlan0 | grep 'inet '${NC}"
echo -e " 4. Connect from PC: ${GREEN}ssh \$(whoami)@<your-ip> -p 8022${NC}"
echo ""
echo -e " ${GRAY}Full install log: $LOG_FILE${NC}"
echo ""
}
# ============== MAIN ==============
main() {
echo "" > "$LOG_FILE"
log "termux-linux-setup.sh started"
# Prevent Android from suspending Termux mid-install when screen turns off.
# A suspended Termux kills background apt processes, causing random failures.
if command -v termux-wake-lock &>/dev/null; then
termux-wake-lock
log "Wake lock acquired."
else
echo -e "${YELLOW} [!] termux-wake-lock unavailable — keep your screen on during install.${NC}"
echo ""
fi
show_banner
setup_environment
step_update
step_repos
step_x11
step_desktop
step_gpu
step_audio
step_apps
step_python
[ "$INSTALL_WINE" == "yes" ] && step_wine
step_launchers
step_shortcuts
show_completion
if command -v termux-wake-unlock &>/dev/null; then
termux-wake-unlock
log "Wake lock released."
fi
}
main