Debian Codegraph @

docker/setup-ubuntu.sh

raw
1 #!/bin/bash
2
3 echo "πŸš€ Starting intelligent NVIDIA Docker setup..."
4
5 echo "πŸ“¦ Updating NVIDIA driver to latest..."
6 sudo apt update
7 sudo ubuntu-drivers autoinstall
8 echo "πŸ”„ Rebooting required after driver update. Run this script again after reboot."
9 # Check if reboot is needed
10 if [ -f /var/run/reboot-required ]; then
11 echo "⚠️ System reboot required. Please reboot and run this script again."
12 echo "After reboot, run: sudo reboot && ./setup-ubuntu.sh"
13 exit 0
14 fi
15
16 echo "πŸ” Detecting NVIDIA driver version..."
17 DRIVER_VERSION=$(nvidia-smi --query-gpu=driver_version --format=csv,noheader,nounits | head -1)
18 echo "Found NVIDIA driver: $DRIVER_VERSION"
19
20 echo "πŸ” Determine compatible CUDA version based on driver..."
21 get_compatible_cuda_version() {
22 local driver_ver=$1
23
24 # Extract major version (e.g., 560 from 560.35.03)
25 local major_ver=$(echo $driver_ver | cut -d'.' -f1)
26
27 # NVIDIA Driver-CUDA Compatibility Matrix
28 if [ $major_ver -ge 565 ]; then
29 echo "12.9" # Driver 565+ supports CUDA 12.9
30 elif [ $major_ver -ge 560 ]; then
31 echo "12.6" # Driver 560+ supports CUDA 12.6 (your current case)
32 elif [ $major_ver -ge 555 ]; then
33 echo "12.5" # Driver 555+ supports CUDA 12.5
34 elif [ $major_ver -ge 550 ]; then
35 echo "12.4" # Driver 550+ supports CUDA 12.4
36 elif [ $major_ver -ge 535 ]; then
37 echo "12.2" # Driver 535+ supports CUDA 12.2
38 elif [ $major_ver -ge 525 ]; then
39 echo "12.0" # Driver 525+ supports CUDA 12.0
40 else
41 echo "11.8" # Fallback to CUDA 11.8
42 fi
43 }
44 COMPATIBLE_CUDA=$(get_compatible_cuda_version $DRIVER_VERSION)
45 echo "βœ… Compatible CUDA version: $COMPATIBLE_CUDA"
46
47 echo "πŸ“¦ Installing latest nvidia-container-toolkit..."
48 curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | sudo gpg --dearmor --yes -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg \
49 && curl -s -L https://nvidia.github.io/libnvidia-container/stable/deb/nvidia-container-toolkit.list | \
50 sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' | \
51 sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list
52 sudo apt-get update
53 sudo apt-get install -y nvidia-container-toolkit
54
55 echo "πŸ“¦ Configure Docker"
56 sudo nvidia-ctk runtime configure --runtime=docker
57 sudo systemctl restart docker
58
59 echo "πŸ” Finding latest patch version for CUDA $COMPATIBLE_CUDA..."
60 AVAILABLE_VERSIONS=$(curl -s "https://registry.hub.docker.com/v2/repositories/nvidia/cuda/tags/?page_size=100" | jq -r '.results[].name' | grep -E "^${COMPATIBLE_CUDA}\.[0-9]+-base-ubuntu24\.04$" | head -1)
61 if [ -n "$AVAILABLE_VERSIONS" ]; then
62 # Extract full version (e.g., "12.9.1" from "12.9.1-base-ubuntu24.04")
63 FULL_CUDA_VERSION=$(echo "$AVAILABLE_VERSIONS" | cut -d'-' -f1)
64 echo "βœ… Found CUDA version: $FULL_CUDA_VERSION"
65 else
66 echo "❌ No CUDA $COMPATIBLE_CUDA images found for Ubuntu 24.04, trying Ubuntu 22.04..."
67 AVAILABLE_VERSIONS=$(curl -s "https://registry.hub.docker.com/v2/repositories/nvidia/cuda/tags/?page_size=100" | jq -r '.results[].name' | grep -E "^${COMPATIBLE_CUDA}\.[0-9]+-base-ubuntu22\.04$" | head -1)
68
69 if [ -n "$AVAILABLE_VERSIONS" ]; then
70 FULL_CUDA_VERSION=$(echo "$AVAILABLE_VERSIONS" | cut -d'-' -f1)
71 UBUNTU_VERSION="22.04"
72 echo "βœ… Found CUDA version: $FULL_CUDA_VERSION for Ubuntu 22.04"
73 else
74 echo "❌ No compatible CUDA images found"
75 exit 1
76 fi
77 fi
78
79 echo "πŸ§ͺ Testing with automatically detected compatible CUDA version: $COMPATIBLE_CUDA..."
80 if docker run --rm --runtime=nvidia nvidia/cuda:${FULL_CUDA_VERSION}-base-ubuntu${UBUNTU_VERSION:-24.04} nvidia-smi; then
81 echo "βœ… GPU support working with CUDA $FULL_CUDA_VERSION!"
82 # Update Dockerfile with compatible version
83 if [ -f "Dockerfile" ]; then
84 sed -i "s/ARG NVIDIA_CUDA_VERSION=.*/ARG NVIDIA_CUDA_VERSION=${FULL_CUDA_VERSION}/" Dockerfile
85 if [ "${UBUNTU_VERSION:-24.04}" = "22.04" ]; then
86 sed -i "s/ARG UBUNTU_VERSION=.*/ARG UBUNTU_VERSION=22.04/" Dockerfile
87 fi
88 echo "βœ… Updated Dockerfile to use CUDA $FULL_CUDA_VERSION"
89 fi
90 else
91 echo "❌ GPU test failed with CUDA $FULL_CUDA_VERSION"
92 fi