60

It seems that Google Colab GPU's doesn't come with CUDA Toolkit, how can I install CUDA in Google Colab GPU's. I am getting this error in installing mxnet in Google Colab.

Installing collected packages: mxnet
Successfully installed mxnet-1.2.0

ERROR: Incomplete installation for leveraging GPUs for computations. Please make sure you have CUDA installed and run the following line in your terminal and try again:

pip uninstall -y mxnet && pip install mxnet-cu90==1.1.0

Adjust 'cu90' depending on your CUDA version ('cu75' and 'cu80' are also available). You can also disable GPU usage altogether by invoking turicreate.config.set_num_gpus(0). An exception has occurred, use %tb to see the full traceback.

SystemExit: 1
1
  • The entire premise of this question is wrong. Colab instances are provisioned with a full CUDA toolkit. What version can vary depending on the hardware you wind up with. On any stock instance you can run nvcc to see which toolkit version you have been provisioned with
    – talonmies
    Commented May 23, 2021 at 10:13

8 Answers 8

95

Cuda is not showing on your notebook because you have not enabled GPU in Colab.

The Google Colab comes with both options GPU or without GPU. You can enable or disable GPU in runtime settings

Go to Menu > Runtime > Change runtime.

As shown in the following image, you can select one of the GPUs from the green-encircled options.

Keep in mind black names are free and that grey names are paid GPUs available through Google Colab Pro

GPU Settings Screenshot

To check if the GPU is running or not, run the following command

!nvidia-smi

If the output is like the following image, your GPU and CUDA are working. You can see the CUDA version also.cuda confirmation screenshot

After that to check if PyTorch can use GPU, run the following code.

import torch
torch.cuda.is_available()
# Output would be True if Pytorch is using GPU otherwise it would be False.

Run the following code to check if TensorFlow can use GPU.

import tensorflow as tf
tf.test.gpu_device_name()
# Standard output is '/device:GPU:0'
29

I pretty much believe that Google Colab has Cuda pre-installed... You can make sure by opening a new notebook and type !nvcc --version which would return the installed Cuda version.

Here is mine: enter image description here

3
  • 3
    Not a sufficient condition. With PyTorch, I get False when running this code: torch.cuda.is_available(). Need to change runtime to include GPU. Commented Feb 25, 2020 at 8:57
  • Maybe this would help you. stackoverflow.com/a/60338745/7001213
    – Ahwar
    Commented Mar 17, 2020 at 7:43
  • 1
    @coder.in.me: It is a sufficient condition if you want to access a CUDA toolkit to build and install packages, which is the actual question
    – talonmies
    Commented May 24, 2021 at 2:49
17
  1. Go here: https://developer.nvidia.com/cuda-downloads
  2. Select Linux -> x86_64 -> Ubuntu -> 16.04 -> deb (local)
  3. Copy link from the download button.
  4. Now you have to compose the sequence of commands. First one will be the call to wget that will download CUDA installer from the link you saved on step 3
  5. There will be installation instruction under "Base installer" section. Copy them as well, but remove sudo from all the lines.
  6. Preface each line with commands with !, insert into a cell and run
  7. For me the command sequence was the following:
    !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
  8. Now finally install mxnet. As cuda version I installed above is 9.2 I had to slighly change your command: !pip install mxnet-cu92
  9. Successfully installed graphviz-0.8.3 mxnet-cu92-1.2.0
2
  • 2
    I tried installing it today and noticed that OS has been updated to Ubuntu 18.04. You can check using !lsb_release -a command. And there's Cuda 10.0 available. Commented Oct 16, 2018 at 19:31
  • 1
    @Dienow : After following step 7, I tried the !nvcc --version and the version seems to be still 10.1, and I tried to install mxnet-cu92, but on importing I get the error : OSError: libcudart.so.9.2: cannot open shared object file: No such file or directory. Also, on the mxnet website, it says to install cudnn 7.4.1, I tried installing it, but my .tgz shows error on trying to unzip by using tar xvzf cudnn-9.2-linux-x64-v7.1.tgz . It exits with an error tar: Exiting with failure status due to previous errors. Also, I agree with Aakash's comment. on 18.04 being there.
    – aspiring1
    Commented May 25, 2019 at 7:58
5

This solution worked for me in November, 2022. Query the version of Ubuntu that Colab is running on (run in notebook using ! or in terminal without):

!lsb_release -a

No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.6 LTS
Release:    18.04
Codename:   bionic

Query the current cuda version in Colab (only for comparision):

!nvcc --version

nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2021 NVIDIA Corporation
Built on Sun_Feb_14_21:12:58_PST_2021
Cuda compilation tools, release 11.2, V11.2.152
Build cuda_11.2.r11.2/compiler.29618528_0 

Next, got to the cuda toolkit archive or latest builds and configure the desired cuda version and os version. The Distribution is Ubuntu. enter image description here

Copy the installation instructions:

wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-ubuntu1804.pin
sudo mv cuda-ubuntu1804.pin /etc/apt/preferences.d/cuda-repository-pin-600
wget https://developer.download.nvidia.com/compute/cuda/11.7.0/local_installers/cuda-repo-ubuntu1804-11-7-local_11.7.0-515.43.04-1_amd64.deb
sudo dpkg -i cuda-repo-ubuntu1804-11-7-local_11.7.0-515.43.04-1_amd64.deb
sudo cp /var/cuda-repo-ubuntu1804-11-7-local/cuda-*-keyring.gpg /usr/share/keyrings/
sudo apt-get update
sudo apt-get -y install cuda

Change the last line to include your cuda-version e.g., apt-get -y install cuda-11-7. Otherwise a more recent version might be installed.

!wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-ubuntu1804.pin
!mv cuda-ubuntu1804.pin /etc/apt/preferences.d/cuda-repository-pin-600
!wget https://developer.download.nvidia.com/compute/cuda/11.7.0/local_installers/cuda-!repo-ubuntu1804-11-7-local_11.7.0-515.43.04-1_amd64.deb
!dpkg -i cuda-repo-ubuntu1804-11-7-local_11.7.0-515.43.04-1_amd64.deb
!cp /var/cuda-repo-ubuntu1804-11-7-local/cuda-*-keyring.gpg /usr/share/keyrings/
!apt-get update
!apt-get -y install cuda-11-7

Your cuda version will now be updated:

nvcc --version

nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2022 NVIDIA Corporation
Built on Wed_Jun__8_16:49:14_PDT_2022
Cuda compilation tools, release 11.7, V11.7.99
Build cuda_11.7.r11.7/compiler.31442593_0
1
  • 1
    I think this post needs an update. I could downgrade CUDA from version 12.2 to 11.8 but an extra step of update alternatives was needed. I needed to add !update-alternatives --set cuda /usr/local/cuda-11.8 Commented Apr 8 at 20:07
2

If you switch to using GPU then CUDA will be available on your VM. Basically what you need to do is to match MXNet's version with installed CUDA version.

Here's what I used to install MXNet on Colab:

First check the CUDA version

!cat /usr/local/lib/python3.6/dist-packages/external/local_config_cuda/cuda/cuda/cuda_config.h |\
grep TF_CUDA_VERSION

For me it outputted #define TF_CUDA_VERSION "8.0"

Then I installed MXNet with

!pip install mxnet-cu80
1

I think the easiest way here is to install mxnet-cu80. Just use the following code:

!pip install mxnet-cu80
import mxnet as mx

And you could check whether it works by:

a = mx.nd.ones((2, 3), mx.gpu())
b = a * 2 + 1
b.asnumpy()

I think colab right now just support cu80 and higher versions won't work.

For more information, you could see the following two websites:

Google Colab Free GPU Tutorial

Installing mxnet

1
  • 1
    so mxnet-cuXX handles the full installation of cuda dependencies?
    – Skyler
    Commented Dec 7, 2019 at 15:58
1

Google Colab provides a runtime environment with pre-installed GPU drivers and CUDA support, so you don't need to install CUDA manually. However, to ensure that you are using a GPU-accelerated runtime, you need to select a GPU runtime from the "Runtime" menu:

  1. Open a new or existing Colab notebook.
  2. Click on the "Runtime" menu at the top.
  3. Select "Change runtime type."
  4. Select "GPU" from the "Hardware accelerator" dropdown in the pop-up window.
  5. Click "SAVE."

Once you've set the runtime type to GPU, your Colab notebook will run on a GPU-enabled environment with CUDA support.

You can verify the GPU and CUDA installation by running the following code in a code cell:

import torch

print(torch.cuda.is_available())
print(torch.cuda.get_device_name(0))

This code will check if CUDA is available and print the name of the GPU device.

-1

To run in Colab, you need CUDA 8 (mxnet 1.1.0 for cuda 9+ is broken). But Google Colab runs now 9.2. There is, however the way to uninstall 9.2, install 8.0 and then install mxnet 1.1.0 cu80.

The complete jupyter code is here : Medium

Not the answer you're looking for? Browse other questions tagged or ask your own question.