CUDA
Ubuntu
Uninstall CUDA
Linux
Software Removal

How to remove cuda completely from ubuntu?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Removing CUDA completely from Ubuntu can be necessary for various reasons, such as resolving version conflicts, preparing for a fresh installation, or reclaiming disk space. This comprehensive guide will walk you through the steps needed to effectively remove CUDA from your Ubuntu system, ensuring no residual files affect future software installations or operations.

Understanding CUDA in Ubuntu

CUDA (Compute Unified Device Architecture) is a parallel computing platform and programming model developed by NVIDIA. It allows developers to leverage the power of NVIDIA GPUs for general-purpose processing. When installing CUDA on Ubuntu, it includes multiple components such as CUDA Toolkit, driver, samples, and more.

Several methods can be used for CUDA installation, like package manager installation or using the runfile installer. The removal process slightly differs depending on the installation method used. This guide will cover both scenarios.

Step-by-Step CUDA Removal Process

1. Determine the Installation Method

Before proceeding, it's crucial to determine how CUDA was installed:

  • Package Manager: If you used apt or apt-get, the installation method is via a package manager.
  • Runfile: If you executed a file with a .run extension, it’s a runfile installation.

Use the command below to check for installed CUDA packages and configuration files which will help you in identifying the method:

bash
dpkg -l | grep -i nvidia

2. Stopping CUDA Services

Regardless of the method used, it's helpful to stop any services that might be using CUDA before purging it:

bash
sudo systemctl stop nvidia-persistenced
sudo systemctl disable nvidia-persistenced

3. Remove CUDA Installed via Package Manager

If CUDA was installed via apt or apt-get, follow these steps:

  1. List Installed CUDA Packages: Use the following command to list all CUDA-related packages:
bash
   dpkg -l | grep cuda
  1. Remove the Packages: Execute the following command to remove all CUDA packages:
bash
   sudo apt-get --purge remove '*cublas*' '*cufft*' '*curand*' '*cusolver*' '*cusparse*' \
   '*npp*' '*nvgraph*' '*nvinfer*' '*nvrtc*' '*nvml*' '*nvidia*' '*nsight*'
  1. Clean Up Residues: Ensure any leftover dependencies are removed:
bash
   sudo apt-get autoremove
   sudo apt-get autoclean

4. Remove CUDA Installed via Runfile

For systems where CUDA was installed via a .run file, follow these steps:

  1. List CUDA Installations: Check the directory where CUDA is typically installed:
bash
   ls /usr/local/
  1. Remove CUDA Directories:
bash
   sudo rm -rf /usr/local/cuda*
  1. Revert Environment Variables and Path Modifications:
    Remove or comment out any CUDA-related paths from your profile scripts (~/.bashrc, ~/.zshrc):
bash
   nano ~/.bashrc

Delete lines like:

bash
   export PATH=/usr/local/cuda/bin${PATH:+:${PATH}}
   export LD_LIBRARY_PATH=/usr/local/cuda/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}

After editing and saving, refresh the environment variables:

bash
   source ~/.bashrc

5. Remove NVIDIA Drivers (Optional)

If you wish to remove NVIDIA drivers as well, execute:

bash
sudo apt-get --purge remove '*nvidia*'

6. Verification of CUDA Uninstallation

To verify that CUDA has been uninstalled, attempt the following:

  • Ensure nvcc is unresponsive:
bash
  nvcc --version

It should return an error if CUDA is no longer installed.

  • Check GPU Status:
bash
  nvidia-smi

This should only display information if NVIDIA drivers are still installed.

Key Points Summary

TaskCommand or Path
List current NVIDIA packagesdpkg -l | grep -i nvidia
Stop servicessudo systemctl stop nvidia-persistenced
Remove CUDA via aptRemove sudo apt-get --purge remove '*cuda*'
Clean-up residuessudo apt-get autoremove sudo apt-get autoclean
Remove directories (runfile)sudo rm -rf /usr/local/cuda*
Reset environment variablesRemove from ~/.bashrc, then source ~/.bashrc
Remove drivers (optional)sudo apt-get --purge remove '*nvidia*'
Verificationnvcc --version nvidia-smi

Conclusion

Removing CUDA from an Ubuntu system involves a systematic approach to ensure complete eradication of all related files. Using package management tools simplifies this process, but even .run file installations can be handled effectively with careful directory management and path reset. Understanding how to manage these components ensures a clean system, ready for new installations or configurations.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.