Configuring Travis CI Dependency Caching in .travis.yml
Learn how to enable dependency caching in Travis CI to reduce build times, including prerequisites, step-by-step configuration, expected checks, and recovery from stale caches.
22 Aug 2025, 04:34 UTC

Rebuilding the same dependency tree on every commit wastes time and network bandwidth. In Travis CI, you can shorten builds by declaring a cache in .travis.yml so the service restores previously installed packages instead of downloading them again. The cache is an optimization, not a correctness mechanism—your build must still pass from a completely cold start.
Desired Outcome
Shorter, more reliable Travis CI builds by restoring dependencies from a cache declared in .travis.yml, while keeping the cache an optimization rather than a correctness dependency. A successful setup reduces install time without hiding failures that only appear when dependencies are fetched fresh.
Prerequisites
- A repository hosted on a Git service supported by Travis CI, with Travis CI enabled for that repository.
- Commit access to
.travis.yml, because all build behavior is declared there. - Access to repository settings in the Travis web interface, or the Travis CLI installed and authenticated, so you can manage caches when recovery is needed.
Procedure
Enable the ecosystem-appropriate cache
Travis CI provides built-in cache declarations for common toolchains. Choose the one that matches your project:
| Ecosystem | Declaration in .travis.yml |
|---|---|
| Ruby (Bundler) | cache: bundler |
| Node.js (npm) | cache: npm |
| Custom paths | cache: directories: followed by a list of paths |
For example, a Ruby project might declare:
language: ruby
cache:
bundler: true
If you need to cache paths that are not covered by a built-in shortcut, list them explicitly:
language: generic
cache:
directories:
- .m2
- vendor/bundle
Where supported, cache keys derive from lockfiles, so editing Gemfile.lock or package-lock.json naturally produces a fresh cache. If your toolchain does not support lockfile-based invalidation, bump a manual cache name or prefix to force a clean rebuild.
Commit and trigger a build
Commit the updated .travis.yml and push. Travis downloads the cache at build start and uploads it only after a successful build. The first run after enabling a cache may not be faster, because the archive is created during that build.
Expected Checks
Inspect the job log. Look for cache download and upload steps. Later builds should reuse the archive rather than reinstalling every dependency.
Compare two consecutive builds. The second build should show a cache restore line and complete faster than the first.
Verify cold-cache behavior. After purging the cache, re-run the build and confirm it still passes. This proves the build does not depend on cached state.
Test lockfile invalidation. Modify the dependency lockfile and confirm the next build reinstalls dependencies instead of restoring the stale archive.
Recovery Options
Purge the cache
If a cache becomes stale or corrupt, delete it and re-run the job.
Where to run: a terminal with the Travis CLI installed and authenticated for the repository.
Command:
travis cache --deleteRepository context: if your shell is not inside the repository, specify the repository slug explicitly (for example,
--repo owner/repo; confirm exact flag names withtravis cache --helpfor your CLI version).Required permissions: write access to the repository.
Expected check: the CLI reports the cache was deleted, and the next build log shows no cache restore step.
Risk: the next build will be slower because dependencies are reinstalled from scratch.
You can also delete the cache from the repository settings in the Travis web interface.
Rename the cache key
If purging does not resolve the issue, change the cache name or prefix in .travis.yml to start with a clean archive. This forces Travis to treat the next upload as a first-time cache and avoids any collision with the old key.
Revert the configuration
If a regression appeared immediately after enabling caching, revert the .travis.yml change and push. Because the file is version-controlled, this is a safe rollback to the previous build behavior.
Limitations and Safety
Service availability. Travis CI's service model has changed over time; the former open-source offering was retired. Confirm current availability and plan limits for your repositories.
Syntax varies. Cache syntax and supported toolchains differ by language and environment. Verify the exact key names for your stack rather than assuming a generic form.
Only successful builds update the cache. A failing build will not refresh a stale archive. Fix the build first, then let a green run re-upload.
Never cache credentials. Caches are shared across builds of the same repository. Store secrets as encrypted environment variables in repository settings instead.
Because Travis CI's platform and CLI flags have evolved, treat the examples above as patterns to verify against current documentation for your account and language image before relying on them in production.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.