Caching Hugo Builds on a Cheap VPS: Cutting Deploy Time Without More RAM

Hugo is fast — a 500-page site builds in under two seconds on a laptop. On a 1 vCPU / 1 GB VPS with no cache, the same build takes 40–90 seconds because it recompiles every partial, re-processes every image, and re-runs SCSS on each deploy. The build itself is rarely the whole cost; npm install alone can take two minutes on a small box.

This guide covers a cache strategy that cuts repeat deploy time on a small VPS by 70–85% without adding RAM: persistent build caches keyed on content hash, a tmpfs working directory for the intermediate output, and an rsync deploy that transfers only changed files. It works on 1 GB of RAM and is safe to run from cron.

Why Hugo Builds Are Slow on Small Instances

Hugo is a single Go binary and does not have a build cache of its own beyond the resources cache in resources/_gen. The expensive parts on a small VPS are image processing (resizing, WebP conversion), SCSS compilation via Dart Sass or libsass, and Node-based asset pipelines if you use one. Each of these re-does work on every build unless its inputs and outputs are cached.

Memory pressure compounds this. A 1 GB box runs out of page cache quickly, so the OS re-reads every source file from disk on each build, and Hugo’s parallel image processing can spike to 500 MB+ RSS. Capping concurrency and keeping the working set warm are as important as any cache layer.

Persisting the Hugo Resources Cache

Hugo caches transformed images in resources/_gen/images, keyed by a hash of the source image plus processing options. If you build in a clean checkout each time, that directory is empty and every image is reprocessed. The fix is to keep it outside the checkout and point Hugo at it with --cacheDir.

  • Create /var/cache/hugo (or /srv/build/cache) owned by the deploy user.
  • Build with hugo --cacheDir /var/cache/hugo --destination /srv/build/site.
  • Never delete the cache directory in the deploy script.
  • Prune monthly with find /var/cache/hugo -type f -atime +60 -delete to bound growth.

On a 600-page site with 1,400 processed images, this alone took repeat builds from 74 seconds to 19 seconds in measurement — the image pipeline is the dominant cost and the cache eliminates almost all of it. Cache size settles around 400 MB–1.5 GB for sites of that scale, which is why a disk-backed cache is preferable to tmpfs for this specific directory.

Using tmpfs for the Working Tree

The intermediate output directory and Hugo’s scratch space benefit from RAM. Mount a 256 MB tmpfs and build into it, then rsync from tmpfs to the web root. This removes the intermediate write I/O entirely and eliminates the case where a partially written output directory is served mid-build.

Add to /etc/fstab: tmpfs /mnt/hugobuild tmpfs size=256M,mode=0755,uid=deploy 0 0. Keep it at 256 MB — a small Hugo site’s output is typically 30–80 MB, and 512 MB on a 1 GB machine risks consuming memory the build itself needs for image processing. If output exceeds 200 MB, build to disk instead.

Capping Concurrency to Fit 1 GB of RAM

Hugo parallelises image processing across all logical cores. On a 2 vCPU box with 1 GB RAM, two concurrent full-size image resizes can push RSS past 450 MB. Setting --gc and limiting the image processing concurrency with the environment variable HUGO_NUMWORKERMULTIPLIER=1 keeps peak memory around 250 MB and produces more predictable build times.

If builds are still hitting the memory ceiling, add a 512 MB swap file as a safety net — not as a performance strategy. Swap turns an OOM kill into a slow build, which is acceptable for a deploy job that nobody is waiting on synchronously, but you should still fix the underlying concurrency rather than rely on it.

Keeping the Checkout Clean

A fresh clone every build is slow and defeats caches. Use a persistent checkout with git fetch --depth=1 plus git reset --hard origin/main, so unchanged files keep their mtimes and Hugo’s file-change detection works. Keep the repository outside the tmpfs so the checkout survives reboots.

For Node-based asset pipelines, persist node_modules and the npm cache (~/.npm) between builds and run npm ci --prefer-offline. A cold npm ci on a small VPS takes 90–150 seconds; warm, it is 8–15 seconds. This is usually the single largest win after the image cache.

Atomic rsync Deployment

Never build directly into the served web root. Build to /mnt/hugobuild/site, then rsync into a release directory and flip a symlink. The rsync flags that matter are -a --delete --checksum: --checksum ignores timestamps (which differ every build in tmpfs) and transfers only files whose content actually changed — typically 20–80 files out of thousands.

Two directories are all you need: /var/www/releases/ and a current symlink. Keep the last three releases for instant rollback. Because rsync only transfers changed bytes, the deploy step is near-instant even on a 1 vCPU instance, and the symlink flip is atomic from Nginx’s perspective. For a VPS sized for this workload, see the plan comparison on the homepage — a 1 vCPU / 2 GB instance is usually a better fit than 2 vCPU / 1 GB because build memory matters more than core count.

Measured Results and a Cron-Safe Script

ScenarioBuild timeDeploy timePeak RSS
Cold clone, no cache74 s6 s510 MB
+ resources cache19 s6 s480 MB
+ tmpfs output + capped workers17 s0.8 s250 MB
+ warm npm cache9 s0.8 s250 MB

The script wraps the whole sequence in flock /var/lock/hugo.lock so overlapping cron runs cannot collide, and it exits non-zero on hugo failure before touching the symlink. Log to a rotating file, and keep a pre-deploy snapshot of the current symlink target so rollback is a single ln -sfn.

The total steady-state cost is about 1 GB of disk for caches and 250 MB of RAM during builds — comfortably within a small instance. If you are provisioning specifically for this pattern, the providers listed in the comparison table show NVMe capacity and RAM per plan, both of which matter more than CPU for cached Hugo builds.

Affordable-Vps-Server-Author
Affordable-Vps-Server-Author
Articles: 437

Leave a Reply