'How can I fix this "dpkg" error while installing CUDA on google colab

I want to run CUDA code on google colab. For that I am following the below steps but I am not able to install CUDA packages.

Step 1: Removing previous CUDA versions

!apt-get --purge remove cuda nvidia* libnvidia-*
!dpkg -l | grep cuda- | awk '{print $2}' | xargs -n1 dpkg --purge
!apt-get remove cuda-*
!apt autoremove
!apt-get update

It is running fine.

Step 2: Installing new CUDA packages

!wget https://developer.nvidia.com/compute/cuda/9.2/Prod/local_installers/cuda-repo-ubuntu1604-9-2-local_9.2.88-1_amd64 -O cuda-repo-ubuntu1604-9-2-local_9.2.88-1_amd64.deb
!dpkg -i cuda-repo-ubuntu1604-9-2-local_9.2.88-1_amd64.deb
!apt-key add /var/cuda-repo-9-2-local/7fa2af80.pub
!apt-get update
!apt-get install cuda-9.2

It is giving error at the end like this:

Errors were encountered while processing:
 /tmp/apt-dpkg-install-Bocamn/67-nvidia-396_396.26-0ubuntu1_amd64.deb
E: Sub-process /usr/bin/dpkg returned an error code (1)

Step 3:

!nvcc --version

I am getting the error after this like:

/bin/bash: nvcc: command not found

I was running CUDA code using the above steps before but now it's not working. Can you help me with this error.



Solution 1:[1]

I was running my code using CUDA 10.0 on Google Colab and it was working fine until two weeks ago. I was using a similar code to install CUDA 10.0 on Colab, i.e.,

!apt-get --purge remove cuda nvidia* libnvidia-*
!dpkg -l | grep cuda- | awk '{print $2}' | xargs -n1 dpkg --purge
!apt-get remove cuda-*
!apt autoremove
!apt-get update

!wget  --no-clobber https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-repo-ubuntu1804_10.0.130-1_amd64.deb
#install CUDA kit dpkg
!dpkg -i cuda-repo-ubuntu1804_10.0.130-1_amd64.deb
!sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub
!apt-get update
!apt-get install cuda-10-0

I think the first block of your code is working. Because it successfully uninstalls the current CUDA version on Colab (version 11.2), when you check !nvcc --version you see that there is no CUDA installed. The issue is with the second block of the code where it uses the public key "7fa2af80" to install CUDA 9.2 from this repo https://developer.nvidia.com/compute/cuda/9.2/Prod/local_installers/cuda-repo-ubuntu1604-9-2-local_9.2.88-1_amd64

I guess the problem is that the public key "7fa2af80" is deprecated by NVIDIA from 27 April. You can find more information about this here.

Solution 2:[2]

I came across the same error while trying to set up nvcc on Colab. Colab already has nvcc installed so no need to install it. Unfortunately since you have uninstalled nvcc here, use a separate gmail account and open a new colab notebook.

Run:

nvcc --version

to see if nvcc is already installed.

Next install the extension to run C/C++ CUDA programs by running:

!pip install git+https://github.com/andreinechaev/nvcc4jupyter.git

In the next cell, load the extension installed:

%load_ext nvcc_plugin

Now you can run cuda programs. To run a C CUDA sample code:

%%cu
// Your C code

Solution 3:[3]

I had the same problem, but i found a solution without installing cuda packages.

1- use the default cuda release on colab:

Run !nvcc --version

Output will be:-

nvcc: NVIDIA (R) Cuda compiler driver Copyright (c) 2005-2020 NVIDIA Corporation Built on Mon_Oct_12_20:09:46_PDT_2020 Cuda compilation tools, release 11.1, V11.1.105 Build cuda_11.1.TC455_06.29190527_0


2- Use %%writefile <file_name>.cu instead of %%cu


3- Use !nvcc -arch=sm_37 -gencode=arch=compute_37,code=sm_37 <file_name>.cu -o <file_name> to compile the code


4- Use !./<file_name> to run the code

Thanks to this link which helps me to solve it

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 m3359s
Solution 2 Aayush Patel
Solution 3