Why Self-Host SearXNG for AI
Agents and AI chat apps need web search to answer questions about anything recent. Hosted search APIs usually bill per query and see every query; your own SearXNG answers in JSON from many engines at once, with no per-query bill. Run it on the same server as your AI chat, n8n workflows or agent and they reach it at http://127.0.0.1:8080.
1. Download the Compose Files
On Ubuntu 24.04, secure the server and install Docker. Then, as in SearXNG's Docker guide:
mkdir ~/searxng && cd ~/searxng curl -fsSL -O https://raw.githubusercontent.com/searxng/searxng/master/container/docker-compose.yml \ -O https://raw.githubusercontent.com/searxng/searxng/master/container/.env.example cp -i .env.example .env
2. Set the Secret and Bind It Locally
echo "SEARXNG_HOST=127.0.0.1" >> .env echo "SEARXNG_SECRET=$(openssl rand -hex 32)" >> .env docker compose up -d docker compose ps # searxng-core and searxng-valkey running
SEARXNG_SECRET sets the server's secret key, which must not stay at its example value. SEARXNG_HOST=127.0.0.1 publishes port 8080 on the server itself only; without it, Docker opens the port to the internet past ufw (see why ufw does not block Docker ports). For a fixed version instead of latest, set SEARXNG_VERSION in the same file.
3. Turn On the JSON API
The first start writes core-config/settings.yml. Edit it so the search section lists JSON among its formats:
search:
formats:
- html
- jsondocker compose restart core curl -s 'http://127.0.0.1:8080/search?q=searxng&format=json' | head -c 400
The API answers on /search (or /) by GET or POST, with the query in q (search API docs). An agent's search tool can be this short:
import requests
def web_search(query: str, n: int = 5):
r = requests.get("http://127.0.0.1:8080/search",
params={"q": query, "format": "json"}, timeout=20)
r.raise_for_status()
return [(x["title"], x["url"]) for x in r.json()["results"][:n]]4. Reach It From Elsewhere
Apps on the same server need nothing more. For your own browser or tools on other servers, either join them over WireGuard or our private network, or put Caddy in front with HTTPS and a password. Point an A record at the server, allow ports 80 and 443, then:
sudo apt install -y caddy
caddy hash-password # type a password, copy the hash
sudo tee /etc/caddy/Caddyfile >/dev/null <<'EOF'
search.example.com {
basic_auth {
you PASTE-THE-HASH-HERE
}
reverse_proxy 127.0.0.1:8080
}
EOF
sudo systemctl reload caddyA public instance is a different job: it needs SearXNG's limiter (limiter: true in the server section, backed by the Valkey container already running) and public_instance: true, as described in the limiter docs.
Keeping It Working
- Engines can push back. Every query goes out from your server's address, and search engines may slow down or challenge an address sending many automated queries. Cache results in your agent and keep the volume reasonable.
- Updates:
docker compose pull && docker compose up -d. Engines change their pages often and SearXNG follows quickly, so update regularly. - Backups: only
core-config/and.envhold anything you wrote.