How To Run CUDA C/C++ Programs On Google Colab?

If you are learning High-Performance Computing(HPC), you might have come across Compute Unified Device Architecture(CUDA). CUDA is a proprietary technology developed by Nvidia Corporation for GPU computing. To use the CUDA framework for programming you need to have access to CUDA capable Nvidia GPU. If you do not have access to such GPU, you either have to rent such GPU on cloud platforms like Azure, AWS, and Google cloud or you can use free services like Google colaboratory to run your CUDA C/C++ programs. In this article, we will show simple steps to run CUDA C/C++ programs on Google colab notebook.

  • Google account to access Google colab
  • Internet connection

Step 1

Go to the Google colab and click on New Notebook.

Google Colab project selection page.

Step 2

Click on the Runtime menu and then select the Change runtime type option.

Runtime menu

Step 3

Set the Hardware accelerator option to the GPU.

Hardware accelerator selection menu.

Step 4

Run the following commands

# Check the nvcc version.
!nvcc --version

# Install the NVCC Plugin for Jupyter notebook.
!pip install git+git://github.com/andreinechaev/nvcc4jupyter.git

# Load the above extension in the notebook.
%load_ext nvcc_plugin

Note: Each command should be executed on a different cell.

Step 5

Now copy the given code in a new cell and run the cell to execute your code.

#include <iostream>
int main() {
    printf("CUDA is working\n");
    return 0;
// Here, only a simple C program has been executed.
// All the other complex CUDA programs can be executed in the
// Google colab environment by following these instructions.

Note: You have to add %%cu on the first line of your notebook cell where your C/C++ code is written.

C program running on the Google Colab.


In this way, we can easily run CUDA C/C++ programs on the Google Colab platform for free. We hope this article helps you in your AI programming journey.