Martin Manullang

Computer Vision Researcher & Tech-savvy

Resintall CUDA and NVidia drivers | Martin Manullang

Resintall CUDA and NVidia drivers

January 22, 2024

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 and nvcc report different versions because they serve different purposes. nvidia-smi indicates the highest CUDA version supported/installed by your driver, while nvcc -V reveals the CUDA installation version your terminal points to. Generally, if nvidia-smi shows a CUDA version equal to or higher than that of nvcc -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

  1. Switch to tty3: Press Ctrl + Alt + F3.
  2. 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
      
  3. 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
    
  4. Reboot and repeat step 2.
  5. 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.
  6. 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
      
  7. 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
      
  8. Reboot your system.
  9. Install CuDNN (Optional):

References