How to install cuda on ubuntu 22. 04 lts

How to Install CUDA on Ubuntu 22.04 LTS

The full form of CUDA is Compute Unified Device Architecture. CUDA is a parallel computing platform and programming model developed by NVIDIA. It is used to run the programs on NVIDIA Graphics Processing Units (GPUs) to dramatically speed up the computing applications.

In this article, we will show you how to install CUDA on Ubuntu 22.04 LTS from the official package repository of Ubuntu. We will also show you how to write, compile, and run your very first CUDA program on Ubuntu 22.04 LTS as well.

Topic of Contents:

  1. Prerequisites
  2. Installing the NVIDIA Drivers on Ubuntu
  3. Updating the APT Package Repository Cache
  4. Sponsored
  5. Installing GCC and Other Build Tools
  6. Installing CUDA on Ubuntu from the Official Ubuntu Package Repository
  7. Testing If CUDA Is Installed Successfully on Ubuntu
  8. Writing, Compiling, and Running a Simple CUDA Program
  9. Conclusion

Prerequisites

For you to install CUDA, compile the CUDA programs, and run the CUDA programs on Ubuntu 22.04 LTS operating system, you need the following:

  1. An installed NVIDIA GPU on your computer.
  2. Installed NVIDIA GPU drivers on your Ubuntu operating system.

Installing NVIDIA Drivers on Ubuntu

You must have the NVIDIA GPU drivers installed on your Ubuntu operating system for CUDA to work. If you haven’t yet installed the NVIDIA GPU drivers on your Ubuntu machine and if you need any assistance to do that, read the article on Installing NVIDIA Drivers on Ubuntu 22.04 LTS.

Updating the APT Package Repository Cache

Once you installed the NVIDIA drivers on Ubuntu, update the APT package repository cache with the following command:

$ sudo apt update

How to install cuda on ubuntu 22. 04 lts 1

The APT package repository cache of Ubuntu should be updated.

How to install cuda on ubuntu 22. 04 lts 2

Installing GCC and Other Build Tools

To compile the CUDA programs, you need to have GCC and some other build tools installed on your Ubuntu machine.

To install the GCC compiler and the required build tools on Ubuntu, run the following command:

$ sudo apt install build-essential

How to install cuda on ubuntu 22. 04 lts 3

To confirm the installation, press Y and then press .

How to install cuda on ubuntu 22. 04 lts 4

GCC and the required packages are now being downloaded. It takes a while to complete.

How to install cuda on ubuntu 22. 04 lts 5

GCC and the required packages are now being installed. It takes a while to complete.

How to install cuda on ubuntu 22. 04 lts 6

GCC and the required build tools for CUDA to work should be installed at this point.

How to install cuda on ubuntu 22. 04 lts 7

To check whether you can access the GCC C and C++ compilers, run the following command:

$ gcc –version
$ g++ –version

How to install cuda on ubuntu 22. 04 lts 8

Install CUDA on Ubuntu from the Official Ubuntu Package Repository

To install CUDA from the official package repository of Ubuntu, run the following command:

$ sudo apt install nvidia-cuda-toolkit nvidia-cuda-toolkit-gcc

How to install cuda on ubuntu 22. 04 lts 9

To confirm the installation, press Y and then press .

How to install cuda on ubuntu 22. 04 lts 10

CUDA and the required packages are now being downloaded. It takes a while to complete.

How to install cuda on ubuntu 22. 04 lts 11

CUDA and the required packages are now being installed. It takes a while to complete.

How to install cuda on ubuntu 22. 04 lts 12

Sponsored

CUDA should be installed at this point.

How to install cuda on ubuntu 22. 04 lts 13

Testing If CUDA Is Installed Successfully on Ubuntu

To check if CUDA is installed successfully on Ubuntu, run the following command:

$ nvcc –version

How to install cuda on ubuntu 22. 04 lts 14

As you can see, CUDA version 11.5 is installed on our Ubuntu machine.

How to install cuda on ubuntu 22. 04 lts 15

Writing, Compiling, and Running a Simple CUDA Program

Now that you installed CUDA on your Ubuntu 22.04 LTS machine, we will show you how to write, compile, and run a very simple CUDA “hello world” program.

First, create a new “hello.cu” file (in the ~/codes directory if you want to follow along). Open it with a code editor of your choice, and type in the following lines of codes:

NOTE: CUDA source files end with the “.cu” extension.

Once you’re done, save the “hello.cu” file.

How to install cuda on ubuntu 22. 04 lts 16

To compile the “hello.cu” CUDA program, open a Terminal and navigate to the ~/codes directory (or the directory where you have saved the hello.cu file).

$ cd ~/codes

How to install cuda on ubuntu 22. 04 lts 17

The “hello.cu” CUDA program should be in this directory.

$ ls -lh

How to install cuda on ubuntu 22. 04 lts 18

To compile the “hello.cu” CUDA program with the “nvcc” CUDA compiler and create an executable “hello”, run the following command:

$ nvcc hello.cu -o hello

How to install cuda on ubuntu 22. 04 lts 19

The “hello.cu” CUDA program should be compiled without any errors and a new executable/binary “hello” file should be created as you can see in the following screenshot:

$ ls -lh

How to install cuda on ubuntu 22. 04 lts 20

You can run the compiled “hello” CUDA program as follows:

$ ./hello

How to install cuda on ubuntu 22. 04 lts 21

If you see the following output, CUDA is working just fine on your Ubuntu machine. You should have no problems compiling and running the CUDA programs:

How to install cuda on ubuntu 22. 04 lts 22

Conclusion

We showed you how to install CUDA on Ubuntu 22.04 LTS from the official package repository of Ubuntu. We also showed you how to write, compile, and run a simple CUDA program on Ubuntu 22.04 LTS.


Discover more from Ubuntu-Server.com

Subscribe to get the latest posts sent to your email.

Comments

No comments yet. Why don’t you start the discussion?

    Leave a Reply