Resintall CUDA and NVidia drivers
I posted this since many people asked me how to reinstall CUDA and Nvidia drivers properly. This tutorials never failed me.
Important Considerations
The CUDA version displayed by
nvidia-smi
doesn’t necessarily reflect the CUDA version used in your PyTorch.
nvidia-smi
andnvcc
report different versions because they serve different purposes.nvidia-smi
indicates the highest CUDA version supported/installed by your driver, whilenvcc -V
reveals the CUDA installation version your terminal points to. Generally, ifnvidia-smi
shows a CUDA version equal to or higher than that ofnvcc -V
, there’s no need for concern. This discrepancy is typical, as newer drivers can support older CUDA toolkits.
Checking CUDA Version
nvcc -V
Reinstalling Driver and CUDA to a Specific Version
- Switch to tty3: Press Ctrl + Alt + F3.
- Unload
nvidia-drm
:- Switch to multi-user.target:
sudo systemctl isolate multi-user.target
- Check if nvidia-drm is in use:
lsmod | grep nvidia.drm
- Unload nvidia-drm:
sudo modprobe -r nvidia-drm
- Confirm nvidia-drm is unloaded:
lsmod | grep nvidia.drm
- Switch to multi-user.target:
- Remove existing CUDA and NVIDIA Drivers:
sudo apt --purge remove "cublas*" "cuda*" "nvidia*" sudo rm -rf /usr/local/cuda* sudo apt-get autoremove && sudo apt-get autoclean
- Reboot and repeat step 2.
- Install the NVIDIA Driver:
- Download the driver from NVIDIA’s website.
- Log out from the GUI and switch to a terminal (Ctrl + Alt + F2 or F3).
- Stop the current X-Server:
sudo service lightdm stop
- Enter runlevel 3:
sudo init 3
- Navigate to the directory containing the downloaded installer.
- Make the installer executable:
chmod +x ./your-nvidia-file.run
- Run the installer:
sudo ./your-nvidia-file.run
- Reboot your system.
- Download and Install a Specific CUDA Version:
- Follow the installation procedure outlined here: CUDA Installation Guide.
- It’s recommended to use the network installer.
- Remember to specify the version during installation:
sudo apt-get -y install cuda-11.8
- Set Environment Variables:
- Adjust for your CUDA version and shell (e.g., bash, zsh). Check your shell with
echo $0
. - Add these lines to your shell configuration file:
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
- Adjust for your CUDA version and shell (e.g., bash, zsh). Check your shell with
- Reboot your system.
- Install CuDNN (Optional):
- Download from the CuDNN Archive.
References