Welcome to the ultimate setup guide for CUDA 11.8, cuDNN, and PyTorch on Ubuntu. Follow these instructions to get your machine learning environment up and running with GPU acceleration!
- Ubuntu Operating System
- NVIDIA GPU (CUDA-Compatible)
- Internet Connection
Before starting, ensure that you have a CUDA-compatible NVIDIA GPU.
lspci | grep -i nvidiaRemove any existing NVIDIA drivers and CUDA installations to prevent conflicts.
sudo apt-get purge nvidia*
sudo apt remove nvidia-*
sudo rm /etc/apt/sources.list.d/cuda*
sudo apt-get autoremove && sudo apt-get autoclean
sudo rm -rf /usr/local/cuda*Keep your system up-to-date for better compatibility.
sudo apt-get update
sudo apt-get upgradeInstall the necessary packages for NVIDIA drivers and CUDA toolkit.
sudo apt-get install g++ freeglut3-dev build-essential libx11-dev libxmu-dev libxi-dev libglu1-mesa libglu1-mesa-devAccess the latest NVIDIA drivers through this repository.
sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt updateBefore installing the drivers, identify the recommended driver for your GPU.
-
Install necessary utilities:
sudo apt-get install alsa-utils sudo apt install ubuntu-drivers-common
-
Check the recommended drivers:
ubuntu-drivers devices
This will output a list of drivers with one marked as recommended.
-
Install the recommended driver:
sudo apt install libnvidia-common-535 sudo apt install libnvidia-gl-535 sudo apt install nvidia-driver-535
Download and install the CUDA Toolkit.
wget https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/cuda-keyring_1.0-1_all.deb
sudo dpkg -i cuda-keyring_1.0-1_all.deb
sudo apt-get update
sudo apt-get -y install cuda-11-8Update your PATH and LD_LIBRARY_PATH variables.
echo 'export PATH=/usr/local/cuda-11.8/bin:$PATH' >> ~/.bashrc
echo 'export LD_LIBRARY_PATH=/usr/local/cuda-11.8/lib64:$LD_LIBRARY_PATH' >> ~/.bashrc
source ~/.bashrc
sudo ldconfigDownload and install cuDNN. Make sure to register at NVIDIA's website before downloading. [https://developer.download.nvidia.com/compute/cudnn/redist/cudnn/linux-x86_64/]
CUDNN_TAR_FILE="cudnn-linux-x86_64-8.9.7.29_cuda11-archive.tar.xz"
wget [YOUR CUDNN DOWNLOAD LINK]
tar -xvf ${CUDNN_TAR_FILE}
sudo cp cudnn-*-archive/include/cudnn*.h /usr/local/cuda/include
sudo cp -P cudnn-*-archive/lib/libcudnn* /usr/local/cuda/lib64
sudo chmod a+r /usr/local/cuda/include/cudnn*.h /usr/local/cuda/lib64/libcudnn*After installing the drivers and CUDA, reboot your system to ensure the drivers are properly synchronized with the GPU hardware.
sudo rebootCheck if NVIDIA drivers and CUDA are installed correctly.
nvidia-smi
nvcc -VFinally, install PyTorch with CUDA support.
pip3 install --force-reinstall torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
pip3 install --force-reinstall torch-cluster torch-geometric torch-scatter torch-sparse torch-spline-conv -f https://data.pyg.org/whl/torch-1.x.x+cu118.htmlYou have successfully set up CUDA, cuDNN, and PyTorch on your Ubuntu system. Start building your amazing machine learning projects!
Note: Always ensure you have the correct versions and your system meets all requirements. Happy coding! 🚀💻