CUDA “Compute Unified Device Architecture” is a parallel computing platform and a programming model powered by Nvidia. CUDA toolkit contains everything you need to develop the GPU-accelerated application on Ubuntu. CUDA comes with an API “Application Program Interface” that allows the users to harness the maximum power of the Nvidia GPU “Graphics Processing Unit” for extreme computing tasks. CUDA API allows the users to write code that can run on both CPU and GPU.

Being a programming model, CUDA extends its support to programming languages like C++, Python, Java, etc. CUDA gets integrated with frameworks like OpenCV, PyTorch, and cuDNN.

CUDA-supported Nvidia GPUs have several cores that can run thousands of computing threads collectively. With CUDA, you can perform operations on Machine learning, cybersecurity, robotics, scientific simulations, healthcare, crypto mining, and other extreme computing tasks. 

How to Install Cuda on Ubuntu 24.04

Downloading the CUDA toolkit will also install a compiler, development tools, CUDA runtime, and libraries. CUDA toolkit on Ubuntu can be installed using the official repository and through Nvidia’s official website. 

But before moving further, let’s check the prerequisites to install CUDA on Ubuntu.

Prerequisites to Install Cuda on Ubuntu

To install CUDA on Ubuntu, you must ensure that you have the Nvidia graphics card and the Nvidia drivers installed. Furthermore, the GCC must be installed on Ubuntu. Let’s check if your system is complying with the mentioned requirements.

1. Check if Your GPU is Capable of Running CUDA

Before installing CUDA on Ubuntu, ensure your graphics card can run the CUDA toolkit. To check whether your graphics card is CUDA capable, press CTRL + Alt + T to open the Terminal and run this command:

lspci | grep -i nvidia

Note: If the command returns no output then your graphics card is not CUDA capable.

But, if the graphics card is from Nvidia then you can install the CUDA toolkit on Ubuntu:

2. Install Nvidia Drivers

To install CUDA on Ubuntu, you must have the Nvidia drivers installed on Ubuntu. Run this command to get the recommended Nvidia driver for your device:

sudo ubuntu-drivers devices

Once, you get the recommended driver, then install it by specifying the driver to the apt install command, as shown below:

sudo apt install nvidia-driver-535

Alternatively, you can also install the recommended driver automatically by running this command:

sudo apt auto-install

Once the drivers are installed, reboot the system by running the reboot command in Terminal.

3. Make Sure GCC is Installed

You must have installed GCC also on Ubuntu to install CUDA. GCC “GNU Compiler Collection” is a package containing various compilers, used to write and compile code in various programming languages.

To check if the GCC is already installed, run this command:

gcc -version

If GCC is not installed on Ubuntu, then run this command to install GCC on Ubuntu:

sudo apt install gcc

Method 1: Install Cuda on Ubuntu from the Official Repository

The most convenient and easy method to install CUDA on Ubuntu is from the official repository. However, the official repository does not install the latest CUDA version. Check the given steps to install the CUDA on Ubuntu:

Step 1: Update Ubuntu Packages

Local repositories must be updated to their latest version before installing the CUDA toolkit on Ubuntu. To update local repositories run this command:

sudo apt update && sudo apt upgrade

Step 2: Install Cuda Toolkit on Ubuntu

Now, install the CUDA toolkit on Ubuntu using the apt package manager from the official repository by running the given command:

sudo apt install nvidia-cuda-toolkit

Step 3: Restart your System

After installing the CUDA on Ubuntu, reboot the system so that drivers can be installed and applied to the system. To reboot the system, run the below command:

reboot

Step 4: Verify Ubuntu Installation

Verify the CUDA installation by running the given command:

nvcc --version

Method 2: Install the Latest Version of Cuda on Ubuntu from the Official Website

You can install the latest version of CUDA on Ubuntu through Nvidia repositories. Installing the latest version is a complicated process rather than installing it through the official repository. Check the mentioned steps to install the latest CUDA version on Ubuntu 24.04.

Step 1: Download and Depackage the CUDA Package

Execute these given commands one by one in the Terminal to download and de-package the CUDA packages in Ubuntu:

Download the pin file to get control over paged memory in the CUDA toolkit by running the below command:

wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-ubuntu2204.pin

This command will move the downloaded CUDA pins file from one repository to another repository:

sudo mv cuda-ubuntu2204.pin /etc/apt/preferences.d/cuda-repository-pin-600

Download the CUDA toolkits .deb file on Ubuntu by running the mentioned command in Terminal:

wget https://developer.download.nvidia.com/compute/cuda/12.4.0/local_installers/cuda-repo-ubuntu2204-12-4-local_12.4.0-550.54.14-1_amd64.deb

Now, de-package the CUDA toolkit .deb package by running the below command:

sudo dpkg -i cuda-repo-ubuntu2204-12-4-local_12.4.0-550.54.14-1_amd64.deb

Lastly, copy the keyrings to a specified repository by running the given command:

sudo cp /var/cuda-repo-ubuntu2204-12-4-local/cuda-*-keyring.gpg /usr/share/keyrings/

Step 2: Update System Packages

After downloading the CUDA packages, update the system packages by running this command:

sudo apt-get update

Step 3: Install CUDA

After updating the system packages, run the given command to install CUDA on Ubuntu:

sudo apt-get install cuda-toolkit-12-4 -y

Step 4: Reboot the System

Once installed, restart the system to save the changes by executing the below command:

reboot

Step 5: Verify CUDA Installation

To verify the CUDA installation, check its version by running the below command:

nvcc --version

As you can see it is showing version 10, but we have installed version 12.4. To fix this issue we need to set the CUDA environment settings:

Environment Setup

The last part of the CUDA installation is to update or set up the environment. Updating the system environment will let the system know about the latest installed version of CUDA. you can set up the environment by configuring the .bashrc file. Check the following steps to set up the environment for CUDA on Ubuntu.

Step 1: Edit the .bashrc File

To configure the .bashrc file, open it with the nano editor by running the below command:

sudo nano ~/.bashrc

Step 2: Add the CUDA Environment Path in the .bashrc File

Once the .bashrc file is launched, copy the given paths and paste them into the file, as shown below:

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

Once the paths are added, press CTRL + O and hit Enter to save the changes.

Step 3: Reload the .bashrc File

Once the changes are made, execute this command to reload the .bashrc file so paths can be updated to the system:

source ~/.bashrc

After reloading the .bashrc file, close and re-open the Terminal.

Step 4: Verify the CUDA Version

Now, again check the CUDA version to see if it is showing the latest version by running the given command:

nvcc --version

As you can see the above command is showing the latest version now:

Conclusion

To install CUDA on Ubuntu 24.04, ensure you have the Nvidia graphics card along with Nvidia drivers installed and gcc installed on Ubuntu. Then, run the sudo apt install nvidia-cuda-toolkit command to install the CUDA on Ubuntu from the official repository. Alternatively, you can install the latest version of CUDA on Ubuntu by downloading its latest deb package from the Nvidia website.