If you run several AI tools and each one talks to a different model provider, your code ends up full of provider-specific clients, API keys, and quirks. The fix is to self-host litellm linux as a single gateway: LiteLLM exposes one OpenAI-compatible API endpoint, and behind it you can route to OpenAI, Anthropic, a local Ollama model, or any other supported provider without changing your application code. One endpoint, one key format, and one place to track spending.

This guide shows you how to self-host LiteLLM Linux with Docker Compose, configure a model list that mixes a hosted provider with a local Ollama model, and test the gateway with curl. The Docker Compose path is the recommended route because it isolates dependencies and makes upgrades a one-line operation. If you prefer a plain Python install, the pip route is covered briefly at the end of the installation section. You should be comfortable with the Linux command line and have Docker already installed; if you are new to self-hosting language models, start with our guide to self-hosting an LLM with Ollama on Ubuntu first.
Prerequisites
Before you begin, make sure you have:
- A Linux server running Ubuntu 22.04 or 24.04, Debian 12, or a similar distribution, with at least 2 GB of RAM and 10 GB of free disk space.
- Docker Engine and the Docker Compose plugin installed. Verify with
docker --versionanddocker compose version. - An API key for at least one hosted provider, such as OpenAI, if you want to route to a hosted model. A local-only setup with Ollama works without any hosted key.
- A firewall rule or security group opening TCP port 4000 only to the machines that should call the gateway. LiteLLM is an API server, not a public website, so never expose it to the whole internet without authentication.
- Basic familiarity with YAML, since the model list lives in a configuration file.
A self-host LiteLLM Linux gateway is lightweight: it is a Python application that proxies requests, so it needs little CPU or memory. The heavy lifting happens at the model providers. If you run Ollama on the same server, size the server for the model you plan to serve, not for LiteLLM.
How to Self-Host LiteLLM Linux with Docker Compose
Start by creating a project directory and a configuration file. The configuration file is the heart of your self-host LiteLLM Linux gateway: the model_list section maps a friendly name that your applications use to the real provider model behind it. This example defines two models: a hosted OpenAI model and a local Ollama model. Note how the API key for OpenAI is read from an environment variable with os.environ/, which keeps secrets out of the configuration file itself.
mkdir -p ~/litellm && cd ~/litellm
cat > config.yaml <<'EOF'
model_list:
- model_name: gpt-4o-mini
litellm_params:
model: openai/gpt-4o-mini
api_key: os.environ/OPENAI_API_KEY
- model_name: local-llama
litellm_params:
model: ollama/llama3.1
api_base: http://host.docker.internal:11434
general_settings:
master_key: os.environ/LITELLM_MASTER_KEY
EOF
A few details in this file matter for a self-host LiteLLM Linux setup. The model_name is the name your applications send in their requests; it can be anything you like. The litellm_params.model value tells LiteLLM which provider and model to call, using the provider/model format. The api_base for Ollama points at host.docker.internal, which resolves to the host machine from inside the container, so LiteLLM can reach an Ollama server running on the same host. If Ollama runs on a different machine, replace it with that machine’s address.
Next, create the Compose file for your self-host LiteLLM Linux gateway. It runs the official LiteLLM image, maps port 4000, mounts the configuration file, and passes the two secrets as environment variables. Keep the secrets in a .env file in the same directory rather than typing them into the Compose file.
cat > docker-compose.yml <<'EOF'
services:
litellm:
image: ghcr.io/berriai/litellm:main-latest
container_name: litellm
ports:
- "4000:4000"
volumes:
- ./config.yaml:/app/config.yaml
env_file:
- .env
command: --config /app/config.yaml --port 4000 --detailed_debug
restart: unless-stopped
EOF
cat > .env <<'EOF'
OPENAI_API_KEY=sk-your-openai-key-here
LITELLM_MASTER_KEY=sk-your-master-key-here
EOF
chmod 600 .env
docker compose up -d
docker compose logs -f litellm
Wait for the log line indicating the server is running on port 4000, then press Ctrl+C to stop following the logs. The container keeps running in the background. If you would rather self-host LiteLLM Linux without Docker, the alternative is a Python virtual environment: create one with python3 -m venv venv, activate it, run pip install 'litellm[proxy]', and start the server with litellm --config config.yaml --port 4000. The configuration file and the API are identical either way, so you can switch approaches later without changing your applications.
Testing Your Self-Host LiteLLM Linux Gateway
The fastest way to confirm your self-host LiteLLM Linux gateway works is a curl request to the OpenAI-compatible chat completions endpoint. Any tool that speaks the OpenAI API format can use this endpoint by changing only its base URL and key, which is exactly why the gateway is useful.
curl http://localhost:4000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-your-master-key-here" \
-d '{
"model": "gpt-4o-mini",
"messages": [
{"role": "user", "content": "Reply with the word OK."}
]
}'
A successful response returns a JSON object with a choices array containing the model’s reply. Now test the local model the same way, changing only the model field to local-llama. If both calls succeed, your gateway is routing correctly, and your applications can switch providers by changing a single string. To confirm the server itself is healthy without spending tokens, query curl http://localhost:4000/health, which returns the status of the configured models.
For a full application example, point an OpenAI client library at your self-host LiteLLM Linux gateway by setting its base URL to http://your-server:4000 and its API key to your master key. Pairing the gateway with a web interface is a natural next step; see our guide to self-hosting Open WebUI with Docker on Linux for a chat front end that can talk to any OpenAI-compatible endpoint, including this one.
Virtual Keys and Spend Tracking Basics
Handing out your master key to every application and teammate is a bad idea: it cannot be scoped, and it cannot be revoked per user. Virtual keys in a self-host LiteLLM Linux setup solve this. A virtual key is a separate key tied to a budget, an expiry date, or a set of allowed models. You create one by calling the key generation endpoint with the master key:
curl http://localhost:4000/key/generate \
-H "Authorization: Bearer sk-your-master-key-here" \
-H "Content-Type: application/json" \
-d '{
"models": ["gpt-4o-mini"],
"max_budget": 10.0,
"key_alias": "dev-team-key"
}'
The response contains the new key. Applications use it exactly like the master key, but spending against it stops at the budget you set, and you can delete it at any time without affecting other users. The models field restricts the key to the listed models, which is useful when you want a team to use the cheaper model only.
Spend tracking is built into a self-host LiteLLM Linux gateway. Open the LiteLLM UI at http://your-server:4000/ui and sign in with the master key to see usage by key, user, and model, along with per-request logs. For programmatic access, the /key/info endpoint returns a key’s spend, budget, and expiry. Check spend weekly at first: a misconfigured loop in an application can burn through a hosted provider budget quickly, and the gateway’s budget limits are the safety net that catches it.
One operational note: virtual keys, spend data, and the UI’s user records are stored in a database. By default LiteLLM uses an in-memory store that does not survive container restarts. For anything beyond testing your self-host LiteLLM Linux gateway, add a PostgreSQL or other supported database to your Compose file and set the DATABASE_URL environment variable, as described in the official LiteLLM proxy documentation. Otherwise your keys and history will vanish the next time the container is recreated.
Troubleshooting Your Self-Host LiteLLM Linux Setup
Model not found
If your self-host LiteLLM Linux gateway replies that a model was not found, the model_name in your request does not match any entry in config.yaml. Remember that the request uses model_name, not the provider model string. Check for typos, confirm the configuration file is mounted at the path in the Compose file, and restart the container after editing it with docker compose restart litellm.
API key errors
Authentication failures usually trace back to environment variables. If the provider reports an invalid key, check that .env is in the same directory as the Compose file and that the variable name matches the os.environ/ reference in config.yaml exactly. Note that variables referenced this way are read when LiteLLM starts, so changing .env requires a container restart. Also confirm you are sending the master key or a valid virtual key in the Authorization header, not the provider’s own key, when calling the gateway.
Port conflicts and Ollama connection failures
If the container exits immediately with a port binding error, something else already listens on port 4000. Find it with sudo ss -tlnp | grep 4000 and either stop that service or change the published port in the Compose file. If the hosted model works but the local Ollama model fails, verify Ollama is actually running with curl http://localhost:11434/api/tags on the host, and check that the api_base in config.yaml is reachable from inside the container. A common mistake is pointing api_base at localhost, which from inside the container means the container itself, not the host.
Conclusion
Self-hosting LiteLLM turns a tangle of provider APIs into one stable interface for your applications. Define your models once in config.yaml, run the gateway with Docker Compose, issue scoped virtual keys instead of sharing a master key, and watch spending from the built-in UI. When you want to add a provider, you edit one file instead of rewriting application code. For the full list of supported providers, configuration options, and database setup, consult the official LiteLLM documentation. With the gateway in place, experimenting with a new model is a one-line config change, and moving a workload from a hosted model to a local one is a one-word change in your requests.