How to Install CUDA on Ubuntu 22.04 | Step-by-Step

October 18th, 2023
How to Install CUDA on Ubuntu 22.04 | Step-by-Step

CUDA (Compute Unified Device Architecture), a parallel computing platform, allows software developers to use a GPU for general-purpose processing. Performance gains, heterogeneous acceleration capabilities, and a vast ecosystem make CUDA very appealing for developers targeting the acceleration of workloads.

This tutorial will first briefly explain what CUDA is, after which we will show you how to install CUDA on Ubuntu 22.04, including how to install the NVIDIA drivers so that you can unlock your NVIDIA GPUs capabilities for compute-intensive applications.

What is CUDA?

CUDA (Compute Unified Device Architecture) is a parallel computing platform developed by NVIDIA. It provides an application programming interface (API) that enables developers to harness the immense processing power of NVIDIA graphics processing units (GPUs) for general-purpose computing.

CUDA cores are specialized processing units within the GPU optimized for parallel computing tasks. They are capable of executing multiple threads concurrently.

What is CUDA used for?

With its parallel computing feature and the ever-growing power of GPUs, CUDA can be used for machine learning and AI applications, scientific simulations involving complex calculations, financial modeling, cybersecurity, cryptocurrency mining, and other computationally intensive tasks.

In this tutorial, we will cover how to install CUDA on Ubuntu 22.04 and install the NVIDIA drivers.

Prerequisites

To follow along with this tutorial, you will need the following:

How to Install CUDA on Ubuntu 22.04: Step-by-step

After completing the following steps, you can compile and execute CUDA applications, taking advantage of the parallel processing power of your NVIDIA GPU. We will first install the NVIDIA driver and then proceed to install the CUDA toolkit.

Step 1: Upgrade your Ubuntu

We will start by upgrading our Ubuntu to ensure we have all the latest software. Use the following command to upgrade your system:

sudo apt update
sudo apt upgrade 

apt update and apt upgrade

Step 2: List the recommended NVIDIA drivers

We will now isntall ubuntu-drivers-common and use the ubuntu-drivers command to get the list of recommended NVIDIA drivers available for your system:

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

ubuntu-drivers devices

We can see that the ubuntu-drivers command lists the model of our computer as well as the NVIDIA GPU installed.

model    : GP108M [GeForce MX150] (Mi Notebook Pro [GeForce MX150])

It also recommends the NVIDIA driver 535

driver   : nvidia-driver-535 - distro non-free recommended

Step 3: Install the driver nvidia-driver-535

Let's install the driver using the following command:

sudo apt install nvidia-driver-535

apt install nvidia-driver-535

In this case, the NVIDIA driver was already installed by default on Ubuntu.

Also read: How to Add a User to Sudoers in Ubuntu

Step 4: Reboot your system

We will now reboot the system to ensure everything has been installed and loaded correctly.

sudo reboot now

sudo reboot now

Step 5: Check the driver installation

Now that we have rebooted our system, we will check our installation with the NVIDIA System Management Interface, which is called with the nvidia-smi command.

nvidia-smi

nvidia-smi

At the top of the table, we will see the driver version and CUDA driver API compatibility:

NVIDIA-SMI 535.86.05              Driver Version: 535.86.05    CUDA Version: 12.2  

Please note that CUDA is not yet installed at this stage.

Step 6: Install GCC

We will need to install the gcc compiler as it will be used when installing the CUDA toolkit. Make sure that you have gcc installed with the following command:

sudo apt install gcc

apt install gcc

You can verify if gcc has been installed using the following command:

gcc -v

This should return the version of gcc that has been installed.

Step 7: Install CUDA toolkit Ubuntu

We will now head to the NVIDIA CUDA download website to get the latest CUDA toolkit for Ubuntu. The website will navigate you through the right package to download as well as the commands to execute to complete the CUDA toolkit installation.

CUDA toolkit download

The website provides the following procedure:

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

Please proceed by executing each of the commands to complete the installation.

If you encounter dependency errors during the installation, try running sudo apt --fix-broken install to fix them. Apt will suggest running it if needed.

Step 8: Reboot your system

Now that the CUDA toolkit has been installed, we will reboot the system to load the right modules required by CUDA.

sudo reboot now

Step 9: Environment setup

The CUDA toolkit is now installed, and a few manual actions must be executed to complete the setup. We will now proceed to update the environment variables as recommended by the NVIDIA documentation.

Add the following line to your .bashrc file using nano ~/.bashrc and paste the following lines at the end of the 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 the file using Ctrl+x and y.

We will now reload the file:

. ~/.bashrc

Step 10: Test the CUDA toolkit

Now that the environment has been set, we can test the CUDA toolkit, and we can execute nvcc, the CUDA compiler.

nvcc -V

nvcc

This returns the nvcc version and confirms that the CUDA toolkit has been installed.

Conclusion

In this tutorial, we have learned how to install CUDA on Ubuntu, the latest NVIDIA drivers, the CUDA toolkit, and how NVIDIA has brought general-purpose and parallel computing to GPUs. You can now use your CUDA setup to work with scientific simulations or get the most out of your deep learning applications. Find more information on CUDA on the NVIDIA CUDA zone website.

Once you have CUDA successfully installed on your Ubuntu system, you can unlock its full performance in a hassle-free cloud environment with Cherry Servers cost-effective dedicated GPU servers. Choose your GPU accelerator, configure it with your server, and deploy in 2 to 24 hours.

Mantas is a hands-on growth marketer with expertise in Linux, Ansible, Python, Git, Docker, dbt, PostgreSQL, Power BI, analytics engineering, and technical writing. With more than seven years of experience in a fast-paced Cloud Computing market, Mantas is responsible for creating and implementing data-driven growth marketing strategies concerning PPC, SEO, email, and affiliate marketing initiatives in the company. In addition to business expertise, Mantas also has hands-on experience working with cloud-native and analytics engineering technologies. He is also an expert in authoring topics like Ubuntu, Ansible, Docker, GPU computing, and other DevOps-related technologies. Mantas received his B.Sc. in Psychology from Vilnius University and resides in Siauliai, Lithuania.

Start Building Now

Deploy your new Cloud VPS server in 3 minutes starting from $5.83 / month.

We use cookies to ensure seamless user experience for our website. Required cookies - technical, functional and analytical - are set automatically. Please accept the use of targeted cookies to ensure the best marketing experience for your user journey. You may revoke your consent at any time through our Cookie Policy.
build: 5bc831c3.737