Learn how to install CUDA 12.2 on Ubuntu 24.04. Follow our step-by-step guide to set up NVIDIA drivers, CUDA Toolkit, and manage Python environments with Pyenv, Pipenv, and Poetry.
So, Ubuntu 24.04 LTS was released on 25 April 2024. Its been more 3 months from the release and I thought it would be safe enough to install it in my main working computer. Wrong!!
I wanted a documentation for setting up the CUDA 12.2 in Ubuntu 24.04 but ended for reinstalling everything. Thus, here is the blog for future me to setup everything. Thanks to all the great help I found in the Internet [ referenced as links ].
At first glance, it was nice and clean. I really the ubuntu drivers, may be it was in the ubuntu 22.x or 20.04.
1
sudo ubuntu-drivers list
1
2
3
4
5
6
7
8
9
10
11
nvidia-driver-520, (kernel modules provided by nvidia-dkms-520)nvidia-driver-470-server, (kernel modules provided by linux-modules-nvidia-470-server-generic-hwe-24.04)nvidia-driver-555, (kernel modules provided by nvidia-dkms-555)nvidia-driver-470, (kernel modules provided by linux-modules-nvidia-470-generic-hwe-24.04)nvidia-driver-535-server, (kernel modules provided by linux-modules-nvidia-535-server-generic-hwe-24.04)nvidia-driver-515, (kernel modules provided by nvidia-dkms-515)nvidia-driver-550, (kernel modules provided by linux-modules-nvidia-550-generic-hwe-24.04)nvidia-driver-525, (kernel modules provided by nvidia-dkms-525)nvidia-driver-535, (kernel modules provided by linux-modules-nvidia-535-generic-hwe-24.04)nvidia-driver-545, (kernel modules provided by nvidia-dkms-545)nvidia-driver-560, (kernel modules provided by nvidia-dkms-560)
< The result might be different for the fresh out of the box Ubuntu >
and the best advertised part was :
1
2
sudo ubuntu-drivers autoinstall
# It will install the best latest version for your device. haha
But, it is great to have all the nvidia-drivers installed right after the fresh OS update. cherry on top, NVIDIA-SMI works. Now, I only need to install libraries like cuda and cudnn for AI/ML pipeline.
so, I chose (CUDA 12.2) as the nvidia driver 535.54.03+ was already installed on. Now to install the CUDA, Nvidia gave you three options based on the Architecture and Distribution selected:
Nvidia CUDA installation
But wait, there is no ubuntu 24.04 version. So, You can’t run the network options or runfile. You have to rely on the 22.04 version deb local method.
so I followed the following instruction method.
Note: I had added the -12-2, otherwise it was installing the latest CUDA, which was 12.6
This thing is great, but I have question for you.
Do you know Nvidia Optimus ? what is Initramfs / prime-select / run level 3 / gdm3 / lightdm / DKMS / MODPROBE / Wayland / X org ?
if not, dont go in the rabbithole as you might end up having problem with it.
if you want to learn about it : Nvidia Optimus: a comfy guide
Luckily the above guide have section : if you see Black Screen after Nvidia drivers install, then check following video
# set PATH for cuda 12.2 installationif[ -d "/usr/local/cuda-12.2/bin/"];thenexportPATH=/usr/local/cuda-12.2/bin${PATH:+:${PATH}}exportLD_LIBRARY_PATH=/usr/local/cuda-12.2/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}fi
That should give you nvcc version
1
2
. ~/.profile
nvcc --version
1
2
3
4
5
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2023 NVIDIA Corporation
Built on Tue_Aug_15_22:02:13_PDT_2023
Cuda compilation tools, release 12.2, V12.2.140
Build cuda_12.2.r12.2/compiler.33191640_0
You can install cuDNN if you are into training Neural Network, check the compability version of cuDNN again: compability matrix
Here is a small script to test the GPU and the packages.
You need to install Torch, Torchvision , Numpy to run the script.
You can use the latest packages for this.
importtorchvisionimporttorchimporttracebacktcuda:torch.cuda=torch.cudaprint(f"How many gpu in the server ? -> {tcuda.device_count()}")print(f"can torch access {tcuda.device_count()} GPUs? :> {tcuda.is_available()}")print(f"Attempting to access GPU device name : {tcuda.current_device()} ....")try:model=torchvision.models.detection.retinanet_resnet50_fpn(pretrained=True)model.cuda()model.eval()x=[torch.rand(3,300,400).cuda(),torch.rand(3,500,400).cuda()]predictions=model(x)print(predictions)print(f"is torch gpu is initialized ? :> {tcuda.is_initialized()}")print(f"torch cuda memory summary :> {tcuda.memory_summary()}")print(f"memory reserved {tcuda.memory_reserved()}")print(f"memory allocated {tcuda.memory_allocated()}")exceptExceptionase:print(e)print("No CUDA available")traceback.print_exc()