CARGO-ADD(1)
CARGO-ADD(1) π
okay bestie, welcome to the General Commands Manual section for cargo-add β yes it's a real thing and yes it's about to become your whole personality β¨
πΈ NAME πΈ
cargo-add β the little command that adds dependencies to a Cargo.toml manifest file. that's it. that's the job. and it EATS π
π SYNOPSIS π
cargo add [options] crateβ¦
cargo add [options] --path path
cargo add [options] --git url [crateβ¦]
like. three whole moods. pick one π¦
β¨ DESCRIPTION β¨
so this command adds or modifies dependencies. that's the vibe. you want a crate in your project? boom. it's in the manifest.
the source for the dependency can be specified with:
crate@version: fetch from a registry with a version constraint of "version"--path path: fetch from the specified path--git url: pull from a git repo aturl
and if you don't specify a source? don't stress, bestie β it'll make a best effort to pick one for you, including:
- existing dependencies in other tables (like
dev-dependencies) - workspace members
- latest release in the registry
when you add a package that's already present, the existing entry gets updated with the flags you specified. no duplicates, no drama, we love a clean update π
and upon successful invocation, the enabled (+) and disabled (-) features β over at <https://doc.rust-lang.org/cargo/reference/features.html> β of the dependency you named will be listed right there in the command's output. so you can see exactly what you got. iconic.
π OPTIONS π
π Source options π
--git urlβ the git URL to add the specified crate from. (details: <https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#specifying-dependencies-from-git-repositories>)--branch branchβ which branch to use when adding from git.--tag tagβ which tag to use when adding from git.--rev shaβ the specific commit to use when adding from git.--path pathβ filesystem path to the local crate you wanna add. (<https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#specifying-path-dependencies>)--registry registryβ name of the registry to use. registry names are defined in Cargo config files (<https://doc.rust-lang.org/cargo/reference/config.html>). if you don't specify one, the default registry gets used, which is defined by theregistry.defaultconfig key β and that defaults tocrates-io.
π Section options π
--devβ add as a development dependency. (<https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#development-dependencies>)--buildβ add as a build dependency. (<https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#build-dependencies>)--target targetβ add as a dependency to the given target platform. (<https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#platform-specific-dependencies>)
to avoid unexpected shell expansions, you may use quotes around each target, e.g., --target 'cfg(unix)'. yes bestie, the quotes are load-bearing π
π¦ Dependency options π¦
--dry-runβ don't actually write the manifest. just vibes. a lil preview.--rename nameβ rename the dependency. (<https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#renaming-dependencies-in-cargotoml>)--optionalβ mark the dependency as optional. (<https://doc.rust-lang.org/cargo/reference/features.html#optional-dependencies>)--no-optionalβ mark the dependency as required. (<https://doc.rust-lang.org/cargo/reference/features.html#optional-dependencies>)--no-default-featuresβ disable the default features. (<https://doc.rust-lang.org/cargo/reference/features.html#dependency-features>)--default-featuresβ re-enable the default features. (<https://doc.rust-lang.org/cargo/reference/features.html#dependency-features>)-F features,--features featuresβ space or comma separated list of features to activate. (<https://doc.rust-lang.org/cargo/reference/features.html#dependency-features>) when you're adding multiple crates, the features for a specific crate may be enabled withpackage-name/feature-namesyntax. this flag may be specified multiple times, which enables all specified features. stack 'em, queen π--ignore-rust-versionβ ignorerust-versionspecification in packages.
β οΈ this option is unstable and available only on the nightly channel (<https://doc.rust-lang.org/book/appendix-07-nightly-rust.html>) and requires the -Z unstable-options flag to enable. see <https://github.com/rust-lang/cargo/issues/5579> for more information. so like, don't come crying to me if it yells at you on stable π«Ά
π Display Options π
-v,--verboseβ use verbose output. may be specified twice for "very verbose" output which includes extra output such as dependency warnings and build script output. may also be specified with theterm.verboseconfig value. (<https://doc.rust-lang.org/cargo/reference/config.html>)-q,--quietβ do not print cargo log messages. may also be specified with theterm.quietconfig value. (<https://doc.rust-lang.org/cargo/reference/config.html>)--color whenβ control when colored output is used. valid values:auto(default): automatically detect if color support is available on the terminal.always: always display colors.never: never display colors.
may also be specified with the term.color config value. (<https://doc.rust-lang.org/cargo/reference/config.html>)
π Manifest Options π
--manifest-path pathβ path to theCargo.tomlfile. by default, Cargo searches for theCargo.tomlfile in the current directory or any parent directory. it'll go hunting π-p spec,--package specβ add dependencies to only the specified package.--frozen,--lockedβ either of these flags requires that theCargo.lockfile is up-to-date. if the lock file is missing, or it needs to be updated, Cargo will exit with an error. the--frozenflag also prevents Cargo from attempting to access the network to determine if it is out-of-date.
these may be used in environments where you want to assert that the Cargo.lock file is up-to-date (such as a CI build) or want to avoid network access.
--offlineβ prevents Cargo from accessing the network for any reason. without this flag, Cargo will stop with an error if it needs to access the network and the network is not available. with this flag, Cargo will attempt to proceed without the network if possible.
β οΈ beware that this may result in different dependency resolution than online mode. Cargo will restrict itself to crates that are downloaded locally, even if there might be a newer version as indicated in the local copy of the index. see the cargo-fetch(1) command to download dependencies before going offline. fr fr, prep first.
may also be specified with the net.offline config value. (<https://doc.rust-lang.org/cargo/reference/config.html>)
β Common Options β
+toolchainβ if Cargo has been installed with rustup, and the first argument tocargobegins with+, it will be interpreted as a rustup toolchain name (such as+stableor+nightly). see the rustup documentation (<https://rust-lang.github.io/rustup/overrides.html>) for more information about how toolchain overrides work.--config KEY=VALUE or PATHβ overrides a Cargo configuration value. the argument should be in TOML syntax ofKEY=VALUE, or provided as a path to an extra configuration file. this flag may be specified multiple times. see the command-line overrides section (<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>) for more information.-C PATHβ changes the current working directory before executing any specified operations. this affects things like where cargo looks by default for the project manifest (Cargo.toml), as well as the directories searched for discovering.cargo/config.toml, for example. this option must appear before the command name, for examplecargo -C path/to/my-project build.
β οΈ this option is only available on the nightly channel (<https://doc.rust-lang.org/book/appendix-07-nightly-rust.html>) and requires the -Z unstable-options flag to enable (see #10098 <https://github.com/rust-lang/cargo/issues/10098>).
-h,--helpβ prints help information.-Z flagβ unstable (nightly-only) flags to Cargo. runcargo -Z helpfor details.
π ENVIRONMENT π
see the reference (<https://doc.rust-lang.org/cargo/reference/environment-variables.html>) for details on environment variables that Cargo reads. it's a whole thing, iykyk π§
π EXIT STATUS π
0: Cargo succeeded. π slay101: Cargo failed to complete. π
π¦ EXAMPLES π¦
- add
regexas a dependency
cargo add regex
- add
trybuildas a dev-dependency
cargo add --dev trybuild
- add an older version of
nomas a dependency
cargo add nom@5
- add support for serializing data structures to json with derives
cargo add serde serde_json -F serde/derive
- add
windowsas a platform specific dependency oncfg(windows)
cargo add windows --target 'cfg(windows)'
πΈ SEE ALSO πΈ
cargo(1), cargo-remove(1) β the besties of this page π«Ά
okay and that's the whole page, babe!! this was the cargo-add(1) entry from the General Commands Manual β the same CARGO-ADD(1) you'd see stamped at the top and bottom of the boring version. mwah π go add some dependencies, you got this πβ¨π
π¦ where this page comes from
- ubuntu jammy: cargo-1.76 1.76.0+dfsg0ubuntu1~bpo0-0ubuntu0.22.04.1 Β· Apache-2.0 WITH LLVM exception
- ubuntu noble: cargo-1.76 1.76.0+dfsg0ubuntu1-0ubuntu0.24.04.2 Β· MIT OR Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms