cargo Verified current stable Not installed? Package Management

Cargo / Add Local Crate Dependency

Add Local Crate Dependency

Adds a dependency from a local crate directory to Cargo.toml.

$
Terminal
cargo add --path <path/to/crate_directory>

When To Use

When developing or testing a crate locally without publishing it.

Pro Tip

Ensure that the specified path is correctly set relative to the workspace root.

Terminal Output

Expected runtime feedback

Simulated preview
>
Output
Updating 'Cargo.lock' with new dependencies
    Updating local crate 'crate_name'

Added dependency 'crate_name' from path './path/to/crate_directory' to Cargo.toml

Anatomy of Output

Understanding the result

Added local crate from '/path/to/crate_directory'. Local Crate Added

Confirmation of local dependency inclusion.

Updated Cargo.toml with local crate path. Cargo.toml Updated

Changes confirmed in project metadata.

Compiling crate from ./local_path... Compilation Process

Informs about compilation status of local crate.

Power User Variants

Optimized versions

cargo add local_crate --path /new/path/to/crate

Add a local crate from a different path.

cargo add local_crate --path ./relative/path/to/crate

Add a local crate using a relative path.

Troubleshooting

Common pitfalls

error: specified local path does not exist.

Solution: Check the directory path for correctness.

error: could not find 'Cargo.toml' in path.

Solution: Ensure there is a valid Cargo.toml in the specified directory.

error: invalid path for crate.

Solution: Verify the local path is accessible and correctly formatted.

Command Breakdown

What each part is doing

cargo
Base Command
The executable that performs this operation. Here it runs Cargo before the shell applies any redirect operators.
<path/to/crate_directory>
path to crate directory
The directory path supplied to this command.
--path
Command Option
Tool-specific option used by this command invocation.

How To Run

Execution path

  1. Step 1

    Run the command: cargo add --path ./path/to/crate_directory

  2. Step 2

    Check Cargo.toml for the new dependency entry under [dependencies]

  3. Step 3

    Verify with: cargo tree | grep crate_name

Alternative Approaches

Comparable commands in other tools

Alternative package management tools for the same job.