cargo-fetch
Download dependencies without building a project
TLDR
Fetch dependencies specified in Cargo.lock (for all targets)
Fetch dependencies for the specified target
SYNOPSIS
cargo fetch [OPTIONS]
PARAMETERS
--all
Fetch all dependencies for all packages in the workspace.
--target
Fetch dependencies only for the specified target triple (e.g., x86_64-unknown-linux-gnu).
--jobs
Number of parallel jobs to run.
--workspace
Fetch dependencies for the current workspace. This is the default if no package is specified.
-p
Fetch dependencies for a specific package in the workspace.
--manifest-path
Path to the Cargo.toml file for the package.
--frozen
Require Cargo.lock to be up-to-date. If not, Cargo will exit with an error.
--locked
Require Cargo.lock to be present and up-to-date. If not, Cargo will exit with an error. Unlike --frozen, this implies Cargo.lock must exist.
--offline
Operate in offline mode, using only cached dependencies. No network requests will be made.
-v, --verbose
Use verbose output, showing more details about the fetching process.
-q, --quiet
Do not print cargo log messages, only errors.
--color
Control color output (`auto`, `always`, `never`).
--features
Space or comma separated list of features to activate for the current package.
--all-features
Activate all available features of the current package.
--no-default-features
Do not activate the `default` feature of the current package.
--ignore-rust-version
Ignore rust-version specification in packages. Useful for older toolchains.
--registry
Name of the registry to use (e.g., crates-io).
--index
URL of the registry index to use.
--profile
Fetch dependencies for the specified profile (e.g., dev, release). This influences conditional dependencies.
DESCRIPTION
cargo fetch is a subcommand of the Rust package manager, cargo. Its primary function is to download all direct and transitive dependencies required by the current Rust project (or specified packages) into the local Cargo registry cache. Unlike cargo build or cargo run, cargo fetch does not compile any code; it solely fetches the source code of the dependencies.
This command is particularly useful for preparing a project for offline development, ensuring that all necessary crates are available locally without an internet connection, or for pre-populating build environments in CI/CD pipelines. It respects the Cargo.lock file, meaning it will download the exact versions of dependencies recorded there, ensuring reproducible builds. If no Cargo.lock exists or --locked is not specified, it may perform an update to determine the latest compatible versions before fetching. The downloaded crates are stored in Cargo's global cache, typically located in ~/.cargo/registry/src, making them accessible to multiple projects.
CAVEATS
- Disk Space: Fetching numerous or large dependencies can consume significant disk space in the Cargo cache over time.
- Network Requirement: Requires an active internet connection to download dependencies if they are not already cached.
- Cargo.lock Importance: While cargo fetch uses Cargo.lock, running it without --locked might trigger an update of the Cargo.lock file, potentially fetching newer dependency versions than intended if Cargo.toml allows it. Use --locked for strict reproducibility.
- No Compilation: This command only downloads; it does not compile or build the fetched crates. Subsequent commands like cargo build or cargo run are still needed for compilation and execution.
CACHE LOCATION
Dependencies fetched by cargo fetch are stored in Cargo's global cache, typically located at ~/.cargo/registry/src/ on Linux. This cache is shared across all Rust projects on the system, avoiding redundant downloads and saving disk space.
OFFLINE DEVELOPMENT WORKFLOW
After running cargo fetch, you can work on your project and perform builds (cargo build) or runs (cargo run) without an internet connection, provided all necessary dependencies were successfully fetched beforehand. For strict offline operation during subsequent commands, you can combine them with the --offline flag (e.g., cargo build --offline).
HISTORY
The cargo fetch command has been an integral part of the Rust cargo build system since its early development stages. As cargo evolved into a robust package manager, the need for efficient dependency management, including pre-fetching for offline use or CI/CD pipelines, made fetch a foundational component. Its design aligns with cargo's philosophy of reproducible builds, heavily relying on the Cargo.lock file to ensure consistent dependency versions across environments. It has seen continuous refinement alongside the broader cargo and Rust ecosystem, with new options added to cater to specific use cases.
SEE ALSO
cargo(1), cargo-build(1), cargo-update(1), cargo-clean(1), rustup(1)