How to Install CUDA on Ubuntu 22.04? (Step-by-Step CUDA Installation on Ubuntu)

The NVIDIA CUDA Toolkit is a software platform designed to help developers efficiently run GPU-accelerated applications by leveraging the computational capabilities of NVIDIA GPUs.

Compute Unified Device Architecture (CUDA), offers significant performance improvements, support for heterogeneous acceleration, and access to a broad ecosystem, making it an attractive choice for developers looking to accelerate compute-heavy applications.

In this tutorial, we will provide an overview of CUDA and guide you through how to install CUDA on Ubuntu 22.04. This includes steps to install NVIDIA drivers, allowing you to unlock the computational potential of your NVIDIA GPU for demanding workloads.

What is CUDA?

CUDA is a programming model and an API that allows developers to create code capable of running on both CPUs and GPUs while managing data transfers between them efficiently.

CUDA cores are specialized units within a GPU designed to handle parallel computing tasks efficiently. They enable the simultaneous execution of multiple threads, making them ideal for high-performance computations.

With the CUDA Toolkit, you can enhance performance, scalability, and efficiency across various applications, including computing, deep learning, computer vision, gaming, and more.

The toolkit supports programming languages such as C, C++, Fortran, Python, and Java, and it integrates seamlessly with popular frameworks and libraries like TensorFlow, PyTorch, OpenCV, and cuDNN.

CUDA’s versatility spans multiple industries, including healthcare, finance, robotics, automotive, and entertainment. Whether you're working on accelerating image processing, natural language processing, cryptography, or ray tracing, the CUDA Toolkit provides the tools needed to achieve faster and more efficient solutions.

The toolkit is compatible with several Linux distributions, such as Ubuntu, Debian, Fedora, CentOS, and OpenSUSE.

Prerequisites

To install CUDA on Ubuntu 22.04, ensure you have the following:

  • The latest version of Ubuntu is installed.
  • An NVIDIA GPU that supports CUDA.

How to Install CUDA on Ubuntu 22.04? [Install Cuda Ubuntu]

By completing the steps outlined below, you'll be able to compile and run CUDA applications, leveraging the parallel processing capabilities of your NVIDIA GPU. First, we’ll install the NVIDIA driver, followed by the CUDA toolkit.

Step 1: Update Ubuntu Apt Repositories

To begin, ensure your Ubuntu system is up-to-date by running the following commands:

$ sudo apt update
$ sudo apt upgrade

how to install cuda on ubuntu 22.04? (step-by-step cuda installation on ubuntu)

Step 2: Install Recommended NVIDIA Drivers Packages

Next, install the ubuntu-drivers-common package and use the ubuntu-drivers command to determine the recommended NVIDIA drivers for your system:

$ sudo apt install ubuntu-drivers-common
$ sudo ubuntu-drivers devices

how to install cuda on ubuntu 22.04? (step-by-step cuda installation on ubuntu)

This command will display details about your system, including the GPU model and the recommended NVIDIA driver.

Step 3: Install NVIDIA Driver 535 on Ubuntu

To install the NVIDIA driver, use the following command:

$ sudo apt install nvidia-driver-535 

how to install cuda on ubuntu 22.04? (step-by-step cuda installation on ubuntu)

In some cases, Ubuntu may already have the NVIDIA driver pre-installed by default.

Step 4: Reboot System

After installing the driver, restart your system to ensure the driver is properly loaded:

$ sudo reboot now 

Step 5: Verify the NVIDIA Driver Installation

Once the system reboots, confirm that the driver has been installed correctly by using the NVIDIA System Management Interface (SMI):

$ nvidia-smi

This command will display a table showing the driver version and CUDA driver API compatibility. At this point, the driver is installed, but the CUDA toolkit has not yet been installed.

Step 6: Install GCC on Ubuntu

The GCC compiler is required for installing the CUDA toolkit. Install GCC with the following command:

$ sudo apt install gcc

To confirm GCC is installed, check its version:

$ gcc -v

This will display the installed version of GCC.

Step 7: Install the CUDA Toolkit on Ubuntu [Install Cuda Ubuntu]

Next, download and install the latest CUDA toolkit for Ubuntu. Visit the official Download Page NVIDIA CUDA Toolkit to get the appropriate version for your system.

The site provides detailed instructions for downloading and installing the toolkit. For Ubuntu 22.04, execute the following commands:

$ wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-keyring_1.1-1_all.deb
$ sudo dpkg -i cuda-keyring_1.1-1_all.deb
$ sudo apt-get update
$ sudo apt-get -y install cuda

If you encounter dependency issues during installation, resolve them with:

$ sudo apt --fix-broken install

After the CUDA toolkit installation is complete, restart your system to ensure the required modules are loaded:

$ sudo reboot now

Step 8: Configure Environment Variables

To finalize the CUDA installation, update your environment variables as recommended in the NVIDIA documentation. Add the following lines to the end of your .bashrc file:

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

Save and exit the file using Ctrl+X and Y.

Reload the updated .bashrc file with:

$ . ~/.bashrc

At this point, the CUDA toolkit is fully installed and configured.

Step 9: Check Cuda Version on Ubuntu

With the environment set up, it's time to test if the CUDA toolkit is working correctly. You can do this by checking the version of the CUDA compiler, nvcc.

Run the following command to verify the installation:

$ nvcc -V

This will display the version of nvcc and confirm that the CUDA toolkit is successfully installed.

Conclusion

In this tutorial, we learned how to install CUDA on Ubuntu, along with the necessary NVIDIA drivers and toolkit. In addition, we have also demonstrated how to install NVCC on Ubuntu for running Cuda programs. This setup enables you to harness the full power of your GPU for tasks like scientific simulations and deep learning. For more information, visit the NVIDIA CUDA Zone.

Is your business outgrowing the capabilities of VPS hosting? It’s time to upgrade to the power and reliability of a dedicated server! With a fully dedicated environment, you’ll experience unmatched performance, scalability, and customization tailored to your needs. Plus, enjoy the freedom of unlimited traffic to handle all your demands seamlessly. Don’t wait—unlock the ultimate hosting solution today with BlueServers.com! 🌐Get Your Dedicated Server Now.

Blog