Introduction
Travis CI caching speeds up builds by storing dependencies, build artifacts, and other files between builds. When configured correctly, caches persist across builds for the same branch, avoiding repeated dependency downloads and compilations. However, cache restoration can fail when configuration is incorrect, cache keys don't match, or storage limits are exceeded.
Cache configuration in Travis CI uses the .travis.yml file to specify directories to cache and caching strategy. The cache key defaults to the branch name, meaning different branches have separate caches by default. Advanced configurations can use custom cache keys, conditional caching, or multiple cache directories.
When cache restoration fails, builds take longer, consume more resources, and may fail if dependency installation times out. Understanding Travis CI's caching mechanism—including key generation, storage limits, and directory handling—is essential for optimizing build performance.
Symptoms
When Travis CI cache is not restoring, you will observe these symptoms:
- Build logs show "cache not found" or "restoring cache" but directory remains empty
- Dependencies download from scratch instead of using cached versions
- Build times increase significantly compared to previous builds
- Cache upload shows as successful but next build doesn't restore
- Branch-specific caches don't work after branch rename
- Cache appears in Travis UI but isn't restored to build environment
Travis CI log output showing cache miss:
``` cache.2 store build cache
checking for cache... cache does not exist, skipping download
install.1 Installing dependencies... # Full download instead of cache hit ```
Expected cache hit output:
checking for cache...
found cache
downloading archive...
unpacking archive...
cache restoredTravis UI showing cache but build doesn't use it:
``` Caches - branch: main (512 MB, 2 days ago) - branch: develop (128 MB, 1 day ago)
Current build (main branch): "cache not found" ```
Common Causes
Several factors cause Travis CI cache restoration failures:
- 1.Cache directory path incorrect: The
cache.directoriespaths don't match where dependencies are actually stored. Travis caches only what's explicitly specified. - 2.Branch name change: Travis CI keys caches by branch by default. Renaming a branch creates a new, empty cache.
- 3.Cache key mismatch: Custom cache keys (
cache.key) don't match between builds due to environment variable changes or key configuration errors. - 4.Cache size limit exceeded: Travis CI has cache size limits (typically 1GB per repository). Oversized caches may be truncated or rejected.
- 5.Cache disabled in configuration:
cache: falseor missing cache configuration prevents caching entirely. - 6.Different language configuration: Language-specific default caching may not apply if language isn't detected or is incorrectly specified.
- 7.Cache storage backend issues: Rare issues with Travis CI's cache storage can prevent restoration.
- 8.Job matrix splits: Different matrix jobs have separate caches by default, which may not share as expected.
Step-by-Step Fix
Follow these steps to diagnose and resolve Travis CI cache restoration issues:
Step 1: Check Travis CI cache configuration
Review the .travis.yml cache settings:
```bash # View current cache configuration cat .travis.yml | grep -A 10 "cache:"
# Common configurations: cache: directories: - node_modules - $HOME/.cache/pip - $HOME/.m2/repository ```
Example configurations:
```yaml # Basic caching cache: npm
# Directory caching cache: directories: - node_modules - vendor/bundle
# With custom key cache: directories: - node_modules key: cache-${TRAVIS_BRANCH}-${TRAVIS_COMMIT}
# Conditional caching cache: directories: - node_modules edge: true # Use edge caching ```
Step 2: Verify cache exists in Travis
Check cache status in Travis CI:
```bash # Via Travis CLI travis cache
# Output: main: 512 MB (2 days ago) develop: 128 MB (1 day ago) feature-branch: 64 MB (1 hour ago)
# Check specific branch cache travis cache --branch main
# Or via Travis API curl -H "Authorization: token $TRAVIS_TOKEN" \ https://api.travis-ci.com/repo/owner%2Frepo/caches ```
Step 3: Check cache directory paths
Verify cached directories match actual locations:
```yaml # For Node.js cache: directories: - node_modules # Default location # NOT: ./node_modules (relative path issues)
# For Python/pip cache: directories: - $HOME/.cache/pip # pip cache location - $HOME/.local/lib/python*/site-packages
# For Ruby/bundler cache: directories: - vendor/bundle # When using --path vendor/bundle
# For Java/Maven cache: directories: - $HOME/.m2/repository
# For Java/Gradle cache: directories: - $HOME/.gradle/caches - $HOME/.gradle/wrapper ```
Step 4: Debug cache paths in build
Add debug output to verify paths:
script:
- echo "Checking cache directories..."
- ls -la node_modules/ || echo "node_modules not found"
- ls -la $HOME/.cache/pip/ || echo "pip cache not found"
- echo "HOME is $HOME"
- echo "PWD is $PWD"Step 5: Fix cache key configuration
Use correct cache keys:
```yaml # Branch-based key (default behavior) cache: directories: - node_modules # Key defaults to branch name
# Commit-based key (invalidates on each commit) cache: directories: - node_modules key: $TRAVIS_COMMIT
# Dependency file hash (recommended) cache: directories: - node_modules key: - package-lock.json # Or explicit hash - v1-dependencies-$( checksum package-lock.json )
# Multiple keys for fallback cache: directories: - node_modules key: - v2-deps-$( checksum package-lock.json ) - v1-deps-$( checksum package-lock.json ) - v1-deps- ```
Step 6: Clear corrupted cache
Reset cache if corrupted:
```bash # Via Travis CLI travis cache --delete
# Delete specific branch cache travis cache --delete --branch main
# Via API curl -X DELETE \ -H "Authorization: token $TRAVIS_TOKEN" \ https://api.travis-ci.com/repo/owner%2Frepo/caches
# After deletion, next build creates fresh cache ```
Step 7: Configure language-specific caching
Use language-appropriate caching:
```yaml # Node.js - automatic npm caching language: node_js node_js: - 18 cache: npm: true # or just 'cache: npm'
# Python - pip caching language: python python: - 3.11 cache: pip: true directories: - $HOME/.cache/pip
# Ruby - bundler caching language: ruby rvm: - 3.2 cache: bundler: true
# Go - module caching language: go go: - 1.21 cache: directories: - $GOPATH/pkg/mod - $HOME/go/pkg/mod
# PHP - composer caching language: php php: - 8.2 cache: directories: - $HOME/.composer/cache ```
Step 8: Handle matrix builds correctly
Configure caching for matrix builds:
```yaml # Matrix builds have separate caches by default matrix: include: - node_js: 16 - node_js: 18
# All matrix jobs share cache from same branch # To share across matrix, use explicit key: cache: directories: - node_modules key: shared-deps-${ checksum package-lock.json } ```
Step 9: Verify cache restoration
Add verification step:
```yaml install: - npm install
script: - echo "Verifying cache restoration..." - | if [ -d "node_modules" ]; then echo "node_modules exists - cache restored" ls node_modules | wc -l else echo "node_modules missing - cache not restored" fi - npm test ```
Verification
After fixing cache configuration, verify restoration works:
```yaml # Add cache verification to .travis.yml script: - | echo "Cache verification:" echo "node_modules files: $(ls node_modules 2>/dev/null | wc -l)" echo "Expected: > 100 packages" - npm test
after_cache: - echo "Cache saved with: $(du -sh node_modules | cut -f1)" ```
Trigger a build and check logs:
```bash # Trigger build travis trigger
# Watch build logs travis logs
# Check for cache hit in logs: # "found cache" # "unpacking archive" # "cache restored" ```
Compare build times:
Before cache fix: 5 minutes (full dependency install)
After cache fix: 1.5 minutes (cache restored)Prevention
To prevent Travis CI cache issues:
- 1.Use dependency file checksums in cache keys: Invalidate cache only when dependencies change.
cache:
directories:
- node_modules
key:
- v1-deps-$( checksum package-lock.json )- 1.Document cache configuration: Keep records of cached paths.
```markdown ## Travis CI Cache - node_modules: npm dependencies - $HOME/.cache/pip: pip packages - vendor/bundle: bundler gems
Cache key: package-lock.json checksum Expected size: ~500MB ```
- 1.Monitor cache size: Keep caches under limits.
after_cache:
- echo "Cache size: $(du -sh node_modules)"
- |
SIZE=$(du -sm node_modules | cut -f1)
if [ $SIZE -gt 800 ]; then
echo "Warning: Cache size approaching limit"
fi- 1.Use explicit paths: Avoid relative path confusion.
cache:
directories:
- $TRAVIS_BUILD_DIR/node_modules- 1.Test cache after configuration changes: Verify cache works after any
.travis.ymlmodifications. - 2.Use before_cache for cleanup: Remove unnecessary files before caching.
before_cache:
- rm -rf node_modules/.cache # Remove temp files
- rm -rf $HOME/.cache/pip/log # Remove logs- 1.Consider cache timeout: Travis may skip slow cache operations.
cache:
timeout: 1000 # Increase timeout to 1000 secondsRelated Articles
- [Technical troubleshooting: Fix Cicd Artifact Upload Failed Storage Issue in C](cicd-artifact-upload-failed-storage)
- [Technical troubleshooting: Fix Cicd Code Quality Gate Failed Sonarqube Issue ](cicd-code-quality-gate-failed-sonarqube)
- [Technical troubleshooting: Fix Cicd Deployment Failed Health Check Issue in C](cicd-deployment-failed-health-check)
- [Technical troubleshooting: Fix Cicd Github Actions Workflow Queue Timeout in ](cicd-github-actions-workflow-queue-timeout)
- [Technical troubleshooting: Fix Cicd Gitlab Runner Stuck Pending Issue in CI/C](cicd-gitlab-runner-stuck-pending)
<script type="application/ld+json"> { "@context": "https://schema.org", "@type": "TechArticle", "headline": "Travis CI Cache Not Restoring", "description": "Travis CI cache not restored when cache directory or key mismatch.", "url": "https://www.fixwikihub.com/fix-cicd-travis-ci-cache-not-restoring", "publisher": { "@type": "Organization", "name": "FixWikiHub", "url": "https://www.fixwikihub.com" }, "author": { "@type": "Person", "name": "FixWikiHub Editorial Team" }, "datePublished": "2026-01-22T17:45:26.220Z", "dateModified": "2026-01-22T17:45:26.220Z" } </script>