The “Cargo home” functions as a download and source cache.
When building a crate, Cargo stores downloaded build dependencies in the Cargo home.
You can alter the location of the Cargo home by setting the
CARGO_HOME environmental variable.
The home crate provides an API for getting this location if you need this information inside your Rust crate.
By default, the Cargo home is located in
Please note that the internal structure of the Cargo home is not stabilized and may be subject to change at any time.
The Cargo home consists of following components:
config.tomlCargo’s global configuration file, see the config entry in the reference.
credentials.tomlPrivate login credentials from
cargo loginin order to log in to a registry.
.crates2.jsonThese hidden files contain package information of crates installed via
cargo install. Do NOT edit by hand!
binThe bin directory contains executables of crates that were installed via
rustup. To be able to make these binaries accessible, add the path of the directory to your
gitGit sources are stored here:
git/dbWhen a crate depends on a git repository, Cargo clones the repo as a bare repo into this directory and updates it if necessary.
git/checkoutsIf a git source is used, the required commit of the repo is checked out from the bare repo inside
git/dbinto this directory. This provides the compiler with the actual files contained in the repo of the commit specified for that dependency. Multiple checkouts of different commits of the same repo are possible.
registryPackages and metadata of crate registries (such as crates.io) are located here.
registry/indexThe index is a bare git repository which contains the metadata (versions, dependencies etc) of all available crates of a registry.
registry/cacheDownloaded dependencies are stored in the cache. The crates are compressed gzip archives named with a
registry/srcIf a downloaded
.cratearchive is required by a package, it is unpacked into
registry/srcfolder where rustc will find the
Caching the Cargo home in CI
To avoid redownloading all crate dependencies during continuous integration, you can cache the
However, caching the entire directory is often inefficient as it will contain downloaded sources twice.
If we depend on a crate such as
serde 1.0.92 and cache the entire
$CARGO_HOME we would actually cache the sources twice, the
registry/cache and the extracted
.rs files of serde inside
That can unnecessarily slow down the build as downloading, extracting, recompressing and reuploading the cache to the CI servers can take some time.
If you wish to cache binaries installed with
cargo install, you need to cache the
bin/ folder and the
It should be sufficient to cache the following files and directories across builds:
Vendoring all dependencies of a project
cargo vendor subcommand.
Clearing the cache
In theory, you can always remove any part of the cache and Cargo will do its best to restore sources if a crate needs them either by reextracting an archive or checking out a bare repo or by simply redownloading the sources from the web.
Alternatively, the cargo-cache crate provides a simple CLI tool to only clear selected parts of the cache or show sizes of its components in your command-line.