Skip to content

Instantly share code, notes, and snippets.

@lmlsna
Last active August 8, 2023 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lmlsna/f84e4308296c1754e02e78fa8a2b1b58 to your computer and use it in GitHub Desktop.
Save lmlsna/f84e4308296c1754e02e78fa8a2b1b58 to your computer and use it in GitHub Desktop.
Automatically configure nvidia's deb repo for CUDA, and update with apt.
#!/bin/bash
# An nvidia and cuda installer for Ubuntu
# Set env DEBUG_MODE=1 (up to 4) for more verbosity
if [[ "$DEBUG_MODE" == "" ]] || [ $DEBUG_MODE ! -gt 0 ]; then export DEBUG_MODE=0; fi
# Set env ASSUME_YES=-y for less interactivity
if [[ "$ASSUME_YES" != "" ]]; then export ASSUME_YES="-y"; else export ASSUME_YES=''; fi
## Pretty Print
# Usage: pp "error|warn|ok|debug" "plain text" "colored text"
function pp {
# Define colors
RESET='\e[0m'
RED='\e[38;5;1m'
YELLOW='\e[38;5;11m'
GREEN='\e[38;5;2m'
CYAN='\e[38;5;12m'
case "${1,,}" in
error|err|er|e|r|red)
echo -e "[${RED}X${RESET}]: $2${RED}$3${RESET}"
;;
warning|warn|w|y)
echo -e "[${YELLOW}!${RESET}]: $2${YELLOW}$3${RESET}"
;;
okay|ok|success|green|g)
echo -e "[${GREEN}*${RESET}]: $2${GREEN}$3${RESET}"
;;
debug|comment|neutral|d|cyan|c)
echo -e "[${CYAN}+${RESET}]: $2${CYAN}$3${RESET}"
;;
esac
}
# Print debug info (1)
[ $DEBUG_MODE -gt 0 ] && pp debug 'env DEBUG_MODE=' "$DEBUG_MODE"
[ $DEBUG_MODE -gt 0 ] && pp debug 'env ASSUME_YES=' "$ASSUME_YES"
## Get distro variables
distro="$(lsb_release -si|tr '[[:upper:]]' '[[:lower:]]')"
kernel="$(uname -r)"
release="$(lsb_release -sr| sed 's/\.//g')"
latest="$(sudo apt-cache search nvidia-kernel-open|sort -n | tail -n 1 |cut -d '-' -f 4)"
arch="x86_64"
url="https://developer.download.nvidia.com/compute/cuda/repos/${distro}${release}/${arch}"
# Print debug info (2)
[ $DEBUG_MODE -gt 0 ] && pp debug 'distro: ' "$distro"
[ $DEBUG_MODE -gt 0 ] && pp debug 'kernel: ' "$kernel"
[ $DEBUG_MODE -gt 0 ] && pp debug 'release: ' "$release"
[ $DEBUG_MODE -gt 0 ] && pp debug 'latest: ' "$latest"
[ $DEBUG_MODE -gt 0 ] && pp debug 'arch: ' "$arch"
[ $DEBUG_MODE -gt 0 ] && pp debug 'url: ' "$url"
[ $DEBUG_MODE -gt 0 ] && pp debug 'has headers:' "$(sudo apt search linux-headers-$kernel)"
## Run as sudoer but not root
if [ $EUID -eq 0 ]; then
pp error "Run this script as a sudo user, but not as root."
exit 1
fi
## Required packages
reqs='apt-transport-https gcc python3-pip wget'
pp ok "Installing required packages..."
sudo apt-get update
sudo apt-get install $ASSUME_YES $reqs
## Move into temp dir
tmpd="$(mktemp -d)";
cd "$tmpd"
## Check for card presence by counting pci ids with "NVIDIA" in them
if [[ $(lspci | grep -i nvidia | cut -d' ' -f2- | wc -l) -lt 1 ]]; then
pp error "Nvidia card not found."
exit 2
fi
## Install kernel headers
[ $DEBUG_MODE -gt 0 ] && pp debug "Installing " "linux-headers-$kernel"
sudo apt-get install $ASSUME_YES linux-headers-$kernel
## remove old cuda keys add new ones
sudo mv "/etc/apt/trusted.gpg.d/cuda-ubuntu2004-keyring.gpg" "/etc/apt/trusted.gpg.d/cuda-ubuntu2004-keyring.gpg.save" || echo 'No old keys.'
## Install new keyring and repo
wget "$url/cuda-archive-keyring.gpg" -O cuda-archive-keyring.gpg
sudo cp -v 'cuda-archive-keyring.gpg' '/etc/apt/trusted.gpg.d/'
echo "deb $url/ /" | sudo tee "/etc/apt/sources.list.d/cuda.list"
wget "$url/cuda-${distro}${release}.pin" -O cuda-repository-pin-600
sudo cp cuda-repository-pin-600 /etc/apt/preferences.d/
sudo apt-get update
sudo apt-get install $ASSUME_YES cuda-keyring
sudo apt-get dist-upgrade $ASSUME_YES
## Install cuda and nvidia-gds
sudo apt-get update
sudo apt-get install $ASSUME_YES --reinstall cuda
sudo apt-get install $ASSUME_YES --reinstall nvidia-gds
## Find latest nvidia-kernel-package
if [[ "$latest" == "" ]]; then
pp error "Couldn't find nvidia-kernel version"
exit 3
else
pp ok "Found version $latest"
sudo apt-get update
sudo apt-get install $ASSUME_YES nvidia-kernel-open-$latest
sudo apt-get install $ASSUME_YES nvidia-driver-${latest}-open
sudo apt-get install $ASSUME_YES cuda
sudo pip3 install --upgrade setuptools pip wheel
sudo pip3 install --upgrade nvidia-pyindex
fi
#function pipnvlib {
# pre="nvidia";
# post="cu12";
# sudo pip3 install --upgrade "${pre}-${1}-${post}";
#}
#
#
#whiptail --title "Additional CUDA Libraries" --checklist \
#"Optionally install submodules for CUDA" $(( $LINES/4*3 )) $(( ${COLUMNS} /2 )) 20 \
#"opencl" "opencl" ON \
#"cublas" "cublas" ON \
#"cufft" "cufft" ON \
#"curand" "curand" ON \
#"cusolver" "cusolver" ON \
#"cusparse" "cuparse" ON \
#"cuda-cupti" "cuda-cupti" ON \
#"cuda-sanitizer_api" "cuda-sanitizer-api" ON \
#"cuda-nvrtc" "cuda-nvrtc" ON \
#"cuda-nvcc" "cuda-nvcc" ON \
#"nvvm-samples" "nvvm-samples" ON \
#"nvml-dev" "nvml-dev" ON \
#"nvjpeg" "nvjpeg" ON \
#"nvjitlink" "nvjitlink" ON \
#"nvtx" "nvtx" ON \
#"npp" "npp" ON
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment