Skip to content

Instantly share code, notes, and snippets.

@staticfloat
Created March 8, 2019 23:47
Show Gist options
  • Save staticfloat/67df66d7da35dbd956f2720be59297e4 to your computer and use it in GitHub Desktop.
Save staticfloat/67df66d7da35dbd956f2720be59297e4 to your computer and use it in GitHub Desktop.
Fixer script for NVidia driver installs on dkms-compatible systems
#!/usr/bin/env bash
function red_echo()
{
tput setaf 1
echo "$*"
tput sgr0
}
if [[ "$*" == *--help* ]]; then
echo "Usage:"
echo " sudo $0 [--force] [--help]"
echo
echo "Options:"
echo " --force: Force-uninstall previous nvidia driver versions."
echo " --help: Print this help script"
fi
# Check for `cc`
if [[ -z "$(which cc 2>/dev/null)" ]]; then
red_echo "WARN: No 'cc'; Ensure that 'gcc' is installed and symlinked to 'cc' or nvidia's build system will likely fail!"
fi
# Make sure we're sudo before continuing
if [[ "$EUID" != "0" ]]; then
red_echo "I AM (G)ROOT!! running 'sudo $0 $*'..."
sudo $0 $*
exit 0
fi
echo
red_echo '************************************'
red_echo '* Kernel upgrade got you down? *'
red_echo '* Never fear for your GPU again! *'
red_echo '* Just run fix_nvidia! *'
red_echo '************************************'
echo
# Try to guess nvidia versio, e.g. '/usr/src/nvidia-390-390.59' -> '390.59'
NVIDIA_VERSION=$(for f in /usr/src/nvidia-*; do (echo $f | cut -d- -f2-); done | head -1)
# If they've asked for an uninstall, then do so.
if [[ "$*" == *--force* ]]; then
red_echo "Uninstalling nvidia ${NVIDIA_VERSION} kernel module as requested by '--force'..."
dkms remove nvidia/${NVIDIA_VERSION} --all
fi
# Install the new GPU code
red_echo "Installing nvidia ${NVIDIA_VERSION} kernel module..."
dkms install --verbose -m nvidia/${NVIDIA_VERSION}
# If there was no module from before, just modprobe it now
if [[ -z "$(lsmod | grep -E '^nvidia')" ]]; then
red_echo "Looks like the nvidia kernel module wasn't loaded, auto-modprobulating...."
modprobe nvidia
else
red_echo "Looks like the nvidia kernel module was loaded, you should restart now."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment