What you need to do to run Colab notebooks in VS Code

Google Colab runs in your browser, but you can write and test code in Visual Studio Code instead by installing the Colab extension. The extension lets you stay in VS Code while your code executes on Colab's free cloud computers. You will need a Google account, VS Code installed on your machine, and the official Google Colab extension from the VS Code marketplace.

The process takes about five minutes: install the extension, sign in with your Google account, and create or open a notebook file. VS Code handles the connection to Colab's servers automatically once you authenticate.

Key Takeaways

  • The Google Colab extension for VS Code is free and installs directly from the VS Code marketplace in under a minute.
  • You must sign in with a Google account that has access to Google Colab before you can run any code.
  • Notebook files in VS Code use the .ipynb format, which is the same format Colab uses, so your files work in both places.
  • Code runs on Colab's servers, not your computer, so you can close VS Code and your notebook will keep running if you set it to do so.

Install the Colab extension in VS Code

Open VS Code and click the Extensions icon on the left sidebar (it looks like four squares). Search for "Google Colab" and select the extension published by Google. Click Install. The extension is about 5 MB and installs in seconds.

Once installed, you will see a Colab icon appear in the left sidebar. You do not need to restart VS Code. The extension is now ready to authenticate.

Sign in with your Google account

Click the Colab icon in the left sidebar. A panel will open asking you to sign in. Click the sign-in button and your browser will open a Google login page. Enter the email and password for the Google account you use for Colab. If you do not have a Colab account yet, you can create one at colab.research.google.com using any Google account.

After you sign in, the browser will show a confirmation message and return you to VS Code. The Colab panel will now show "Connected" and display your Google Drive folders. This connection stays active until you sign out, so you will not need to log in again each time you open VS Code.

Create or open a notebook file

In the Colab panel, you can create a new notebook by clicking the plus icon, or you can open an existing .ipynb file from your computer or Google Drive. If you open a file from your computer, VS Code will ask where to save it — you can keep it local or upload it to Google Drive so Colab can access it.

Once a notebook is open, you will see cells in the editor. Each cell is a block of code or text. Click inside a cell to edit it, just like you would in a regular Python file. The notebook format lets you run cells one at a time instead of running the whole file at once.

Run code cells and see output

To run a single cell, click the play button that appears to the left of the cell, or press Ctrl+Enter (Windows/Linux) or Cmd+Enter (Mac). Your code will execute on Colab's servers, and the output will appear directly below the cell in VS Code. If your code takes time to run, you will see a spinner while it processes.

You can run cells in any order, but keep in mind that variables and imports from earlier cells stay in memory. If you run a cell that uses a variable defined in a later cell, you will get an error. Run cells from top to bottom to avoid confusion, or restart the kernel (the connection to Colab's computer) from the Colab panel to clear all variables and start fresh.

Install packages and use GPU or TPU

You can install Python packages using pip inside a code cell, just as you would in regular Colab. Type !pip install package-name in a cell and run it. The package will install on Colab's server, not on your computer.

To use GPU or TPU acceleration, you must change the runtime type in Colab itself, not in VS Code. Open your notebook in a browser at colab.research.google.com, click the Runtime menu, select Change Runtime Type, and choose GPU or TPU. VS Code will use that same runtime the next time you run a cell. Free GPU access is limited to a few hours per day, and TPU access requires a Colab Pro subscription.

Save your work and manage files

When you edit a notebook in VS Code, changes are saved to your local file automatically. If you want to save the notebook to Google Drive so you can access it from Colab in a browser, use the Colab panel to upload it, or save it manually in VS Code and drag it into Google Drive.

Notebooks saved in Google Drive stay synced across Colab and VS Code. If you edit the same notebook in both places, the most recent save wins — there is no automatic merging, so avoid editing the same file in both at the same time.

Frequently Asked Questions

Do I need Colab Pro to use the extension?

No. The free version of Colab works with the VS Code extension. Colab Pro gives you longer runtimes and priority access to GPUs, but the extension itself is free and works with the free tier.

Can I run code on my own computer instead of Colab's servers?

The Colab extension specifically connects to Colab's cloud servers. If you want to run notebooks locally, you can use Jupyter in VS Code instead — install the Jupyter extension and Python, and notebooks will run on your machine without needing Colab.

What happens if I close VS Code while code is running?

The code will keep running on Colab's servers. You can close VS Code, close your browser, or shut down your computer, and the cell will finish executing. When you reconnect, you can see the output that was generated while you were away.

Can I use the extension offline?

No. The extension requires an active internet connection to Colab's servers. You can edit notebook files offline in VS Code, but you cannot run code until you reconnect to the internet and sign back in.

Why is my code running slowly?

Colab's free tier shares resources among many users, so performance varies. If you need consistent speed, Colab Pro offers faster CPUs and GPUs. You can also check Colab's status page to see if the service is under heavy load.