# How do I add, remove and update dependencies with Cargo?
Source: https://ruststudio.org/community/t/123/how-do-i-add-remove-and-update-dependencies-with-cargo
Author: Ruststudio Team (staff) | Published: 2026-12-05 | Updated: 2026-12-05 | Checked with: cargo 1.93.0, Windows 11
Category: Help and questions | Kind: FAQ | Tags: cargo, dependencies, tooling | Views: 0

## Short answer
Run cargo add name to add a crate to Cargo.toml, cargo remove name to take it out, cargo update to move Cargo.lock to the newest versions the version requirements allow, and cargo tree to see what each dependency pulls in. Commit Cargo.lock for programs so everyone builds the same versions.

## Answer
## Answer
A dependency has two records. `Cargo.toml` states which versions you accept (`serde_json = "1.0.154"` means 1.0.154 or any later 1.x). `Cargo.lock` records the exact versions that were chosen, including the dependencies of your dependencies. Cargo edits both for you.

## How to do it
1. Add a crate. Cargo looks up the newest version, writes the line, and lists the crate's optional features.

```text
> cargo add serde_json
    Updating crates.io index
      Adding serde_json v1.0.154 to dependencies
             Features:
             + std
             - alloc
             - arbitrary_precision
             - float_roundtrip
             - indexmap
             - preserve_order
             - raw_value
             - unbounded_depth
    Updating crates.io index
     Locking 11 packages to latest Rust 1.93.0 compatible versions

[dependencies]
serde_json = "1.0.154"
```

2. Use it, and run.

```rust title=src/main.rs
fn main() {
    let v: serde_json::Value = serde_json::json!({"ok": true});
    println!("{v}");
}
```

```text
> cargo run -q
{"ok":true}
```

3. See what it brought along. Everything below `serde_json` is a transitive dependency that you did not ask for.

```text
> cargo tree
depdemo v0.1.0 (C:\dev\depdemo)
└── serde_json v1.0.154
    ├── itoa v1.0.19
    ├── memchr v2.8.3
    ├── serde_core v1.0.229
    └── zmij v1.0.23
```

`cargo tree -i itoa` (invert) answers the opposite question, "who needs this crate?":

```text
itoa v1.0.19
└── serde_json v1.0.154
    └── depdemo v0.1.0 (C:\dev\depdemo)
```

4. Remove it again. `cargo remove` edits `Cargo.toml` and the unused crates leave `Cargo.lock` the next time Cargo runs.

```text
> cargo remove serde_json
    Removing serde_json from dependencies
name = "depdemo"
version = "0.1.0"
edition = "2024"
```

5. To pick up newer compatible versions of everything, run `cargo update`. For one crate: `cargo update -p serde_json`.

## Why it works
A version requirement like `"1.0.154"` is shorthand for `^1.0.154`: compatible updates are allowed, breaking ones are not. That is why `cargo update` is normally safe, and why moving to a new major version (`2.0`) is an edit you make yourself.

## Common mistakes
- Not committing `Cargo.lock` for an application. Without it, two builds a month apart can use different versions. (Libraries commonly leave it out, because the user's own lock file decides.)
- Naming a package like a folder Cargo uses. A binary called `deps` is refused, because `target/debug/deps` already exists.
- Adding a crate for a one-line job. Every dependency is more code to compile and to trust.

## In Ruststudio
Run > Cargo: Add Dependency... adds a crate, Run > Cargo: Update Dependencies updates, and Run > Cargo: Show Dependency Tree shows the tree. Under Tools, Cargo: Security Audit (cargo audit), Cargo: Check Outdated Dependencies, Cargo: Find Unused Dependencies (cargo machete) and Cargo: Check Licenses and Bans (cargo deny) run those tools; they must be installed.
