Ollama on Google Colab with Free GPU
This guide explains how to set up and run Ollama on Google Colab, taking advantage of the free T4 GPU tier. We'll also cover how to expose your Ollama instance to the web using ngrok, making it accessible from anywhere.
This setup is perfect for experimenting with large language models without needing powerful local hardware.
Key Configuration Steps
To successfully run and expose Ollama on Google Colab, there are a few critical steps you'll need to follow.
1. Exposing Ollama to the Network
By default, Ollama only listens for requests from the local machine. To make it accessible from outside the Colab environment, you need to set two environment variables before starting the Ollama server:
OLLAMA_HOST='0.0.0.0'tells Ollama to listen on all available network interfaces.OLLAMA_ORIGINS='*'allows cross-origin requests from any domain, which is necessary for ngrok to forward requests correctly.
2. Exposing the URL with ngrok
Google Colab instances are not directly accessible from the public internet. This is where ngrok comes in. It creates a secure tunnel from a public URL to your local Colab environment.
To use ngrok, you'll need to:
Sign up for a free account at ngrok.com.
Get your authtoken from your ngrok dashboard.
Add the token to Google Colab's Secrets Manager under the name
NGROK_TOKEN. This keeps your token secure and allows the notebook to access it without hardcoding.
The Python script will then use this token to authenticate with ngrok and create the public URL.
Google Colab Notebook
Here is the complete script to run in your Google Colab notebook. You can copy and paste these cells to get started quickly.
Cell 1: System Setup and Server Launch
This cell installs necessary packages, sets up authentication, and starts the Ollama server.
Cell 2: Pulling Models
Use this cell to download the models you want to use. You can uncomment or add any models from the Ollama library.
Cell 3: List Installed Models
This cell lists all the models you have downloaded to confirm they are ready.