Created
June 21, 2024 14:21
-
-
Save spy86/37e91468b1bb887d0d7460f990e0c35b to your computer and use it in GitHub Desktop.
script will guide you through the steps to uninstall any existing NVIDIA drivers, install the latest NVIDIA drivers and CUDA toolkit, and verify the installation by running a sample CUDA program.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Uninstall existing NVIDIA drivers | |
sudo apt-get purge -y 'nvidia*' | |
sudo apt-get autoremove -y | |
sudo apt-get autoclean -y | |
# Add the graphics drivers PPA | |
sudo add-apt-repository -y ppa:graphics-drivers/ppa | |
sudo apt-get update -y | |
# Install recommended NVIDIA driver | |
RECOMMENDED_DRIVER=$(ubuntu-drivers devices | grep -oP 'nvidia-driver-\d+') | |
sudo apt-get install -y $RECOMMENDED_DRIVER | |
sudo reboot | |
# Install CUDA Toolkit | |
CUDA_URL="https://developer.download.nvidia.com/compute/cuda/11.6.2/local_installers/cuda_11.6.2_470.57.02_linux.run" | |
CUDA_INSTALLER="cuda_11.6.2_470.57.02_linux.run" | |
wget $CUDA_URL | |
chmod +x $CUDA_INSTALLER | |
sudo ./$CUDA_INSTALLER | |
# Set up environment variables | |
echo 'export PATH=/usr/local/cuda-11.6/bin${PATH:+:${PATH}}' >> ~/.bashrc | |
echo 'export LD_LIBRARY_PATH=/usr/local/cuda-11.6/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}' >> ~/.bashrc | |
source ~/.bashrc | |
# Verify CUDA installation | |
nvcc -V | |
# Compile and run a sample CUDA program | |
cat <<EOF > sample.cu | |
#include <iostream> | |
__global__ void helloFromGPU() { | |
printf("Hello World from GPU!\n"); | |
} | |
int main() { | |
helloFromGPU<<<1, 10>>>(); | |
cudaDeviceSynchronize(); | |
return 0; | |
} | |
EOF | |
nvcc -o sample sample.cu | |
./sample |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment