circuit

How to Run VS Code Server with Google Colab


Colaboratory, Google Colab or “Colab” for short, is a product from Google Research. Colab allows anybody to write and execute arbitrary python code through the browser, and is especially well suited to machine learning, data analysis and education.

Visual Studio Code, also commonly referred to as VS Code, is a source-code editor made by Microsoft for Windows, Linux, and macOS. Features include support for debugging, syntax highlighting, intelligent code completion, snippets, code refactoring, and embedded Git

Data scientists or Python developers/programmers may already be familiar with Google Colab.

However, did you know that you may install Visual Studio Code on Google Colab and use it in the same way as on your local machine?

In this article, you will learn:

  1. How to set up the Python Colab code package.
  2. Installing and running VS Code (code server).
  3. how to get to VS Code online.
  4. Opening the terminal.
  5. How to execute a Python script.

Open Colab Notebook

The first step is to launch a new Colab notebook in your Google Colab. You can rename the file as you want.

You can do this by going to https://colab.research.google.com/ and then creating a new notebook.

Install colabcode Python package.

To use Google Colab with VS Code (code server), you need to install the colabcode Python package. This is an awesome open-source Python package developed by Abhishek Thakur.

To install the package, run the following command in your notebook cell:

!pip install colabcode

Install and Authenticate Ngrok

ngrok is a globally distributed reverse proxy fronting your web services running in any cloud or private network, or your machine.

Before you can run the codeserver from your browser without getting errors you need to install and authenticate Ngrok with your colab instance. To do this you need to sign up for an ngrok account and get your authtoken. To sign up visit: http://ngrok.com

After signing up hit the sidebar and you’d see something like this in the picture.

Click on “Your Authtoken” then copy your token.

After you’ve completed the above steps run the code below in a new cell in your google colab instance:

!wget https://bin.equinox.io/c/bNyj1mQVY4c/ngrok-v3-stable-linux-amd64.tgz
!tar -xvzf ngrok-v3-stable-linux-amd64.tgz
!ngrok authtoken your_authtoken

Replace your_authtoken with your actual authtoken.

Import ColabCode

The next step is to import the ColabCode class from the package.

from colabcode import ColabCode

Create an instance of ColabCode

After importing ColabCode, you need to create an instance of ColabCode and set the following arguments:

  • port — The port you want to run the code-server on. For example port=10000
  • password — You can set a password to protect your code-server from unauthorized access. This is an optional argument.
  • mount_drive — If you want to use your Google drive. This is a Boolean argument which means you can set it to True or False. This is an optional argument.
ColabCode(port=10000) or
ColabCode(port=10000, password='mypassword', mount_drive=True)

Start the Code Server

After running the ColabCode instance, it will start the server and show the link to access the code server.

You need to click the link and it will open in a new tab.

You may now use a complete code editor and do various experiments on the Colab VM.

You can observe that the cell that executes the ColabCode instance is always running by looking at your Colab Notebook.
Close your Colab notebook only if you also wish to shut down the server that hosts VS Code.

In the image below, because I set my password I will have to type in my password before I can use the code-server. This is a nice feature if you want more security.

code-server login page:

VS Code Server Page:

Next Steps

After launching the code server, use the following tips to help you start using VS Code on Google Colab.

  1. Launch your terminal:
Ctrl + `
  1. Change your theme

  1. Run a simple Python script

Final Thoughts

You have successfully finished reading this article!
I’m hoping you now know something new.
Take your coding to the next level by installing VS Code on Google Colab.

VS Code can also be run on the Kaggle platform using the colabcode Python package!

You made it to the end of the article! Thanks for reading and hope you learned a lot, if you like my content and want to connect with me you can do that by:

  1. Following Me On Medium.
  2. Connecting With Me On LinkedIn and on Twitter.
  3. Checking out my work on GitHub



Continue Learning