Skip to content

Instantly share code, notes, and snippets.

@darki73
Created April 21, 2023 12:24
Show Gist options
  • Save darki73/2a774ecba30fa5b7dc5858ec22a6e54f to your computer and use it in GitHub Desktop.
Save darki73/2a774ecba30fa5b7dc5858ec22a6e54f to your computer and use it in GitHub Desktop.
Linux automatic installer for NVIDIA driver
#!/usr/bin/env bash
# Get installed kernel version
KERNEL_VERSION=$(uname -r)
# Build the package name for the kernel headers
HEADERS_PACKAGE_NAME="linux-headers-${KERNEL_VERSION}"
# Check whether the kernel headers are already installed
if [ ! -f "/usr/src/${HEADERS_PACKAGE_NAME}/.config" ]; then
echo "Installing ${HEADERS_PACKAGE_NAME}..."
sudo apt-get install -y "${HEADERS_PACKAGE_NAME}"
fi
# List of required packages to build the driver
REQUIRED_PACKAGES=(
"build-essential"
"libglvnd-dev"
"pkg-config"
)
# Check if the required packages are installed
for package in "${REQUIRED_PACKAGES[@]}"; do
if ! dpkg -s "$package" >/dev/null 2>&1; then
echo "Installing $package..."
sudo apt-get install -y "$package"
fi
done
# Raw list of video devices
VIDEO_DEVICES_RAW=$(lspci -k | grep "VGA")
# List of video devices
VIDEO_DEVICES=()
while read -r line; do
if echo "$line" | grep -q "NVIDIA Corporation"; then
if echo "$line" | grep -q '\[.*\]'; then
DEVICE_NAME=("$(echo "$line" | grep -o '\[.*\]' | tr -d '[]')")
VIDEO_DEVICES+=$DEVICE_NAME
echo "Found NVIDIA device: $DEVICE_NAME"
else
UNKNOWN_DEVICE_NAME=$(echo "$line" | grep -o 'NVIDIA Corporation.*$' | sed 's/NVIDIA Corporation //')
echo "Unkown NVIDIA device: $UNKNOWN_DEVICE_NAME"
fi
fi
done <<< "$VIDEO_DEVICES_RAW"
# Latest driver version
LATEST_DRIVER_VERSION=$(curl -s https://download.nvidia.com/XFree86/Linux-x86_64/latest.txt | awk 'NR==1{print $1}' | awk -F/ '{print $1}')
echo "Latest NVIDIA driver version: $LATEST_DRIVER_VERSION"
# check if nvidia driver is installed
if [ -f /usr/bin/nvidia-smi ]; then
# get nvidia driver version
NVIDIA_DRIVER_VERSION=$(nvidia-smi | grep "Driver Version" | awk '{print $3}')
echo "NVIDIA driver version: $NVIDIA_DRIVER_VERSION"
# check if latest driver is already installed
if [ "$NVIDIA_DRIVER_VERSION" == "$LATEST_DRIVER_VERSION" ]; then
echo "Latest NVIDIA driver is already installed."
exit 0
fi
fi
# Latest driver readme URL
LATEST_DRIVER_README_URL="https://download.nvidia.com/XFree86/Linux-x86_64/${LATEST_DRIVER_VERSION}/README/supportedchips.html"
# Latest driver curl User-Agent
LATEST_DRIVER_README_USER_AGENT="Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:15.0) Gecko/20100101 Firefox/15.0.1"
# Temporary file to hold readme content
LATEST_DRIVER_TEMP_FILE=$(mktemp)
# Load readme content
curl -s -H "User-Agent: $LATEST_DRIVER_README_USER_AGENT" "$LATEST_DRIVER_README_URL" -o "$LATEST_DRIVER_TEMP_FILE"
# List of supported GPUs
SUPPORTED_GPUS=()
# List of unsupported GPUs
UNSUPPORTED_GPUS=()
# Check if the GPU is supported by the driver
is_gpu_supported_by_driver() {
local gpu_name="$1"
in_gpu_list=$(grep -oP '(?<=<td>).*?(?=</td>)' <(sed -n '/<h3>Current NVIDIA GPUs<\/h3>/,/<\/tbody>/p' "$LATEST_DRIVER_TEMP_FILE"))
if echo "$in_gpu_list" | grep -q "$gpu_name"; then
echo "The NVIDIA $gpu_name is supported."
SUPPORTED_GPUS+=("$gpu_name")
else
echo "The NVIDIA $gpu_name is not supported."
UNSUPPORTED_GPUS+=("$gpu_name")
fi
}
if [ ${#VIDEO_DEVICES[@]} -eq 0 ]; then
echo "No NVIDIA video devices found."
else
for gpu_name in "${VIDEO_DEVICES[@]}"; do
is_gpu_supported_by_driver "$gpu_name"
done
fi
if [ ${#UNSUPPORTED_GPUS[@]} -ne 0 ]; then
echo "The following NVIDIA video devices are not supported:"
for gpu_name in "${UNSUPPORTED_GPUS[@]}"; do
echo " - $gpu_name"
done
echo "Do you want to continue? [y/N]"
read -r answer
if [ "$answer" != "${answer#[Yy]}" ]; then
echo "Continuing..."
else
# Remove temporary readme content file
rm "$LATEST_DRIVER_TEMP_FILE"
echo "Exiting..."
exit 1
fi
fi
# Latest driver download URL
LATEST_DRIVER_DOWNLOAD_URL="https://download.nvidia.com/XFree86/Linux-x86_64/${LATEST_DRIVER_VERSION}/NVIDIA-Linux-x86_64-${LATEST_DRIVER_VERSION}.run"
# Download latest driver
echo "Downloading latest NVIDIA driver..."
curl -s -L "$LATEST_DRIVER_DOWNLOAD_URL" -o "NVIDIA-Linux-x86_64-${LATEST_DRIVER_VERSION}.run"
# Make the driver executable
chmod +x "NVIDIA-Linux-x86_64-${LATEST_DRIVER_VERSION}.run"
echo "Do you want to install the driver? [y/N]"
read -r answer
if [ "$answer" != "${answer#[Yy]}" ]; then
# Install the driver
echo "Installing latest NVIDIA driver..."
sudo "./NVIDIA-Linux-x86_64-${LATEST_DRIVER_VERSION}.run" -s
else
echo "Exiting..."
fi
# Remove downloaded driver
rm "NVIDIA-Linux-x86_64-${LATEST_DRIVER_VERSION}.run"
# Remove temporary readme content file
rm "$LATEST_DRIVER_TEMP_FILE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment