Skip to content

Instantly share code, notes, and snippets.

@betsalel-williamson
Last active April 22, 2019 23:51
Show Gist options
  • Save betsalel-williamson/107882646e49af258d075cdcc9f01131 to your computer and use it in GitHub Desktop.
Save betsalel-williamson/107882646e49af258d075cdcc9f01131 to your computer and use it in GitHub Desktop.
Bash script for installing Nvidia drivers and CUDA on Ubuntu 14.04.5 LTS with Titan X
#!/bin/bash
# install file
# run when system is first installed to install the necessary components to get up and running
#
# Copyright 2018 Betsalel "Saul" Williamson
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
##
# Define a timestamp function
timestamp() {
date '+%Y-%m-%d %H:%M:%S'
}
echo "Starting install script on $(timestamp)"
# read in the state variable
if grep -Gxq '^export SETUP_SCRIPT_LAST_COMMAND=.*' ~/.bashrc
then
# code if found
# we have a state already
# do nothing or skip to the point in the script
echo "Last run command: $SETUP_SCRIPT_LAST_COMMAND"
else
export SETUP_SCRIPT_LAST_COMMAND="init"
printf '\n\nexport SETUP_SCRIPT_LAST_COMMAND="init"' >> ~/.bashrc
fi
# jump to the correct place in the script
installSublime() {
functionName='installSublime'
export SETUP_SCRIPT_LAST_COMMAND="${functionName}_start"
sed -i "s/^export SETUP_SCRIPT_LAST_COMMAND=.*/export SETUP_SCRIPT_LAST_COMMAND=\"${SETUP_SCRIPT_LAST_COMMAND}\"/g" ~/.bashrc
if hash subl 2>/dev/null; then
echo "Sublime text already installed"
else
echo "Installing Sublime text..."
wget -qO - https://download.sublimetext.com/sublimehq-pub.gpg | sudo apt-key add -
sudo apt-get install apt-transport-https
echo "deb https://download.sublimetext.com/ apt/stable/" | sudo tee /etc/apt/sources.list.d/sublime-text.list
sudo apt-get update
sudo apt-get install sublime-text
fi
export SETUP_SCRIPT_LAST_COMMAND="${functionName}_end"
sed -i "s/^export SETUP_SCRIPT_LAST_COMMAND=.*/export SETUP_SCRIPT_LAST_COMMAND=\"${SETUP_SCRIPT_LAST_COMMAND}\"/g" ~/.bashrc
}
if [ "$SETUP_SCRIPT_LAST_COMMAND" = "init" ] || [ "$SETUP_SCRIPT_LAST_COMMAND" = "installSublime_start" ] ; then
installSublime
fi
updateSystem(){
functionName='updateSystem'
export SETUP_SCRIPT_LAST_COMMAND="${functionName}_start"
sed -i "s/^export SETUP_SCRIPT_LAST_COMMAND=.*/export SETUP_SCRIPT_LAST_COMMAND=\"${SETUP_SCRIPT_LAST_COMMAND}\"/g" ~/.bashrc
echo "Updating system..."
sudo apt-get update
sudo apt-get upgrade
export SETUP_SCRIPT_LAST_COMMAND="${functionName}_end"
sed -i "s/^export SETUP_SCRIPT_LAST_COMMAND=.*/export SETUP_SCRIPT_LAST_COMMAND=\"${SETUP_SCRIPT_LAST_COMMAND}\"/g" ~/.bashrc
sudo reboot
}
if [ "$SETUP_SCRIPT_LAST_COMMAND" = "installSublime_end" ] || [ "$SETUP_SCRIPT_LAST_COMMAND" = "updateSystem_start" ] ; then
updateSystem
fi
# this is meant to work with Ubuntu 14.04.5 LTS and the titan X
installNvidia1(){
functionName='installNvidia1'
export SETUP_SCRIPT_LAST_COMMAND="${functionName}_start"
sed -i "s/^export SETUP_SCRIPT_LAST_COMMAND=.*/export SETUP_SCRIPT_LAST_COMMAND=\"${SETUP_SCRIPT_LAST_COMMAND}\"/g" ~/.bashrc
echo "Setup for NVIDIA drivers..."
echo "System will reboot at the end of this step."
cd ~
sudo apt-get purge nvidia*
# Note this might remove your cuda installation as well
sudo apt-get autoremove
# Recommended if .deb files from NVIDIA were installed
# Change 1404 to the exact system version or use tab autocompletion
# After executing this file, /etc/apt/sources.list.d should contain no files related to nvidia or cuda
sudo dpkg -P cuda-repo-ubuntu1404
## according to the website this is the correct version
wget -O NVIDIA-Linux-x86_64-390.48.run http://us.download.nvidia.com/XFree86/Linux-x86_64/390.48/NVIDIA-Linux-x86_64-390.48.run
chmod +x ~/NVIDIA-Linux-x86_64-390.48.run
sudo apt-get install build-essential gcc-multilib dkms xorg
sudo apt-get remove --purge nvidia-*
echo "You will now need to update the black list file and append the following information:"
echo "ctrl-alt-t"
echo "sudoedit /etc/modprobe.d/blacklist.conf"
echo "# black list drivers for Nvidia install
# https://askubuntu.com/questions/481414/install-nvidia-driver-instead-of-nouveau
# this one might not be required for x86 32 bit users.
blacklist amd76x_edac
blacklist vga16fb
blacklist nouveau
blacklist rivafb
blacklist nvidiafb
blacklist rivatv"
read -p "Press any key to reboot..." -n1 s
# # check that the black list exists
# if grep -Gxq '^\# black list drivers for Nvidia install.*' /etc/modprobe.d/blacklist.conf
# then
# # code if found
# # we have a state already
# # do nothing or skip to the point in the script
# echo "Blacklist for Nouveau Driver found"
# else
# # https://unix.stackexchange.com/questions/202929/using-sudoedit-in-a-script-non-interactively
# echo "Creating blacklist for Nouveau Driver"
# ED="/bin/ed"
# content="
# ##### Set editor #####
# OLD_EDITOR=$EDITOR
# export EDITOR=$ED;
# ##### Append using ed #####
# echo "a
# $CONTENT_TO_APPEND
# .
# w
# q" | sudoedit -u $USER /etc/modprobe.d/blacklist.conf
# ##### Clean up #####
# export EDITOR=$OLD_EDITOR
# fi
export SETUP_SCRIPT_LAST_COMMAND="${functionName}_end"
sed -i "s/^export SETUP_SCRIPT_LAST_COMMAND=.*/export SETUP_SCRIPT_LAST_COMMAND=\"${SETUP_SCRIPT_LAST_COMMAND}\"/g" ~/.bashrc
sudo reboot
}
if [ "$SETUP_SCRIPT_LAST_COMMAND" = "updateSystem_end" ] || [ "$SETUP_SCRIPT_LAST_COMMAND" = "installNvidia1_start" ] ; then
installNvidia1
fi
installNvidia2() {
functionName='installNvidia2'
export SETUP_SCRIPT_LAST_COMMAND="${functionName}_start"
sed -i "s/^export SETUP_SCRIPT_LAST_COMMAND=.*/export SETUP_SCRIPT_LAST_COMMAND=\"${SETUP_SCRIPT_LAST_COMMAND}\"/g" ~/.bashrc
echo "Setup for NVIDIA drivers..."
echo "After this step is run, use ctrl-alt-F1 to open terminal."
echo "To enter GUI after run:"
echo "sudo service lightdm start"
echo "Then use ctrl-alt-F7"
sudo service lightdm stop
export SETUP_SCRIPT_LAST_COMMAND="${functionName}_end"
sed -i "s/^export SETUP_SCRIPT_LAST_COMMAND=.*/export SETUP_SCRIPT_LAST_COMMAND=\"${SETUP_SCRIPT_LAST_COMMAND}\"/g" ~/.bashrc
}
if [ "$SETUP_SCRIPT_LAST_COMMAND" = "installNvidia1_end" ] || [ "$SETUP_SCRIPT_LAST_COMMAND" = "installNvidia2_start" ] ; then
installNvidia2
fi
installNvidia3() {
functionName='installNvidia3'
export SETUP_SCRIPT_LAST_COMMAND="${functionName}_start"
sed -i "s/^export SETUP_SCRIPT_LAST_COMMAND=.*/export SETUP_SCRIPT_LAST_COMMAND=\"${SETUP_SCRIPT_LAST_COMMAND}\"/g" ~/.bashrc
echo "Installing NVIDIA drivers..."
echo "The precheck will fail, it is ok to continue through it."
sudo update-initramfs -u
sudo ~/NVIDIA-Linux-x86_64-390.48.run --dkms
if hash nvidia-smi 2>/dev/null; then
echo "Success!"
nvidia-smi
else
echo "There was a problem with your install..."
exit 1
fi
export SETUP_SCRIPT_LAST_COMMAND="${functionName}_end"
sed -i "s/^export SETUP_SCRIPT_LAST_COMMAND=.*/export SETUP_SCRIPT_LAST_COMMAND=\"${SETUP_SCRIPT_LAST_COMMAND}\"/g" ~/.bashrc
}
if [ "$SETUP_SCRIPT_LAST_COMMAND" = "installNvidia2_end" ] || [ "$SETUP_SCRIPT_LAST_COMMAND" = "installNvidia3_start" ] ; then
installNvidia3
fi
downloadCuda7(){
functionName='downloadCuda7'
export SETUP_SCRIPT_LAST_COMMAND="${functionName}_start"
sed -i "s/^export SETUP_SCRIPT_LAST_COMMAND=.*/export SETUP_SCRIPT_LAST_COMMAND=\"${SETUP_SCRIPT_LAST_COMMAND}\"/g" ~/.bashrc
cd ~
filename="cuda_7.0.28_linux.run"
wget -O $filename http://developer.download.nvidia.com/compute/cuda/7_0/Prod/local_installers/cuda_7.0.28_linux.run
chmod +x ~/$filename
./$filename --extract=$HOME
export SETUP_SCRIPT_LAST_COMMAND="${functionName}_end"
sed -i "s/^export SETUP_SCRIPT_LAST_COMMAND=.*/export SETUP_SCRIPT_LAST_COMMAND=\"${SETUP_SCRIPT_LAST_COMMAND}\"/g" ~/.bashrc
}
if [ "$SETUP_SCRIPT_LAST_COMMAND" = "installNvidia3_end" ] || [ "$SETUP_SCRIPT_LAST_COMMAND" = "downloadCuda7_start" ] ; then
downloadCuda7
fi
downloadCuda7p5(){
functionName='downloadCuda7p5'
export SETUP_SCRIPT_LAST_COMMAND="${functionName}_start"
sed -i "s/^export SETUP_SCRIPT_LAST_COMMAND=.*/export SETUP_SCRIPT_LAST_COMMAND=\"${SETUP_SCRIPT_LAST_COMMAND}\"/g" ~/.bashrc
cd ~
filename="cuda_7.5.18_linux.run"
wget -O $filename http://developer.download.nvidia.com/compute/cuda/7.5/Prod/local_installers/cuda_7.5.18_linux.run
chmod +x ~/$filename
./$filename --extract=$HOME
export SETUP_SCRIPT_LAST_COMMAND="${functionName}_end"
sed -i "s/^export SETUP_SCRIPT_LAST_COMMAND=.*/export SETUP_SCRIPT_LAST_COMMAND=\"${SETUP_SCRIPT_LAST_COMMAND}\"/g" ~/.bashrc
}
if [ "$SETUP_SCRIPT_LAST_COMMAND" = "downloadCuda7_end" ] || [ "$SETUP_SCRIPT_LAST_COMMAND" = "downloadCuda7p5_start" ] ; then
downloadCuda7p5
fi
downloadCuda8IncludingPatch2(){
functionName='downloadCuda8'
export SETUP_SCRIPT_LAST_COMMAND="${functionName}_start"
sed -i "s/^export SETUP_SCRIPT_LAST_COMMAND=.*/export SETUP_SCRIPT_LAST_COMMAND=\"${SETUP_SCRIPT_LAST_COMMAND}\"/g" ~/.bashrc
cd ~
filename="cuda_8.0.61_375.26_linux-run"
wget -O $filename https://developer.nvidia.com/compute/cuda/8.0/Prod2/local_installers/cuda_8.0.61_375.26_linux-run
chmod +x ~/$filename
./$filename --extract=$HOME
filename2="cuda_8.0.61.2_linux-run"
wget -O $filename2 https://developer.nvidia.com/compute/cuda/8.0/Prod2/patches/2/cuda_8.0.61.2_linux-run
chmod +x ~/$filename2
export SETUP_SCRIPT_LAST_COMMAND="${functionName}_end"
sed -i "s/^export SETUP_SCRIPT_LAST_COMMAND=.*/export SETUP_SCRIPT_LAST_COMMAND=\"${SETUP_SCRIPT_LAST_COMMAND}\"/g" ~/.bashrc
}
if [ "$SETUP_SCRIPT_LAST_COMMAND" = "downloadCuda7p5_end" ] || [ "$SETUP_SCRIPT_LAST_COMMAND" = "downloadCuda8IncludingPatch2_start" ] ; then
downloadCuda8IncludingPatch2
fi
echo "Setup eneded with: $SETUP_SCRIPT_LAST_COMMAND"
echo "Ended install script on $(timestamp)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment