Bash script for installing CUDA, cudNN, and TensorRT in Ubuntu 20.04

Crossposted: linux - Bash script for installing CUDA, cudNN, and TensorRT in Ubuntu 20.04 - Stack Overflow

I have written the following bash script to perform a clean install of CUDA 11.0 and cudNN 8.9.6, and TensorRT.

#!/bin/bash

# Remove and clean all previous CUDA installations
echo "Removing CUDA files..."
sudo rm -rf /usr/local/cuda-*

echo "Purging nvidia-cuda-toolkit..."
sudo apt-get purge -y nvidia-cuda-toolkit
sudo apt-get autoremove -y

echo "Removing /opt/cuda and ~/NVIDIA_GPU_Computing_SDK folders if they exist..."
sudo rm -rf /opt/cuda
rm -rf ~/NVIDIA_GPU_Computing_SDK

# Remove CUDA paths from .bash_profile or .profile if they exist
files=( "~/.bash_profile" "~/.profile" )
for file in "${files[@]}"; do
    if [ -f "$file" ]; then
        echo "Removing CUDA paths from $file..."
        sed -i '/\/opt\/cuda\/bin/d' "$file"
        sed -i '/\/opt\/cuda\/lib:/d' "$file"
        sed -i '/\/opt\/cuda\/lib64/d' "$file"
    else
        echo "$file does not exist. Skipping..."
    fi
done

# Create the Download directory if it does not exist and navigate into it
mkdir -p Download
cd Download

# List of file URLs
files=(
    "cuda_11.0.2_450.51.05_linux.run"
    "7fa2af80.pub"
    "libnccl2_2.8.3-1+cuda11.0_amd64.deb"
    "libnccl-dev_2.8.3-1+cuda11.0_amd64.deb"
    "nvidia-machine-learning-repo-ubuntu2004_1.0.0-1_amd64.deb"
    "cudnn-local-repo-ubuntu2004-8.9.6.50_1.0-1_amd64.deb"
    "nv-tensorrt-local-repo-ubuntu2004-8.6.1-cuda-11.8_1.0-1_amd64.deb"
)

# Download files
for file in "${files[@]}"; do
    if [ ! -f "$file" ]; then
        wget "http://developer.download.nvidia.com/compute/cuda/11.0.2/local_installers/$file"
        if [ $? -ne 0 ]; then
            echo "Failed to download $file"
            exit 1
        fi
    fi
done

# Install downloaded packages
sudo sh cuda_11.0.2_450.51.05_linux.run
sudo apt-get install cuda-11-0
sudo dpkg -i libnccl2_2.8.3-1+cuda11.0_amd64.deb
sudo dpkg -i libnccl-dev_2.8.3-1+cuda11.0_amd64.deb
sudo dpkg -i nvidia-machine-learning-repo-ubuntu2004_1.0.0-1_amd64.deb
sudo apt-get update
sudo dpkg -i cudnn-local-repo-ubuntu2004-8.9.6.50_1.0-1_amd64.deb
sudo dpkg -i nv-tensorrt-local-repo-ubuntu2004-8.6.1-cuda-11.8_1.0-1_amd64.deb

# Fix any potential broken dependencies
sudo apt-get install -f

# Find CUDA version and set CUDA_HOME to correct path
CUDA_VERSION=$(ls /usr/local | grep cuda- | sed 's/cuda-//')
export CUDA_HOME=/usr/local/cuda-$CUDA_VERSION

# Add CUDA_HOME to .bashrc if it doesn't already exist
if ! grep -q "export CUDA_HOME=${CUDA_HOME}" ~/.bashrc ; then
    echo "Adding CUDA_HOME to ~/.bashrc"
    echo "export CUDA_HOME=${CUDA_HOME}" >> ~/.bashrc
else
    echo "CUDA_HOME already exists in ~/.bashrc. Skipping..."
fi

I am confused about the following two lines:

sudo sh cuda_11.0.2_450.51.05_linux.run
sudo apt-get install cuda-11-0

I have a feeling that these two lines are redundant.

Should I remove sudo sh cuda_11.0.2_450.51.05_linux.run?

They are approximately redundant.

Among the methods to install CUDA, one is called a runfile installer method. Another is called a package manager method.

This invokes a runfile installer:

This invokes a package manager:

You can find more info about this in the CUDA linux install guide.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.