-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinstall-nvidia-cuda.sh
More file actions
572 lines (486 loc) · 18.3 KB
/
Copy pathinstall-nvidia-cuda.sh
File metadata and controls
572 lines (486 loc) · 18.3 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
#!/usr/bin/env bash
# ============================================================
# NVIDIA Driver + (optional) CUDA Toolkit for Debian 13 (Trixie)
# "Debian-clean" edition (APT-first, no mixed installer by default)
# Author: Dennis Hilk
# Version: 1.2.0
#
# Features:
# - Strict mode + logging
# - Safe APT/dpkg lock handling (no blind killing)
# - Detect NVIDIA GPU
# - Checks non-free + non-free-firmware APT sources
# - Stable driver install (Debian repo) or Backports install
# - Optional CUDA Toolkit (Debian package)
# - Nouveau enable, full clean remove
# - Secure Boot + Wayland hints
# - Uses /etc/X11/xorg.conf.d (modular) instead of patching xorg.conf
# ============================================================
set -Eeuo pipefail
# ----------------------------
# Logging
# ----------------------------
LOGFILE="/var/log/nvidia-optimizer.log"
mkdir -p "$(dirname "$LOGFILE")"
exec > >(tee -a "$LOGFILE") 2>&1
# ----------------------------
# Colors
# ----------------------------
GREEN="\033[0;32m"; RED="\033[0;31m"; YELLOW="\033[1;33m"; CYAN="\033[0;36m"; NC="\033[0m"
# ----------------------------
# Helpers
# ----------------------------
die() { echo -e "${RED}❌ $*${NC}"; exit 1; }
info() { echo -e "${CYAN}ℹ️ $*${NC}"; }
ok() { echo -e "${GREEN}✅ $*${NC}"; }
warn() { echo -e "${YELLOW}⚠️ $*${NC}"; }
need_cmd() {
command -v "$1" >/dev/null 2>&1 || die "Missing command: $1 (please install it first)"
}
pause() {
read -rp "Press Enter to continue..." _ </dev/tty || true
}
confirm() {
local prompt="${1:-Are you sure?} [y/N]: "
read -r -p "$prompt" ans </dev/tty || true
[[ "${ans:-}" =~ ^[Yy]$ ]]
}
progress() { local msg="$1"; echo -e "${CYAN}${msg}${NC}"; }
# ----------------------------
# Root check (cleaner than sudo everywhere)
# ----------------------------
if [[ ${EUID:-0} -ne 0 ]]; then
die "Please run as root: sudo $0"
fi
clear
echo -e "${CYAN}──────────────────────────────────────────────────────────"
echo -e " 🧠 NVIDIA Driver + CUDA Toolkit (Debian 13) v1.2.0"
echo -e "──────────────────────────────────────────────────────────${NC}"
echo -e "Log file: ${YELLOW}${LOGFILE}${NC}"
echo
# ----------------------------
# Minimal requirements
# ----------------------------
need_cmd uname
need_cmd grep
need_cmd sed
need_cmd lspci
need_cmd apt
need_cmd dpkg
need_cmd apt-cache
need_cmd tee
# ----------------------------
# System info
# ----------------------------
KERNEL="$(uname -r)"
SESSION="${XDG_SESSION_TYPE:-unknown}"
ARCH="$(dpkg --print-architecture)"
SECURE_BOOT_ENABLED=0
info "Kernel: ${KERNEL}"
info "Session: ${SESSION}"
if [[ "$SESSION" == "wayland" ]]; then
warn "Wayland session detected. Xorg config tweaks may be ignored (depends on DE)."
fi
# Secure Boot hint (best-effort)
if command -v mokutil >/dev/null 2>&1; then
if mokutil --sb-state 2>/dev/null | grep -qi "enabled"; then
SECURE_BOOT_ENABLED=1
warn "Secure Boot appears ENABLED. Unsigned NVIDIA modules may fail to load."
warn "If the driver does not load: disable Secure Boot or enroll/sign modules (MOK)."
warn "Tip: update-secureboot-policy --enroll-key (requires secureboot-policy package)."
fi
fi
echo
# ----------------------------
# NVIDIA GPU detection
# ----------------------------
GPU="$(lspci | grep -E "VGA|3D" | grep -i nvidia || true)"
[[ -n "$GPU" ]] || die "No NVIDIA GPU detected."
ok "NVIDIA GPU detected:"
echo "$GPU"
echo
# ----------------------------
# Debian release / codename
# ----------------------------
CODENAME="unknown"
if [[ -r /etc/os-release ]]; then
# shellcheck disable=SC1091
. /etc/os-release
CODENAME="${VERSION_CODENAME:-${CODENAME}}"
fi
info "Debian codename: ${CODENAME}"
# ----------------------------
# APT sources sanity (non-free + non-free-firmware)
# ----------------------------
check_nonfree_sources() {
# Debian 12/13 typically need: main contrib non-free non-free-firmware
local sources=()
[[ -f /etc/apt/sources.list ]] && sources+=("/etc/apt/sources.list")
if compgen -G "/etc/apt/sources.list.d/*.list" >/dev/null; then
sources+=(/etc/apt/sources.list.d/*.list)
fi
if (( ${#sources[@]} == 0 )); then
warn "No APT source files found in /etc/apt/sources.list or /etc/apt/sources.list.d."
return 1
fi
local all_text
all_text="$(cat "${sources[@]}" 2>/dev/null || true)"
if ! echo "$all_text" | grep -Eq '^[[:space:]]*deb[[:space:]].*(non-free)'; then
warn "APT sources do not seem to include: non-free"
warn "Enable it in your sources.list (recommended for NVIDIA)."
return 1
fi
if ! echo "$all_text" | grep -Eq '^[[:space:]]*deb[[:space:]].*(non-free-firmware)'; then
warn "APT sources do not seem to include: non-free-firmware"
warn "Debian 13 typically needs it for firmware packages."
return 1
fi
return 0
}
if ! check_nonfree_sources; then
echo
warn "Fix your APT sources first, then re-run this script."
echo "Example (Debian 13 Trixie):"
echo " deb http://deb.debian.org/debian trixie main contrib non-free non-free-firmware"
echo " deb http://security.debian.org/debian-security trixie-security main contrib non-free non-free-firmware"
echo " deb http://deb.debian.org/debian trixie-updates main contrib non-free non-free-firmware"
echo
die "APT sources missing required components."
fi
ok "APT sources look good (non-free + non-free-firmware detected)."
echo
has_suite_sources() {
local suite="$1"
local sources=()
[[ -f /etc/apt/sources.list ]] && sources+=("/etc/apt/sources.list")
if compgen -G "/etc/apt/sources.list.d/*.list" >/dev/null; then
sources+=(/etc/apt/sources.list.d/*.list)
fi
if (( ${#sources[@]} == 0 )); then
return 1
fi
local all_text
all_text="$(cat "${sources[@]}" 2>/dev/null || true)"
echo "$all_text" | grep -Eq "^[[:space:]]*deb[[:space:]].*\\b${suite}\\b"
}
# ----------------------------
# Smart APT/dpkg lock monitor (SAFE)
# ----------------------------
check_locks() {
info "Checking for running apt/dpkg processes..."
local timeout=120
local waited=0
while pgrep -x apt >/dev/null 2>&1 || pgrep -x dpkg >/dev/null 2>&1; do
if (( waited >= timeout )); then
warn "APT/dpkg still running after ${timeout}s."
warn "Killing package manager processes can BREAK your system."
if confirm "Do you want to attempt killing apt/dpkg anyway?"; then
warn "Attempting to kill apt/dpkg..."
pkill -9 apt 2>/dev/null || true
pkill -9 dpkg 2>/dev/null || true
break
else
die "Please wait until apt/dpkg finishes, then re-run."
fi
fi
echo -ne "${YELLOW}⏳ Waiting for package manager... (${waited}s)\r${NC}"
sleep 3
(( waited += 3 ))
done
echo
# Do NOT rm lock files blindly unless user confirms (can be dangerous)
if [[ -e /var/lib/dpkg/lock-frontend || -e /var/lib/dpkg/lock || -e /var/cache/apt/archives/lock ]]; then
warn "Lock files detected."
warn "Removing lock files while apt is running can corrupt dpkg state."
if confirm "Remove lock files now? (only do this if you are sure apt is NOT running)"; then
rm -f /var/lib/dpkg/lock-frontend /var/lib/dpkg/lock /var/cache/apt/archives/lock || true
ok "Lock files removed."
else
warn "Keeping lock files."
fi
fi
}
# ----------------------------
# Detect current driver
# ----------------------------
if command -v nvidia-smi >/dev/null 2>&1; then
CURRENT="$(nvidia-smi --query-gpu=driver_version --format=csv,noheader | head -n1 || true)"
ok "Current NVIDIA driver: ${CURRENT:-unknown}"
else
warn "No NVIDIA driver currently detected (nvidia-smi not found)."
fi
echo
# ----------------------------
# Xorg config (modular)
# ----------------------------
ensure_xorg_conf_d() {
mkdir -p /etc/X11/xorg.conf.d
}
write_xorg_driver_snippet() {
# $1: driver name
local driver="$1"
ensure_xorg_conf_d
cat > /etc/X11/xorg.conf.d/10-gpu-driver.conf <<EOF
Section "Device"
Identifier "GPU0"
Driver "${driver}"
EndSection
EOF
ok "Wrote /etc/X11/xorg.conf.d/10-gpu-driver.conf (Driver \"${driver}\")"
}
remove_xorg_driver_snippet() {
if [[ -f /etc/X11/xorg.conf.d/10-gpu-driver.conf ]]; then
cp -a /etc/X11/xorg.conf.d/10-gpu-driver.conf \
"/etc/X11/xorg.conf.d/10-gpu-driver.conf.backup.$(date +%F_%H-%M-%S)"
rm -f /etc/X11/xorg.conf.d/10-gpu-driver.conf
ok "Removed xorg driver snippet (backup created)."
fi
}
# ----------------------------
# Common install deps
# ----------------------------
install_common_deps() {
progress "Updating APT index"
apt update
progress "Installing build deps + headers"
local packages=(dkms build-essential firmware-misc-nonfree)
if [[ "${ARCH}" == "amd64" ]]; then
packages+=(linux-headers-amd64)
fi
if apt-cache show "linux-headers-${KERNEL}" >/dev/null 2>&1; then
packages+=("linux-headers-${KERNEL}")
else
warn "No exact headers for running kernel found: linux-headers-${KERNEL}"
warn "Consider rebooting into the newest kernel or installing linux-headers-${ARCH}."
fi
apt install -y --no-install-recommends "${packages[@]}"
}
# ----------------------------
# Nouveau handling
# ----------------------------
blacklist_nouveau() {
cat > /etc/modprobe.d/blacklist-nouveau.conf <<'EOF'
blacklist nouveau
options nouveau modeset=0
EOF
ok "Nouveau blacklisted: /etc/modprobe.d/blacklist-nouveau.conf"
}
unblacklist_nouveau() {
rm -f /etc/modprobe.d/blacklist-nouveau.conf || true
ok "Nouveau blacklist removed (if it existed)."
}
# ----------------------------
# NVIDIA remove/clean
# ----------------------------
remove_nvidia() {
local installed
installed="$(dpkg-query -W -f='${Package}\n' 'nvidia-*' 2>/dev/null || true)"
if [[ -n "${installed}" ]]; then
warn "Installed NVIDIA-related packages that may be removed:"
echo "${installed}"
warn "This includes CUDA-related packages that match nvidia-*."
else
info "No installed NVIDIA-related packages detected."
fi
progress "Purging NVIDIA packages"
apt purge -y 'nvidia-*' || true
progress "Autoremoving unused deps"
apt autoremove -y || true
progress "Cleaning DKMS remnants (best-effort)"
rm -rf /var/lib/dkms/nvidia* 2>/dev/null || true
progress "Removing old modprobe snippets (best-effort)"
rm -f /etc/modprobe.d/nvidia*.conf 2>/dev/null || true
progress "Updating initramfs"
update-initramfs -u
remove_xorg_driver_snippet
ok "NVIDIA removed."
}
# ----------------------------
# CUDA Toolkit (Debian package)
# ----------------------------
install_cuda_toolkit_debian() {
warn "Installing CUDA Toolkit from Debian repo (nvidia-cuda-toolkit)."
warn "Note: This may not be the newest CUDA version, but it is Debian-managed and stable."
warn "If you need the newest CUDA, consider NVIDIA's official repo:"
warn "https://developer.nvidia.com/cuda-downloads (use with caution on Debian)."
apt install -y nvidia-cuda-toolkit
ok "CUDA Toolkit installed (Debian package)."
echo
info "Verify: nvcc --version"
}
# ----------------------------
# Menu
# ----------------------------
echo -e "${CYAN}──────────────────────────────────────────────────────────${NC}"
echo -e "${CYAN}Choose your action:${NC}"
echo -e "${CYAN}──────────────────────────────────────────────────────────${NC}"
echo -e "1️⃣ Install NVIDIA driver (Debian stable repo) [recommended]"
echo -e "2️⃣ Install NVIDIA driver (Debian backports) [recommended if you need newer]"
echo -e "3️⃣ Enable open-source nouveau driver"
echo -e "4️⃣ Remove NVIDIA driver and clean system"
echo -e "5️⃣ ADVANCED: Install NVIDIA .run driver (NOT recommended on Debian)"
echo -e "${CYAN}──────────────────────────────────────────────────────────${NC}"
read -rp "Enter choice [1-5]: " CHOICE </dev/tty
echo -e "${CYAN}──────────────────────────────────────────────────────────${NC}"
echo
check_locks
handle_secure_boot() {
if (( SECURE_BOOT_ENABLED == 1 )); then
warn "Secure Boot is enabled. NVIDIA DKMS modules may require MOK enrollment."
if confirm "Install secureboot-policy and enroll MOK now?"; then
apt update
apt install -y secureboot-policy kmod
if command -v update-secureboot-policy >/dev/null 2>&1; then
update-secureboot-policy --enroll-key || true
else
warn "update-secureboot-policy not found. Please enroll MOK manually."
fi
warn "You may be prompted to set a MOK password and reboot to enroll."
fi
fi
}
should_write_xorg_snippet() {
if [[ "${SESSION}" == "wayland" || "${SESSION}" == "unknown" ]]; then
warn "Wayland/unknown session detected. Xorg snippets may be ignored."
confirm "Write Xorg driver snippet anyway?"
return $?
fi
return 0
}
case "${CHOICE}" in
1)
info "Installing NVIDIA driver from Debian stable repo..."
handle_secure_boot
remove_nvidia
unblacklist_nouveau
install_common_deps
progress "Installing nvidia-driver"
apt install -y nvidia-driver
blacklist_nouveau
progress "Updating initramfs"
update-initramfs -u
if should_write_xorg_snippet; then
write_xorg_driver_snippet "nvidia"
fi
ok "Driver installation finished (Debian stable repo)."
;;
2)
info "Installing NVIDIA driver from Debian backports..."
handle_secure_boot
remove_nvidia
unblacklist_nouveau
install_common_deps
# Try to detect backports suite name; Debian typically uses: trixie-backports
SUITE="${CODENAME}-backports"
warn "Using APT suite: ${SUITE}"
warn "If you don't have backports enabled, this will fail."
echo "Example line:"
echo " deb http://deb.debian.org/debian ${SUITE} main contrib non-free non-free-firmware"
echo
if ! has_suite_sources "${SUITE}"; then
warn "Backports source (${SUITE}) not detected in APT sources."
if ! confirm "Continue anyway?"; then
die "Enable ${SUITE} in APT sources and re-run."
fi
fi
progress "Installing nvidia-driver from backports"
apt install -y -t "${SUITE}" nvidia-driver || die "Backports install failed. Enable ${SUITE} in APT sources."
blacklist_nouveau
progress "Updating initramfs"
update-initramfs -u
if should_write_xorg_snippet; then
write_xorg_driver_snippet "nvidia"
fi
ok "Driver installation finished (backports)."
;;
3)
info "Enabling nouveau driver..."
remove_nvidia
progress "Installing nouveau Xorg driver"
apt install -y xserver-xorg-video-nouveau
unblacklist_nouveau
progress "Updating initramfs"
update-initramfs -u
if should_write_xorg_snippet; then
write_xorg_driver_snippet "nouveau"
fi
ok "Nouveau enabled."
;;
4)
info "Removing NVIDIA driver and cleaning system..."
remove_nvidia
# Prefer modesetting: no explicit snippet needed
remove_xorg_driver_snippet
ok "System cleaned. Using default modesetting (no forced Xorg driver)."
;;
5)
warn "ADVANCED MODE: NVIDIA .run installer on Debian is NOT recommended."
warn "This can cause APT conflicts and breaks Debian-managed updates."
if ! confirm "Continue with .run installer anyway?"; then
die "Aborted."
fi
need_cmd curl
need_cmd wget
if dpkg -l | grep -q '^ii[[:space:]]\+nvidia-driver'; then
warn "Debian nvidia-driver package appears installed."
warn "Mixing .run installer with APT can cause conflicts."
if ! confirm "Proceed and remove Debian NVIDIA packages?"; then
die "Aborted."
fi
fi
handle_secure_boot
remove_nvidia
unblacklist_nouveau
install_common_deps
mkdir -p /root/nvidia-install
cd /root/nvidia-install
warn "Fetching latest driver version (best-effort)..."
# Keep a safe fallback; API endpoints can change.
LATEST="$(curl -fsSL https://api.nvidia.com/v1/driver-latest-version/linux 2>/dev/null | grep -oP '"version":"\K[0-9.]+' || true)"
if [[ -z "${LATEST}" ]]; then
LATEST="580.95.05"
warn "Could not fetch latest version. Using fallback: ${LATEST}"
else
ok "Latest NVIDIA version detected: ${LATEST}"
fi
progress "Downloading NVIDIA-Linux-x86_64-${LATEST}.run"
wget -O NVIDIA-Linux.run "https://us.download.nvidia.com/XFree86/Linux-x86_64/${LATEST}/NVIDIA-Linux-x86_64-${LATEST}.run"
chmod +x NVIDIA-Linux.run
warn "You may need to stop your display manager before running the installer."
warn "If you are on a desktop system, consider switching to a TTY (Ctrl+Alt+F3) and stopping gdm/sddm/lightdm."
pause
progress "Running .run installer with DKMS"
./NVIDIA-Linux.run --dkms --no-cc-version-check
blacklist_nouveau
progress "Updating initramfs"
update-initramfs -u
if should_write_xorg_snippet; then
write_xorg_driver_snippet "nvidia"
fi
ok ".run driver installation finished (advanced)."
;;
*)
die "Invalid choice."
;;
esac
echo
# ----------------------------
# Optional CUDA prompt
# ----------------------------
if [[ "${CHOICE}" == "1" || "${CHOICE}" == "2" || "${CHOICE}" == "5" ]]; then
if confirm "Install CUDA Toolkit (Debian package: nvidia-cuda-toolkit) now?"; then
progress "Installing CUDA Toolkit"
apt update
install_cuda_toolkit_debian
else
info "Skipping CUDA Toolkit."
fi
fi
echo
echo -e "${CYAN}──────────────────────────────────────────────────────────${NC}"
ok "All tasks completed."
echo -e "Log file: ${YELLOW}${LOGFILE}${NC}"
echo -e "Reboot recommended: ${CYAN}reboot${NC}"
echo -e "Verify driver: ${CYAN}nvidia-smi${NC}"
echo -e "Verify CUDA (if set):${CYAN}nvcc --version${NC}"
echo -e "${CYAN}──────────────────────────────────────────────────────────${NC}"