Deploy an Open-Source AI Gateway on Ubuntu 24.04: A Practical Guide

    This article walks through deploying an open-source AI gateway on an Ubuntu 24.04 server, connecting it to an OpenAI-compatible inference endpoint, and securely accessing its web console over an SSH tunnel. It covers environment preparation, Node.js installation, npm global directory planning, troubleshooting common install errors, interactive provider configuration, connectivity verification, hardening practices, and a quick reference for common issues. The focus is on engineering habits that keep the setup portable across providers and gateway implementations.

    1. Background and Goals: Why Self-Host an AI Gateway

    As large language models mature, more teams want to funnel model calls through a single, controlled entry point instead of scattering call logic and API keys across every application. This need has given rise to the AI gateway layer: a component that sits between business applications and model services and handles unified authentication, routing, audit logging, and usage tracking.

    This article uses an Ubuntu 24.04 server as an example to show how to deploy an open-source AI gateway / coding agent tool, connect it to an OpenAI-compatible inference API from a model provider, and reach its web console safely from a local browser. The approach is vendor-neutral and relies on common industry practices: installing a CLI tool globally, configuring a provider interactively, and reaching an internal port through a reverse tunnel.

    Who this is for and prerequisites:

    • An Ubuntu 24.04 server with at least 2 cores and 4 GB of RAM (inference runs remotely; the local gateway process is lightweight);
    • A working model service API key (often prefixed with sk-, shown only as a shape example; prefixes vary by platform);
    • A local SSH client and browser;
    • It is recommended to install and validate as root first, then create a regular user and run with reduced privileges rather than operating under a high-privilege account long term.

    2. Environment Preparation: Lay the Groundwork

    2.1 System Update and Base Tools

    Sync package sources and install common utilities first to avoid “command not found” distractions later:

    1. apt update && apt upgrade -y
    2. apt install -y curl git build-essential ca-certificates

    2.2 Install the Node.js Runtime

    Most gateway tools in this space are distributed through the Node.js ecosystem. Install a long-term support (LTS) release using the recommended version-management method rather than apt, since distribution repositories often ship older versions that trigger engine-version mismatches:

    1. curl -fsSL https://deb.nodesource.com/setup_lts.x | bash -
    2. apt install -y nodejs
    3. node -v && npm -v

    2.3 Plan the npm Global Directory

    The default global directory can cause permission confusion and leftover clutter in some environments. Explicitly set a dedicated prefix:

    1. mkdir -p /root/.npm-global
    2. npm config set prefix '/root/.npm-global'
    3. export PATH=/root/.npm-global/bin:$PATH
    4. echo 'export PATH=/root/.npm-global/bin:$PATH' >> ~/.bashrc

    The value here is clarity: when you later uninstall, reinstall, or investigate leftovers, the path is unambiguous, and mixing multiple Node versions won’t cause module directories to overwrite each other.

    3. Install the Open-Source AI Gateway Tool

    Install the latest version globally with npm:

    1. npm install -g openclaw@latest

    Verify immediately after installation:

    1. openclaw --version # expected output looks like a version number such as 2026.3.2

    3.1 Common Install Failures and How to Handle Them

    Failure 1: ENOTEMPTY (directory not empty). A module directory left over from a previous install wasn’t fully cleaned, so npm fails while overwriting. Locate and remove the leftovers, clear the cache, then reinstall:

    1. rm -rf /root/.npm-global/lib/node_modules/openclaw
    2. npm cache clean --force
    3. npm install -g openclaw@latest

    Failure 2: Many deprecated warnings. These come from upstream packages in the dependency tree being marked deprecated and do not mean the install failed. Judge success by the exit code and whether --version prints correctly, not by the number of warnings.

    Failure 3: Permission errors (EACCES). Don’t take the shortcut of running everything with sudo. Fix ownership of the global directory instead, or install under the user directory planned in section 2.3.

    Failure 4: Network timeouts. Switch to a mirror and retry. Note that mirrors can lag, so fall back to the official source if necessary.

    4. Connect a Large Model Service: Interactive Provider Configuration

    The gateway does not provide inference itself; it needs to point at a model service endpoint. The mainstream approach is to choose an API that is compatible with the OpenAI Chat Completions protocol, which means client-side code and ecosystem tools need almost no changes.

    Start the interactive wizard:

    1. openclaw config

    Follow the prompts to complete these steps:

    1. Choose “Add custom provider” to manually declare a third-party inference endpoint;
    2. Enter a provider identifier, for example zhipu; this name is only a local routing key and can be anything you like;
    3. Enter the API Base URL, using the v4-compatible endpoint given in the provider’s documentation (shaped like https://<provider-domain>/api/paas/v4);
    4. Paste the API key;
    5. Specify the default model name, using the platform’s current model identifier string;
    6. Save and let the wizard write the configuration file.

    4.1 What the Configuration Items Mean

    • Base URL: the gateway appends standard paths such as /chat/completions to this address, so follow the service documentation exactly; adding or dropping a path segment leads to 404;
    • Model identifier: this is a service-side string that changes as the platform evolves, so re-check the documentation after upgrades;
    • Key storage: the wizard usually writes to a user-level configuration file (permissions should be 600); in production, injecting from environment variables is preferred so keys don’t end up in version control.

    4.2 Verify Connectivity

    Confirm the link with a minimal request before handing off to the gateway:

    1. curl https://<provider-domain>/api/paas/v4/chat/completions \
    2. -H "Authorization: Bearer $API_KEY" \
    3. -H "Content-Type: application/json" \
    4. -d '{"model":"<model-id>","messages":[{"role":"user","content":"ping"}]}'

    A normal completion response means the key, address, and model name all match correctly.

    5. Secure Access to the Web Console: SSH Port Forwarding

    The gateway listens on a port on the local loopback address by default (such as 3000). Do not expose it to the public internet for convenience; once the console is scanned, the keys and call quota are effectively exposed. Set up an encrypted tunnel instead:

    1. ssh -L 3000:127.0.0.1:3000 root@<server-address> -N

    Then open http://127.0.0.1:3000 in your local browser. For long-running background use, write it into a config snippet:

    1. # ~/.ssh/config
    2. Host ai-gateway
    3. HostName <server-address>
    4. User root
    5. LocalForward 3000 127.0.0.1:3000
    6. ServerAliveInterval 30

    Hardening essentials:

    • Open only port 22 on the firewall; do not expose the gateway port to the public internet;
    • Prefer key-based login and disable password authentication;
    • Create a regular user and run the gateway with reduced privileges as a systemd service;
    • Set a separate access password for the console and keep key file permissions at 600;
    • Combine log services and monitoring alerts to trigger threshold warnings on abnormal call volume.

    6. Next Steps: From “Working” to “Governed”

    Once this structure becomes part of daily team use, the focus shifts from installation to governance. Four general capabilities can be added gradually: first, multi-provider configuration with automatic failover to avoid business disruption from a single endpoint’s instability; second, call-chain logging and usage statistics to support cost allocation; third, rate limiting and quotas to prevent a single application from exhausting the budget; fourth, a key rotation mechanism that replaces keys periodically and keeps audit records. If you need elastic scaling and high availability, containerize the gateway and let a container platform orchestrate it, placing configuration and keys in configuration-management and key-management services rather than hardcoding them into the image.

    7. Quick Reference for Common Issues

    • Port already in use: run ss -lntp | grep 3000 to locate the process, or switch to another port and adjust the tunnel parameters accordingly;
    • Configuration not taking effect: confirm the wizard wrote to the config under the current user’s home directory; root and regular users cannot see each other’s;
    • 401 on calls: the key is wrong or expired; 404: the Base URL path segment is incorrect; model not found: the model identifier is misspelled or has been retired;
    • Issues after upgrade: back up the configuration file first, then reinstall and check fields against the new version’s documentation.

    8. Summary

    Completing this deployment on Ubuntu 24.04 depends less on any single install command and more on four engineering habits: explicitly managing the runtime and global directory, connecting to model services through a standard protocol, keeping the console internal and reaching it over an encrypted tunnel, and designing accounts and keys around least privilege from the start. With this methodology in hand, switching the underlying model provider or gateway implementation won’t require rebuilding the whole architecture.