Skip to content

Commit fdeaf3b

Browse files
committed
Work on installing CUDA toolkit on Debian
1 parent 14aa571 commit fdeaf3b

File tree

2 files changed

+129
-107
lines changed

2 files changed

+129
-107
lines changed

src/scripts/utils.sh

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,132 @@ function setupSSL() {
559559
fi
560560
}
561561

562+
563+
function CheckAndSetupCUDA () {
564+
565+
hasCUDA=false
566+
cuDNN_version=""
567+
cuda_major_version=""
568+
cuda_major_minor=""
569+
570+
if [ "$os" = "macos" ]; then
571+
cuda_version=""
572+
elif [ "${edgeDevice}" = "Jetson" ]; then
573+
hasCUDA=true
574+
cuda_version=$(getCudaVersion)
575+
cuda_major_version=${cuda_version%%.*}
576+
cuda_major_minor=$(echo "$cuda_version" | sed 's/\./_/g')
577+
cuDNN_version=$(getcuDNNVersion)
578+
579+
elif [ "${edgeDevice}" = "Raspberry Pi" ] || [ "${edgeDevice}" = "Orange Pi" ] || [ "${edgeDevice}" = "Radxa ROCK" ]; then
580+
cuda_version=""
581+
else
582+
cuda_version=$(getCudaVersion)
583+
cuda_major_version=${cuda_version%%.*}
584+
cuda_minor_version=${cuda_version#*.}
585+
cuda_major_minor=$(echo "$cuda_version" | sed 's/\./_/g')
586+
587+
if [ "$cuda_version" != "" ]; then
588+
589+
hasCUDA=true
590+
cuDNN_version=$(getcuDNNVersion)
591+
592+
installKeyring=false
593+
if [ "$cuDNN_version" = "" ] || [ ! -x "$(command -v nvcc)" ]; then
594+
writeLine "Installing NVIDIA keyring" $color_info
595+
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-keyring_1.1-1_all.deb > /dev/null 2>&1 &
596+
spin $!
597+
sudo dpkg -i cuda-keyring_1.1-1_all.deb >/dev/null &
598+
spin $!
599+
rm cuda-keyring_1.1-1_all.deb
600+
fi
601+
602+
if [ "$cuDNN_version" = "" ]; then
603+
writeLine "Installing cuDNN" $color_info
604+
# cuDNN
605+
# https://developer.nvidia.com/cudnn-downloads?target_os=Linux&target_arch=x86_64&Distribution=Ubuntu&target_version=22.04&target_type=deb_local
606+
sudo apt-get update >/dev/null 2>&1 &
607+
spin $!
608+
sudo apt-get install "cudnn-cuda-$cuda_major_version" -y >/dev/null 2>&1 &
609+
spin $!
610+
fi
611+
612+
if [ ! -x "$(command -v nvcc)" ]; then
613+
# CUDA toolkit
614+
# https://developer.nvidia.com/cuda-downloads?target_os=Linux&target_arch=x86_64&Distribution=WSL-Ubuntu&target_version=2.0&target_type=deb_network
615+
writeLine "Installing CUDA toolkit" $color_info
616+
617+
if [ "${os_name}" = "debian" ]; then
618+
619+
# Backup existing sources.list
620+
# echo "Backing up /etc/apt/sources.list to /etc/apt/sources.list.bak..."
621+
# sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
622+
623+
main_repo="deb http://deb.debian.org/debian $os_code_name main"
624+
if grep -qF "$main_repo contrib non-free" /etc/apt/sources.list; then
625+
echo "Found '$main_repo'. Replacing it with '$main_repo contrib non-free'..."
626+
sudo sed -i "s|^$main_repo|$main_repo contrib non-free|" /etc/apt/sources.list
627+
else
628+
echo "'$main_repo' not found. Adding '$main_repo contrib non-free' to sources.list..."
629+
echo "$main_repo contrib non-free" | sudo tee -a /etc/apt/sources.list > /dev/null
630+
fi
631+
632+
src_repo="deb-src http://deb.debian.org/debian $os_code_name main"
633+
if grep -qF "$src_repo contrib non-free" /etc/apt/sources.list; then
634+
echo "Found '$src_repo'. Replacing it with '$src_repo contrib non-free'..."
635+
sudo sed -i "s|^$src_repo|$updated_repo contrib non-free|" /etc/apt/sources.list
636+
else
637+
echo "'$src_repo' not found. Adding '$src_repo contrib non-free' to sources.list..."
638+
echo "$src_repo contrib non-free" | sudo tee -a /etc/apt/sources.list > /dev/null
639+
fi
640+
fi
641+
642+
sudo apt-get update > /dev/null 2>&1 &
643+
spin $!
644+
writeLine "Installing cuda kit" $color_info
645+
sudo apt-get install cuda-toolkit-${cuda_major_version}-${cuda_minor_version} -y >/dev/null 2>&1 &
646+
spin $!
647+
fi
648+
649+
# disable this
650+
if [ "${systemName}" = "WSL-but-we're-ignoring-this-for-now" ]; then # we're disabling this on purpose
651+
checkForAdminRights
652+
if [ "$isAdmin" = false ]; then
653+
writeLine "insufficient permission to install CUDA toolkit. Rerun under sudo" $color_error
654+
else
655+
# https://stackoverflow.com/a/66486390
656+
cp /usr/lib/wsl/lib/nvidia-smi /usr/bin/nvidia-smi > /dev/null 2>&1
657+
chmod a+x /usr/bin/nvidia-smi > /dev/null 2>&1
658+
659+
# Out of the box, WSL might come with CUDA 11.5, and no toolkit,
660+
# meaning we rely on nvidia-smi for version info, which is wrong.
661+
# We also should be thinking about CUDA 11.8 as a minimum, so let's
662+
# upgrade.
663+
cuda_comparison=$(versionCompare $cuda_version "11.8")
664+
if [ "$cuda_comparison" = "-1" ]; then
665+
writeLine "Upgrading WSL's CUDA install to 11.8" $color_info
666+
667+
saveState
668+
correctLineEndings "${installScriptsDirPath}/install_cuDNN.sh"
669+
source "${installScriptsDirPath}/install_cuDNN.sh" 11.8
670+
restoreState
671+
fi
672+
fi
673+
fi
674+
675+
# We may have nvidia-smi, but not nvcc (eg in WSL). Fix this.
676+
if [ -x "$(command -v nvidia-smi)" ] && [ ! -x "$(command -v nvcc)" ]; then
677+
678+
installAptPackages "nvidia-cuda-toolkit"
679+
680+
# The initial version we got would have been from nvidia-smi, which
681+
# is wrong. Redo.
682+
cuda_version=$(getCudaVersion)
683+
fi
684+
fi
685+
fi
686+
}
687+
562688
function getDotNetVersion() {
563689

564690
local requestedType=$1

src/setup.sh

Lines changed: 3 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@ writeLine "${formattedFreeSpace} of ${formattedTotalSpace} available on ${system
796796
# Install tools that we know are available via apt-get or brew
797797
if [ "$os" = "linux" ]; then
798798
checkForTool curl
799-
if [ "${os_name}" = "Debian" ]; then
799+
if [ "${os_name}" = "debian" ]; then
800800
checkForTool psmisc
801801
else
802802
checkForTool pstree
@@ -857,7 +857,7 @@ writeLine
857857

858858
if [ "$verbosity" != "quiet" ]; then
859859
writeLine
860-
writeLine "os, name, arch = ${os} ${os_name} ${architecture}" $color_mute
860+
writeLine "os, name, arch = ${os} ${os_name} (${os_code_name}) ${architecture}" $color_mute
861861
writeLine "systemName, platform = ${systemName}, ${platform}" $color_mute
862862
writeLine "edgeDevice = ${edgeDevice}" $color_mute
863863
writeLine "SSH = ${inSSH}" $color_mute
@@ -885,111 +885,7 @@ writeLine "GPU support" "White" "DarkGreen" $lineWidth
885885
writeLine
886886

887887
# Test for CUDA
888-
889-
hasCUDA=false
890-
891-
# =====================================================================
892-
# MOVE ALL OF THIS INTO utils.sh
893-
894-
cuDNN_version=""
895-
cuda_major_version=""
896-
cuda_major_minor=""
897-
898-
if [ "$os" = "macos" ]; then
899-
cuda_version=""
900-
elif [ "${edgeDevice}" = "Jetson" ]; then
901-
hasCUDA=true
902-
cuda_version=$(getCudaVersion)
903-
cuda_major_version=${cuda_version%%.*}
904-
cuda_major_minor=$(echo "$cuda_version" | sed 's/\./_/g')
905-
cuDNN_version=$(getcuDNNVersion)
906-
907-
elif [ "${edgeDevice}" = "Raspberry Pi" ] || [ "${edgeDevice}" = "Orange Pi" ] || [ "${edgeDevice}" = "Radxa ROCK" ]; then
908-
cuda_version=""
909-
else
910-
cuda_version=$(getCudaVersion)
911-
cuda_major_version=${cuda_version%%.*}
912-
cuda_minor_version=${cuda_version#*.}
913-
cuda_major_minor=$(echo "$cuda_version" | sed 's/\./_/g')
914-
915-
if [ "$cuda_version" != "" ]; then
916-
917-
hasCUDA=true
918-
cuDNN_version=$(getcuDNNVersion)
919-
920-
installKeyring=false
921-
if [ "$cuDNN_version" = "" ] || [ ! -x "$(command -v nvcc)" ]; then
922-
writeLine "Installing NVIDIA keyring" $color_info
923-
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-keyring_1.1-1_all.deb > /dev/null 2>&1 &
924-
spin $!
925-
sudo dpkg -i cuda-keyring_1.1-1_all.deb >/dev/null &
926-
spin $!
927-
rm cuda-keyring_1.1-1_all.deb
928-
fi
929-
930-
if [ "$cuDNN_version" = "" ]; then
931-
writeLine "Installing cuDNN" $color_info
932-
# cuDNN
933-
# https://developer.nvidia.com/cudnn-downloads?target_os=Linux&target_arch=x86_64&Distribution=Ubuntu&target_version=22.04&target_type=deb_local
934-
sudo apt-get update >/dev/null 2>&1 &
935-
spin $!
936-
sudo apt-get install "cudnn-cuda-$cuda_major_version" -y >/dev/null 2>&1 &
937-
spin $!
938-
fi
939-
940-
if [ ! -x "$(command -v nvcc)" ]; then
941-
# CUDA toolkit
942-
# https://developer.nvidia.com/cuda-downloads?target_os=Linux&target_arch=x86_64&Distribution=WSL-Ubuntu&target_version=2.0&target_type=deb_network
943-
writeLine "Installing CUDA toolkit" $color_info
944-
945-
if [ "${os_name}" = "Debian" ]; then
946-
deb http://deb.debian.org/debian $os_code_name main contrib non-free
947-
deb-src http://deb.debian.org/debian $os_code_name main contrib non-free
948-
fi
949-
950-
sudo apt-get update > /dev/null 2>&1 &
951-
spin $!
952-
sudo apt-get install cuda-toolkit-${cuda_major_version}-${cuda_minor_version} -y >/dev/null 2>&1 &
953-
spin $!
954-
fi
955-
956-
# disable this
957-
if [ "${systemName}" = "WSL-but-we're-ignoring-this-for-now" ]; then # we're disabling this on purpose
958-
checkForAdminRights
959-
if [ "$isAdmin" = false ]; then
960-
writeLine "insufficient permission to install CUDA toolkit. Rerun under sudo" $color_error
961-
else
962-
# https://stackoverflow.com/a/66486390
963-
cp /usr/lib/wsl/lib/nvidia-smi /usr/bin/nvidia-smi > /dev/null 2>&1
964-
chmod a+x /usr/bin/nvidia-smi > /dev/null 2>&1
965-
966-
# Out of the box, WSL might come with CUDA 11.5, and no toolkit,
967-
# meaning we rely on nvidia-smi for version info, which is wrong.
968-
# We also should be thinking about CUDA 11.8 as a minimum, so let's
969-
# upgrade.
970-
cuda_comparison=$(versionCompare $cuda_version "11.8")
971-
if [ "$cuda_comparison" = "-1" ]; then
972-
writeLine "Upgrading WSL's CUDA install to 11.8" $color_info
973-
974-
saveState
975-
correctLineEndings "${installScriptsDirPath}/install_cuDNN.sh"
976-
source "${installScriptsDirPath}/install_cuDNN.sh" 11.8
977-
restoreState
978-
fi
979-
fi
980-
fi
981-
982-
# We may have nvidia-smi, but not nvcc (eg in WSL). Fix this.
983-
if [ -x "$(command -v nvidia-smi)" ] && [ ! -x "$(command -v nvcc)" ]; then
984-
985-
installAptPackages "nvidia-cuda-toolkit"
986-
987-
# The initial version we got would have been from nvidia-smi, which
988-
# is wrong. Redo.
989-
cuda_version=$(getCudaVersion)
990-
fi
991-
fi
992-
fi
888+
CheckAndSetupCUDA
993889

994890
write "CUDA (NVIDIA) Present: "
995891
if [ "$hasCUDA" = true ]; then

0 commit comments

Comments
 (0)