Run localhost server in Google Colab notebook
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Running a web server in Google Colab is possible, but localhost behaves differently than it does on your laptop. The notebook is running on a remote machine, so a server started on 127.0.0.1 is local to the Colab runtime, not directly visible in your browser unless you expose it through a proxy or tunnel.
Understand What localhost Means in Colab
In a normal local development setup, opening http://127.0.0.1:5000 in the browser reaches a server running on your own machine. In Colab, the code runs on a hosted VM, so that same address refers to the VM itself.
That means this pattern works inside the notebook process:
But opening http://127.0.0.1:8000 in your local browser will not reach that server, because your browser is not on the Colab VM.
A Practical Flask Example
For app-style experiments, Flask is a simple choice. In a notebook, run the server in a background thread so the cell does not block the entire notebook.
Install the packages first:
Then start the server:
Using 0.0.0.0 allows the server to listen on the runtime's network interface. It still is not public yet, but it is now available for a tunnel to forward.
Exposing the Server With pyngrok
The usual way to reach the app from your browser is to create a public tunnel. pyngrok gives you a simple Python interface to ngrok.
Once that prints a URL, open it in your browser and the request will be forwarded to the Flask app running inside Colab.
If your ngrok account requires an auth token, configure it first:
This is usually the most reliable way to test a notebook-hosted web app from outside the Colab runtime.
A Minimal End-to-End Example
Putting the pieces together, the full notebook flow looks like this:
That gives you a temporary public entry point to the notebook server. It is good for demos, small experiments, and validating that request handling works.
When This Approach Makes Sense
Running a server in Colab is best for short-lived experiments:
- testing a lightweight Flask endpoint,
- previewing a notebook-driven demo,
- validating a model inference endpoint quickly.
It is not a substitute for a real deployment. Colab runtimes can reset, go idle, or disconnect, which means the server and the public URL disappear with the session.
For anything persistent, deploy the app to an actual hosting environment instead of treating Colab as a web server platform.
Common Pitfalls
- Expecting
localhostin the notebook to be reachable directly from your own browser. - Running
app.run()in the foreground and blocking the notebook session. - Forgetting to expose the port through a tunnel or proxy.
- Assuming the tunnel URL will remain stable after the runtime restarts.
- Putting secrets into notebook cells when configuring public tunnels.
Summary
- A server can run inside Google Colab, but
localhostrefers to the remote runtime, not your computer. - Use a background thread so the notebook stays interactive.
- Expose the port with a tool such as
pyngrokif you need browser access. - Treat the setup as temporary and experimental, not production hosting.
- Keep security in mind, because tunneling makes the notebook service reachable from outside the runtime.

