Skip to content

sccache - Shared Compilation Cache Cheatsheet

sccache - Shared Compilation Cache Cheatsheet

sccache is a compiler cache (in the spirit of ccache) from Mozilla that supports Rust, C/C++, and NVCC. It wraps your compiler, hashes the inputs, and returns a cached object when it has seen the same compilation before. Its distinguishing feature is shared storage backends — S3, GCS, Redis, Azure — so a whole team and your CI fleet share one cache. That turns “CI recompiles everything from scratch every run” into “CI downloads what it already built.”

Installation

MethodCommand
Cargocargo install sccache --locked
macOS (Homebrew)brew install sccache
Binarydownload from GitHub Releases
Verifysccache --version

Using It with Rust

# One-off
RUSTC_WRAPPER=sccache cargo build

# Persistent (in ~/.cargo/config.toml)
[build]
rustc-wrapper = "sccache"
VariablePurpose
RUSTC_WRAPPER=sccacheRoute rustc through sccache
SCCACHE_DIRLocal cache directory
SCCACHE_CACHE_SIZEMax local cache size (e.g. 20G)

Using It with C/C++

# CMake
cmake -DCMAKE_C_COMPILER_LAUNCHER=sccache \
      -DCMAKE_CXX_COMPILER_LAUNCHER=sccache ..

# Make
make CC="sccache gcc" CXX="sccache g++"

Checking Effectiveness

CommandShows
sccache --show-statsHits, misses, cache size, errors
sccache --zero-statsReset counters before a test build
sccache --stop-serverStop the background server
sccache --start-serverStart it explicitly
# Measure a cold vs warm build
sccache --zero-stats
cargo build --release
sccache --show-stats     # expect misses
cargo clean && cargo build --release
sccache --show-stats     # expect hits

The key number is cache hit rate. A low rate usually means something in the inputs is changing (flags, env vars, absolute paths).

Shared Cloud Backends

# S3
export SCCACHE_BUCKET=my-build-cache
export SCCACHE_REGION=us-east-1
export AWS_ACCESS_KEY_ID=... AWS_SECRET_ACCESS_KEY=...

# Redis
export SCCACHE_REDIS=redis://cache.internal:6379

# GCS
export SCCACHE_GCS_BUCKET=my-build-cache
export SCCACHE_GCS_KEY_PATH=/path/to/key.json
BackendEnv prefix
Local diskSCCACHE_DIR
S3 / S3-compatibleSCCACHE_BUCKET, SCCACHE_ENDPOINT
RedisSCCACHE_REDIS
GCSSCCACHE_GCS_*
AzureSCCACHE_AZURE_*
GitHub ActionsSCCACHE_GHA_ENABLED=true

CI Pattern

# GitHub Actions
env:
  SCCACHE_GHA_ENABLED: "true"
  RUSTC_WRAPPER: "sccache"
steps:
  - uses: mozilla-actions/sccache-action@v0.0.5
  - run: cargo build --release
  - run: sccache --show-stats

Things That Break Caching

CauseFix
Absolute paths in outputUse --remap-path-prefix
Changing RUSTFLAGSKeep flags stable across runs
Incremental compilationDisable it (CARGO_INCREMENTAL=0) for cache use
Proc macros / build scriptsNot always cacheable
Different compiler versionsCache keys include the compiler

sccache vs Alternatives

AspectsccacheccacheBazel/Buck
LanguagesRust, C/C++, CUDAC/C++Many (full build system)
Shared cacheCloud backends built inMostly localRemote cache/execution
SetupWrapper env varWrapperAdopt a new build system
Best forSpeeding up existing buildsC/C++ local cachingLarge monorepos

Pairs well with mold — sccache cuts compile time, mold cuts link time.

Resources