I recently discovered a way to setup VSCode on Google Colab and use it as an editor to write code and run experiments on the Colab VM.
With this setup, you can still prototype in the Colab Notebook while also using VSCode for all the advantages of an full-fledged code editor. Here is how you can replicate my setup.
I have described the steps in details below. After going through all the steps, please use this colab notebook to try it out directly.
First, we will install the code-server package to run VSCode editor as a web app. Copy and run the following command on colab to install
!curl -fsSL https://code-server.dev/install.sh | sh
After the installation is complete, we will expose a random port
9000to an external URL we can access using the
pyngrokpackage. To install
!pip install -qqq pyngrok
Then, run the following following command to get a public ngrok URL. This will be the URL we will use to access VSCode.
from pyngrok import ngrok url = ngrok.connect(port=9000) print(url)
Now, we will start the VSCode server in the background at port 9000 without any authentication using the following command.
!nohup code-server --port 9000 --auth none &
Now, you can access the VSCode interface at the URL you got from step 3. The interface and functionality is same as the desktop version of VSCode.
- All the keyword shortcuts of regular VSCode works with this. For example, you can use
Ctrl + Shift + Pto open a popup for various actions.
- To open a terminal, you can use the shortcut
Ctrl + Shift + `.
- To get python code completions, you can install the Python(
ms-python) extension from the extensions page on the left sidebar.
- The Colab interface is still usable as a notebook and regular functions to upload and download files and mount with Google Drive. Thus, you get the benefits of both a notebook and a code editor.
If you enjoyed the blog post, feel free to connect with me on Twitter where I share new blog posts every week.