How can I install CUDA 12.1 on Ubuntu 22.04. I'm going insane here

I just want to install CUDA 12.1 on Ubuntu, the latest version that is compatible with Pytorch. How is it possible to do this? Going through their Cuda 12.1 Toolkit options will install 12.5 every time which is not compatible with pytorch. I think this is an on-going problem, I remember having the same issue when trying to upgrade Cuda in order to go from Pytorch v1 to v2, it would install higher versions than what I requested, that were incompatible with pytorch.

the linux install guide has instructions.

If you do:

sudo apt install cuda

you will get the latest CUDA version available, regardless of which version you attempted to download or use.

To install a version of cuda that is not the latest, you must do:

sudo apt install cuda-X-Y 

where X and Y are the version info, so for CUDA 12.1 you would do X=12, Y=1:

sudo apt install cuda-12-1

If you already have a GPU driver installed that is compatible with CUDA 12.5 (or compatible with some CUDA version that is 12.1 or higher, nvidia-smi will indicate the version your driver is compatible with in the upper right corner of the output), you do not need to reinstall the driver. In that case you could simplify things a bit and do:

sudo apt install cuda-toolkit-12-1

Thanks for the quick response.

I spent an entire day trying everything I could think to do or find on the internet. I should mention that it is an AWS AMI (effectively a backup) instance that I use for deep learning tasks. As such it is usually out of date when I first start it and update/upgrade… the instance. In the process of upgrading pytorch stopped recognising cuda, probably because of a minor package upgrade. sudo apt install cuda/{-12-1} made things worse. I ended up with nvcc, nvidia-smi, ls /usr/local all showing different versions of cuda. I then spent the rest of the day trying every possible solution I could find on the internet to reinstall a particular version of CUDA, 12.1, which is the version compatible with Pytorch. It was a very slow process as I tried to remove cuda completely between each attempt. I will say most of the tutorials, instructions etc. were out of date or didn’t work for my particular setup. But surprisingly the worst of the lot was the Nvidia Cuda Developer helper, which asked about the configuration and helpfully gave specific instructions to install the particular version of Cuda in that environment. The instruction even included downloading installation files with 12.1 in their filenames. Problem was they then tried to install 12.5.

This isn’t a new problem. A while ago I tried to upgrade Cuda so I could move from Pytorch v1 to Pytoch v2. I had exactly the same problem.

I think I might have found a solution, it might be just that the two ‘deb’ installation options install Cuda 12.5 instead of Cuda 12.1 and not the run-file option. But it’s a bit of a disaster whatever the case.