Use ngrok with secret key on Google Colab

0š¯•¸koji
2 min readNov 14, 2023

What is ngrok?

ngrok combines your reverse proxy, firewall, API gateway, and global load balancing to deliver apps and APIs.

https://ngrok.com/

Simply put, ngrok makes it easy to publish a web application running locally with a single command.
This is very useful to build prototyping.

For Google Colab, using ngrok would make things easy if you want to run an open source LLM and use it via an API server (like FastAPI, Flask etc).

How to use ngrok on Google Colab

There are 3 steps to use ngrok with Google Colab secret key.

  1. Download ngrok
  2. Set secret key
  3. Load secret key

Step1. Download ngrok

This step is pretty straightforward.
Download zip file for Linux and unzip it.

!wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip
!unzip -o ngrok-stable-linux-amd64.zip

Step2. Set secret key

Click key icon in the sidebar and click Add a new secret.
You will need to fill two text box. One is the key name and you can name whatever you want and the other is the key value or token. Then you need to click a toggle button to enable the key.

Step3. Load secret key

The following code is very simple, load the secret key and pass it to ngrok via ngrokā€™s python package for authentication.

import os
from google.colab import userdata
from pyngrok import conf
os.environ["NGROK"] = userdata.get("NGROK")
conf.get_default().auth_token = os.environ["NGROK"]

These three steps will work for things that you need to pass a secret key or token on Google Colab.

--

--

0š¯•¸koji

software engineer works for a Biotechnology Research startup in Brooklyn. #CreativeCoding #Art #IoT #MachineLearning #python #typescript #javascript #reactjs