Skip to content
Mohit Acharya

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

CUDA on Google Colab

If you are learning High-Performance Computing (HPC), you have probably come across CUDA. CUDA is a proprietary technology developed by Nvidia Corporation for GPU computing. However, not everyone has access to dedicated GPU hardware. The good news is that you can run CUDA C/C++ programs for free using Google Colaboratory (Colab).

Requirements

That’s it — no GPU hardware needed.

Step 1: Open Google Colab and Create a New Notebook

Go to colab.research.google.com and create a new notebook.

Google Colab project selection page

Step 2: Change the Runtime Type to GPU

Go to Runtime → Change runtime type, then set the Hardware accelerator to GPU and save.

Runtime menu

Step 3: Select GPU Hardware Accelerator

Hardware accelerator selection menu

Step 4: Run the Setup Commands

In separate cells, run the following commands:

Check that NVCC (CUDA compiler) is available:

!nvcc --version

Install the NVCC plugin for Jupyter:

!pip install nvcc4jupyter

Load the extension:

%load_ext nvcc4jupyter

Step 5: Write and Run Your CUDA Code

Now you can write CUDA C/C++ code using the %%cu magic command at the top of a cell:

%%cu
#include <iostream>

int main() {
    printf("CUDA is working\n");
    return 0;
}

C program running on Google Colab

Run the cell and you should see CUDA is working printed as output.

Conclusion

We can easily run CUDA C/C++ programs on the Google Colab platform for free. This makes GPU computing accessible to anyone learning HPC without the need to purchase expensive hardware.


Share this post on:

Previous Post
Top Free Resources to Learn the MERN Stack