As a developer working with NVIDIA GPUs, you know how crucial it is to have the right CUDA toolkit installed on your system. I have found myself having to constantly install the CUDA toolkit on new GPU instances. This post is about a simple way to install CUDA 12.8 on Ubuntu 22.04 using a single bash script.
Note that this assumes your instance has NVIDIA drivers that support CUDA 12.8
Prerequisites
- Ubuntu 22.04 (64-bit)
- NVIDIA GPU (supporting CUDA 12.8)
The Script
Create a new file cuda_init.sh
#! /bin/bash
# Initial download CUDA
start_time=$(date +%s.%N)
sudo apt install -y wget git
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-keyring_1.1-1_all.deb
sudo dpkg -i cuda-keyring_1.1-1_all.deb
sudo apt update
end_time=$(date +%s.%N)
echo "APT Pre-Install Time: $(echo "$end_time - $start_time" | bc) seconds"
# Installing CUDA 12.8
sudo apt update && sudo apt install -y libcudnn9-cuda-12 cuda-toolkit-12-8
# Adding paths to ~/.bashrc
echo "export CUDA_VERSION=12.8" >> /home/Ubuntu/.bashrc
echo "export CUDA_HOME=\"/usr/local/cuda-\${CUDA_VERSION}\"" >> /home/Ubuntu/.bashrc
echo "export CUDA_PATH=\"\${CUDA_HOME}\"" >> /home/Ubuntu/.bashrc
echo "export PATH=\"\${CUDA_PATH}/bin:\${PATH}\"" >> /home/Ubuntu/.bashrc
echo "export LIBRARY_PATH=\"\${CUDA_PATH}/lib64:\${LIBRARY_PATH}\"" >> /home/Ubuntu/.bashrc
echo "export LD_LIBRARY_PATH=\"\${CUDA_PATH}/lib64:\${LD_LIBRARY_PATH}\"" >> /home/Ubuntu/.bashrc
echo "export LD_LIBRARY_PATH=\"\${CUDA_PATH}/extras/CUPTI/lib64:\${LD_LIBRARY_PATH}\"" >> /home/Ubuntu/.bashrc
echo "export NVCC=\"\${CUDA_PATH}/bin/nvcc\"" >> /home/Ubuntu/.bashrc
echo "export CFLAGS=\"-I\${CUDA_PATH}/include \${CFLAGS}\"" >> /home/Ubuntu/.bashrc
echo "CUDA setup complete."
Running the Script
Make the script executable and run it:
chmod +x cuda_init.sh
./cuda_init.sh
Verifying the Installation
After the script finishes running, verify that CUDA 12.8 is installed correctly by running:
nvcc --version
Wrap up
Hopefully, this simple script helps someone who is struggling to get CUDA to install. I have also used this with CUDA 12.4 in the past so it is a versatile script that should allow you to install any version with a few modifications.
I developed this script using Massed Compute resources. If you need cost-effective GPU or CPU instances on demand. You can create a free account on Massed Compute. They provide VM instances, bare metal servers, and bespoke clusters to fit your needs.
Top comments (0)