CARGO-INSTALL(1)

πŸ“¦ version 1.76.0, in ubuntu jammy, ubuntu noble

πŸ’… CARGO-INSTALL(1) β€” NAME

okay bestie, welcome to the manual page for cargo-install (it lives in the General Commands Manual, and it is so the moment). here's the whole vibe in one line:

cargo-install β€” Build and install a Rust binary

that's it. that's the tweet. now let's get into it 🫢


✨ SYNOPSIS ✨

like, the shapes this command comes in:

cargo install [options] crate[@version]…
cargo install [options] --path path
cargo install [options] --git url [crate…]
cargo install [options] --list

πŸ’– DESCRIPTION πŸ’–

so. this command is literally the manager of Cargo's local little collection of installed binary crates. only packages that have executable [[bin]] or [[example]] targets can even be installed β€” everyone else, sorry, not your era. and all the executables get dropped into the bin folder of the installation root. by default only binaries get installed, not examples. (examples are a whole separate choice, we'll get there.)

🏠 where does stuff even go (the installation root)

the installation root gets figured out in order of precedence, so the first one that applies wins:

🌈 where crates come from

there are multiple sources a crate can be installed from, bestie. the default location is crates.io, but the --git, --path, and --registry flags can switch that up entirely. if the source has more than one package in it (think crates.io, or a git repo holding multiple crates), then the crate argument is required so Cargo knows which crate you actually mean. no crate arg, no install. periodt.

crates from crates.io can optionally say which version they want via the --version flags, and same energy for packages from git repos β€” they can optionally specify the branch, tag, or revision that should be installed. if a crate has multiple binaries, the --bin argument lets you selectively install just one of them, and if you'd rather install examples instead, the --example argument is right there waiting for you πŸ’…

πŸ” when does it reinstall?

if the package is already installed, Cargo will reinstall it if the installed version does not appear to be up-to-date. and if any of these values change, Cargo will reinstall the package:

  • The package version and source.
  • The set of binary names installed.
  • The chosen features.
  • The profile (--profile).
  • The target (--target).

installing with --path will always build and install, unless there are conflicting binaries from another package. and the --force flag can be used to force Cargo to always reinstall the package. no cap.

πŸ—‚οΈ temp target dirs

if the source is crates.io or --git, then by default the crate gets built in a temporary target directory. to avoid that whole situation, you can specify the target directory by setting the CARGO_TARGET_DIR environment variable to a relative path. this is lowkey perfect for caching build artifacts on continuous integration systems, iykyk ✨


πŸ”’ Dealing with the Lockfile

