Skip to content

Installing Varnish Cache on Ubuntu

A production-grade Varnish install on Ubuntu — packages, systemd overrides, listener, storage backend, and the operational checks before pointing a domain at it.

A site is buckling under read traffic and the directive is “put Varnish in front of it.” This article is the install we follow in production: get the packages on from the upstream repository, set the listener and the storage backend explicitly via a systemd override, confirm the daemon answers, and leave the box documented enough for the next operator. The goal is not “Varnish is up” — it is “Varnish is up, listening where we expect, with a known cache size and a workable default VCL.”

How to verify

Before installing, check what already binds port 80 and which Varnish (if any) is present from the Ubuntu archive. The distro package is usually behind; we install from the Varnish-Cache project repository so we control the major version.

ss -ltnp | grep -E ':(80|6081|6082)\b'
dpkg -l | grep -E 'varnish|nginx|apache2'
cat /etc/os-release
curl -fsSL https://packagecloud.io/varnishcache/varnish75/gpgkey | gpg --dearmor -o /tmp/key.gpg && file /tmp/key.gpg

If port 80 is occupied, decide now whether Varnish will sit on 80 with the origin moved to 8080, or whether Varnish stays on 6081 behind a separate TLS terminator. Mixing both decisions later is where outages start.

What’s happening

Varnish is a reverse caching HTTP accelerator. A request arrives at the listener; VCL (Varnish Configuration Language) decides whether to serve from cache, fetch from the backend, or pass through. Hot objects live in a storage backend — malloc (in-RAM, the only production-sane choice for most workloads) or file (mmap’d file, useful only on very specific disk-heavy archives). The daemon is varnishd; the worker process holds the cache, and a parent process restarts it if it dies. Configuration changes that require restarting the worker drop the cache, so storage sizing and the listener address are decisions you make once and live with.

Two ports matter: the listener (default 6081, frequently moved to 80) and the management CLI (default 6082, loopback only). The default VCL ships with no backend defined — Varnish will not start until you point it somewhere.

The procedure

  1. Add the upstream apt repository. Pin a major version rather than tracking latest so a routine apt upgrade cannot jump the major version under you.

    curl -fsSL https://packagecloud.io/varnishcache/varnish75/gpgkey \
      | gpg --dearmor -o /usr/share/keyrings/varnishcache_varnish75-archive-keyring.gpg
    echo "deb [signed-by=/usr/share/keyrings/varnishcache_varnish75-archive-keyring.gpg] \
      https://packagecloud.io/varnishcache/varnish75/ubuntu/ $(lsb_release -cs) main" \
      > /etc/apt/sources.list.d/varnishcache_varnish75.list
    apt update && apt install -y varnish
  2. Place the daemon arguments in a systemd drop-in rather than editing the shipped unit file. This survives package upgrades.

    mkdir -p /etc/systemd/system/varnish.service.d
    cat > /etc/systemd/system/varnish.service.d/override.conf <<'EOF'
    [Service]
    ExecStart=
    ExecStart=/usr/sbin/varnishd \
      -j unix,user=vcache \
      -F \
      -a :80 \
      -a localhost:8443,PROXY \
      -T localhost:6082 \
      -f /etc/varnish/default.vcl \
      -S /etc/varnish/secret \
      -s malloc,2g \
      -p workspace_client=128k \
      -p http_resp_hdr_len=32k
    EOF
    systemctl daemon-reload
  3. Replace the stock default.vcl with a minimal but explicit one. The shipped file points at 127.0.0.1:8080 — fine for a smoke test, dangerous if you forget to change it.

    vcl 4.1;
    backend origin {
        .host = "127.0.0.1";
        .port = "8080";
        .probe = { .url = "/healthz"; .interval = 5s; .timeout = 2s; .window = 5; .threshold = 3; }
    }
    sub vcl_recv {
        if (req.method == "PURGE") { return (synth(405)); }
    }
  4. Start the service, confirm the listener, and exercise the cache. The X-Varnish and Age headers tell you whether you are seeing a hit.

    systemctl enable --now varnish
    ss -ltnp | grep varnishd
    curl -I http://127.0.0.1/ | grep -iE 'x-varnish|age|via'
    varnishstat -1 | head -20
  5. Open the firewall narrowly. If TLS terminates upstream (a typical stack — see the HAProxy + Varnish article), only the TLS terminator should reach 6081/80.

    ufw allow from 10.0.0.0/8 to any port 80 proto tcp
    ufw status numbered

Common pitfalls

  • Sizing -s malloc,2g to “whatever sounds reasonable” — measure working-set size from origin logs first; over-sizing forces the kernel into swap, under-sizing wastes the install.
  • Leaving the management CLI (-T) on a public interface; restrict to loopback or a management VLAN with -S secret enforced.
  • Editing /lib/systemd/system/varnish.service directly — your changes vanish on the next package update. Always use a drop-in under /etc/systemd/system/varnish.service.d/.
  • Forgetting that a varnishd restart drops the cache; for VCL-only changes use varnishadm vcl.load and vcl.use instead.
  • Running Varnish on the same box as the origin without cgroup memory limits — a runaway worker can OOM the application.

We run Varnish in front of read-heavy origins for several clients — Drupal, Magento, headless catalogs — and the install is the smallest part of the job. What matters is the VCL, the invalidation strategy, the observability pipeline, and the run-book for what happens when origin goes down with a hot cache. This is the operating model Stack Harbor brings to managed operations engagements: install once, then own the cache forever.