GitHub - googlecolab/google-colab-cli
Service

GitHub - googlecolab/google-colab-cli

googlecolab
2026.06.09
·GitHub·by Homin.Lee
#Agent#AI#DL#ML#RAG

Key Points

  • 1The Google Colab CLI is a command-line interface that allows users to provision high-performance CPU, GPU, and TPU runtimes, execute local code, and orchestrate automated cloud pipelines directly from their terminal.
  • 2It offers comprehensive features for session management, remote file operations, workspace automation like Google Drive mounting and GCP authentication, and package installation.
  • 3The tool includes an "ephemeral job runner" (`colab run`) for executing local scripts on fresh VMs with automatic teardown, facilitating seamless developer productivity, headless automation, and AI agent integrations.

The Colab CLI is a command-line interface designed to extend Google Colaboratory's capabilities, enabling provisioning of high-performance CPU, GPU, and TPU runtimes, execution of local code, management of remote files, and orchestration of automated cloud pipelines directly from a terminal. It aims to enhance developer productivity, facilitate headless automation, and support AI agent integrations. The CLI currently supports Linux and macOS operating systems.

Its core methodology revolves around a set of distinct commands categorized by function, interacting with the Colab backend to manage computational resources and execute tasks.

Key features and their underlying mechanisms include:

  1. Instant VM Provisioning: The colab new command allocates a new virtual machine instance. Users can specify the desired hardware, including CPU, various GPU types (T4, L4, G4, H100, A100), or TPU types (v5e1, v6e1), via options like --gpu or --tpu. This command initiates the VM lifecycle, making the remote environment available for subsequent operations.
  1. Robust Code Execution:
    • colab exec: This command executes Python code on an active Colab session. It supports input from standard input (stdin), local Python scripts (.py), or Jupyter Notebooks (.ipynb). A key technical detail is its "Transparent Code Execution": when a local file is specified (e.g., colab exec -f file.py), the CLI reads the file's content locally and transmits it to the remote Jupyter kernel for execution, eliminating the need for manual file uploads prior to execution. Outputs can be directed to a specified image path using --output-image.
    • colab repl: Initiates an interactive Python Read-Eval-Print Loop (REPL) on the remote VM, allowing real-time code interaction. It handles piped EOF for clean exit in non-interactive contexts.
    • colab console: Establishes a raw interactive TTY shell (backed by tmux) on the remote VM, providing direct terminal access. Both repl and console require a local TTY for interactive use.
  1. Ephemeral Job Runner (colab run): This command streamlines single-shot script execution on a fresh VM. Upon invocation, colab run [--gpu GPU] [--tpu TPU] SCRIPT [ARGS...] provisions a new VM, uploads and executes the specified local script along with any forwarded arguments, retrieves potential output files, and then automatically tears down the runtime upon completion. This automates the entire lifecycle of a short-lived computational job. It also supports shebang execution, allowing a local script to define the colab run interpreter and its desired hardware (#!/usr/bin/env -S colab run --gpu L4).
  1. Automatic Keep-Alive: A built-in background daemon associated with active sessions prevents idle VM termination, maintaining resource allocation without requiring an open browser tab, addressing a common Colab limitation.
  1. Seamless Workspace Automation:
    • colab drivemount [PATH]: Mounts the user's Google Drive onto the VM filesystem, typically at /content/drive, facilitating data access and persistence.
    • colab auth: Authenticates the VM for Google Cloud Platform (GCP) services (e.g., BigQuery, GCS) through various authentication strategies (oauth2, adc).
    • colab install [-r FILE | PKG...]: Manages package dependencies on the VM. It prioritizes uv for high-performance package management and falls back to pip if uv is unavailable or fails.
  1. State & Log Archival:
    • colab log [-n N] [-o FILE]: Provides functionality to view or export session history. This history can be archived into standard Jupyter Notebooks (.ipynb), Markdown (.md), plain text (.txt), or structured JSONL formats, useful for reproducibility and auditing.
    • Local session states and metadata are stored in ~/.config/colab-cli/sessions.json, and global CLI settings in ~/.config/colab-cli/settings.json, configurable via the global --config flag.

The CLI also includes commands for session management (colab sessions, colab status, colab restart-kernel, colab stop, colab url), file operations (colab ls, colab upload, colab download, colab rm, colab edit which uses the local $EDITOR), and utilities (colab pay, colab version, colab update). Global options like --auth and --client-oauth-config provide control over authentication methods and credentials.