CUDA and OpenCL typically needs the latest and greatest drivers in order to work. Installing these drivers is obviously very operating system specific, and also depends on each video card vendor.
CUDA is, of course, specific to nVidia based hardware. OpenCL is supported by a range of hardware manufacturers.
Ubuntu provides an open source driver for nVidia hardware. However, to use CUDA you need the proprietary nVidia drivers. This can be readily installed using System>Administration>Additional Drivers.
After installing the new drivers, you need to reboot the machine.
The proprietary driver installed will not be the latest development driver, but rather an older, tested and stable driver. For example, on Ubuntu 22.04.1 LTS this would be the 525 driver. The CUDA runtime requires that the driver version be a minimum version number depending on the CUDA Toolkit. If you have the latest and greatest CUDA Toolkit, you also need the latest and greatest driver. If you do not, the program will fail to run.
DO NOT manually install a device driver unless you must, as doing so will cause you endless trouble and could brick your installation.
Ubuntu 22.04.1 provides an CUDA 11.5 using
apt-get install nvidia-cuda-toolkit
I recommend you just use this version of CUDA for this class. It is by far the
least headaches. Note that installing CUDA also installs OpenCL.
This command should work for CUDA
nvcc -o $@ $^ -lcuda
and for OpenCL
g++ -Wall -o $@ $^ -lOpenCL