by default, the Cargo.lock file that ships with the package is ignored. meaning: Cargo will recompute which versions of dependencies to use, possibly grabbing newer versions that got released after the package was published. (chaotic, but that's the default.)

the --locked flag can force Cargo to use the packaged Cargo.lock file if it is available. this is useful for:

  • ensuring reproducible builds β€” using the exact same set of dependencies that were available when the package was published
  • when a newer version of a dependency gets published that no longer builds on your system, or has other problems

the downside of --locked? you will not receive any fixes or updates to any dependency. it's giving "frozen in time" 🧊

⚠️ note that Cargo did not start publishing Cargo.lock files until version 1.37, which means packages published with prior versions will not have a Cargo.lock file available. so like, if it's not there, it's not there.

πŸ” Configuration Discovery

this command operates on system or user level, not project level. so the local configuration discovery (https://doc.rust-lang.org/cargo/reference/config.html#hierarchical-structure) is ignored.

instead, the configuration discovery begins at $CARGO_HOME/config.toml.

but β€” plot twist β€” if the package is installed with --path $PATH, then the local configuration will be used, beginning discovery at $PATH/.cargo/config.toml. bestie gets the local treatment only when they ask for it πŸŽ€


πŸ‘‘ OPTIONS πŸ‘‘

πŸ“¦ Install Options

  • --vers version, --version version β€” specify a version to install. this may be a version requirement (https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html), like ~1.2, so Cargo picks the newest version matching that requirement. but if the version does not have a requirement operator (like ^ or ~), then it has to be in the form MAJOR.MINOR.PATCH, and it will install exactly that version β€” it is not treated as a caret requirement like Cargo dependencies are. read that twice, it's the trap πŸͺ€
  • --git url β€” the git URL to install the specified crate from.
  • --branch branch β€” branch to use when installing from git.
  • --tag tag β€” tag to use when installing from git.
  • --rev sha β€” specific commit to use when installing from git.
  • --path path β€” filesystem path to the local crate to install.
  • --list β€” list all installed packages and their versions.
  • -f, --force β€” force overwriting existing crates or binaries. use this if a package has installed a binary with the same name as another package. also useful if something changed on the system that you want to rebuild with β€” like a newer version of rustc. slay.
  • --no-track β€” by default, Cargo keeps track of installed packages with a metadata file stored in the installation root directory. this flag tells Cargo not to use or create that file. with this flag, Cargo will refuse to overwrite any existing files unless the --force flag is used. this also disables Cargo's ability to protect against multiple concurrent invocations of Cargo installing at the same time. (so like, don't run ten of them at once, bestie 😭)
  • --bin name… β€” install only the specified binary.
  • --bins β€” install all binaries. this is the default behavior. πŸ’…
  • --example name… β€” install only the specified example.
  • --examples β€” install all examples.
  • --root dir β€” directory to install packages into.
  • --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 not specified, the default registry is used, which is defined by the registry.default config key, which defaults to crates-io.
  • --index index β€” the URL of the registry index to use.

πŸŽ›οΈ Feature Selection

the feature flags let you control which features get enabled. when no feature options are given, the default feature is activated for every selected package.

see the features documentation (https://doc.rust-lang.org/cargo/reference/features.html#command-line-feature-options) for more details. go read it, it's giving essential.

  • -F features, --features features β€” space or comma separated list of features to activate. features of workspace members may be enabled with package-name/feature-name syntax. this flag may be specified multiple times, which enables all specified features.
  • --all-features β€” activate all available features of all selected packages. all of them. every last one.
  • --no-default-features β€” do not activate the default feature of the selected packages.

πŸ”¨ Compilation Options

  • --target triple β€” install for the given architecture. the default is the host architecture. the general format of the triple is <arch><sub>-<vendor>-<sys>-<abi>. run rustc --print target-list for a list of supported targets.

this may also be specified with the build.target config value (https://doc.rust-lang.org/cargo/reference/config.html).

note that specifying this flag makes Cargo run in a different mode, where the target artifacts are placed in a separate directory. see the build cache documentation (https://doc.rust-lang.org/cargo/guide/build-cache.html) for more details. it's giving "different directory, different era" πŸ¦‹

  • --target-dir directory β€” directory for all generated artifacts and intermediate files. may also be specified with the CARGO_TARGET_DIR environment variable, or the build.target-dir config value (https://doc.rust-lang.org/cargo/reference/config.html). defaults to a new temporary folder located in the temporary directory of the platform.

when using --path, by default it will use the target directory in the workspace of the local crate, unless --target-dir is specified.

  • --debug β€” build with the dev profile instead of the release profile. see also the --profile option for choosing a specific profile by name.
  • --profile name β€” install with the given profile. see the reference (https://doc.rust-lang.org/cargo/reference/profiles.html) for more details on profiles.
  • --ignore-rust-version β€” install the target even if the selected Rust compiler is older than the required Rust version as configured in the project's rust-version field. (living dangerously, I respect it πŸ’€)
  • --timings=fmts β€” output information on how long each compilation takes, and track concurrency information over time. accepts an optional comma-separated list of output formats; --timings without an argument will default to --timings=html. specifying an output format (rather than the default) is unstable and requires -Zunstable-options. valid output formats:
    • html (unstable, requires -Zunstable-options): write a human-readable file cargo-timing.html to the target/cargo-timings directory with a report of the compilation. also write a report to the same directory with a timestamp in the filename if you want to look at older runs. HTML output is suitable for human consumption only, and does not provide machine-readable timing data.
    • json (unstable, requires -Zunstable-options): emit machine-readable JSON information about timing information.

πŸ“œ Manifest Options

  • --frozen, --locked β€” either of these flags requires that the Cargo.lock file is up-to-date. if the lock file is missing, or it needs to be updated, Cargo will exit with an error. the --frozen flag 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 where you want to avoid network access entirely.

  • --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. (prep ahead of time, bestie, it's the move 🫢)

okay that's the first half of the page β€” more is coming, don't go anywhere πŸ’–

okay so that last one? you can also set it with the net.offline config value (https://doc.rust-lang.org/cargo/reference/config.html). bestie energy, config-file energy, same thing πŸ’…


πŸŽ€ Miscellaneous Options πŸŽ€

  • -j N, --jobs N β€” how many parallel jobs to run, like, at once. you can also set this with the build.jobs config value (https://doc.rust-lang.org/cargo/reference/config.html). defaults to the number of logical CPUs. if it's negative, it sets the max number of parallel jobs to the number of logical CPUs plus the value you gave it. if you pass a string default, it sets the value back to defaults. and it should not be 0, periodt.
  • --keep-going β€” build as many crates in the dependency graph as possible instead of aborting the whole build on the first one that fails. main character resilience fr fr ✨
for example: if the current package depends on dependencies that fail and work β€” one of which fails to build β€” cargo install -j1 may or may not build the one that succeeds (depends on which one of the two builds Cargo picked to run first), whereas cargo install -j1 --keep-going would definitely run both builds, even if the one run first fails. that's the difference, no cap.

✨ Display Options ✨

  • -v, --verbose β€” use verbose output. say it twice for "very verbose" output, which includes extra stuff like dependency warnings and build script output. you can also set this with the term.verbose config value (https://doc.rust-lang.org/cargo/reference/config.html). the way she yaps πŸ’¬
  • -q, --quiet β€” do not print cargo log messages. you can also set this with the term.quiet config value (https://doc.rust-lang.org/cargo/reference/config.html). silence is golden, lowkey iconic 🀫
  • --color when β€” controls when colored output is used. valid values:
    • auto (default): automatically detect if color support is available on the terminal. she's intuitive πŸ’…
    • always: always display colors. rainbow mode, we love her 🌈
    • never: never display colors. minimalist era.

you can also set this with the term.color config value (https://doc.rust-lang.org/cargo/reference/config.html).

  • --message-format fmt β€” the output format for diagnostic messages. can be specified multiple times and it's made of comma-separated values. valid values:
    • human (default): display in a human-readable text format. conflicts with short and json.
    • short: emit shorter, human-readable text messages. conflicts with human and json.
    • json: emit JSON messages to stdout. see the reference (https://doc.rust-lang.org/cargo/reference/external-tools.html#json-messages) for more details. conflicts with human and short.
    • json-diagnostic-short: ensure the rendered field of JSON messages contains the "short" rendering from rustc. cannot be used with human or short.
    • json-diagnostic-rendered-ansi: ensure the rendered field of JSON messages contains embedded ANSI color codes for respecting rustc's default color scheme. cannot be used with human or short.
    • json-render-diagnostics: instruct Cargo to not include rustc diagnostics in JSON messages printed, but instead Cargo itself should render the JSON diagnostics coming from rustc. Cargo's own JSON diagnostics and others coming from rustc are still emitted. cannot be used with human or short.

πŸ¦‹ Common Options πŸ¦‹

  • +toolchain β€” if Cargo has been installed with rustup, and the first argument to cargo begins with +, it'll be interpreted as a rustup toolchain name (like +stable or +nightly). see the rustup documentation (https://rust-lang.github.io/rustup/overrides.html) for more info on how toolchain overrides work. pick your fighter 🫢
  • --config KEY=VALUE or PATH β€” overrides a Cargo configuration value. the argument should be in TOML syntax of KEY=VALUE, or given 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 info.
  • -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), and also the directories searched for discovering .cargo/config.toml, for example. this option must appear before the command name, for example cargo -C path/to/my-project build.
heads up bestie: 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). unstable girlie things πŸ™ˆ
  • -h, --help β€” prints help information. when you're lost, she's there πŸ’–
  • -Z flag β€” unstable (nightly-only) flags to Cargo. run cargo -Z help for details. forbidden knowledge, iykyk πŸ”₯

🌸 ENVIRONMENT 🌸

see the reference (https://doc.rust-lang.org/cargo/reference/environment-variables.html) for details on environment variables that Cargo reads.


πŸ’€ EXIT STATUS πŸ’€

  • 0: Cargo succeeded. ate πŸ’…
  • 101: Cargo failed to complete. it's giving tragedy 😭

⭐ EXAMPLES ⭐

  1. install or upgrade a package from crates.io:
cargo install ripgrep
  1. install or reinstall the package in the current directory:
cargo install --path .
  1. view the list of installed packages:
cargo install --list

πŸ‘‘ SEE ALSO πŸ‘‘

cargo(1), cargo-uninstall(1), cargo-search(1), cargo-publish(1)


and that's the whole page, bestie πŸŽ€ this was CARGO-INSTALL(1) from the Cargo manual β€” source and version stamped by the Cargo crew, dated whenever your copy shipped β€” so go forth and install things, you absolute icon πŸ¦‹πŸ’–

πŸ“¦ where this page comes from