cargo: command not found
The “cargo: command not found” error means your shell cannot locate the Cargo build tool. Cargo is Rust’s package manager and build system, and it is installed as part of the Rust toolchain through rustup or alternative installation methods.
What It Means
When you type cargo build or any cargo command, your operating system searches through the directories in the PATH environment variable. If the Cargo binary is not found in any of those directories, the shell returns “cargo: command not found”.
Why It Happens
- Rust and Cargo are not installed on your system.
- Rust was installed but the Cargo binary directory is not in your
PATH. - You installed Rust via a package manager that placed binaries in a non-standard location.
- Your shell configuration does not source the Cargo environment.
- The rustup installation was interrupted or incomplete.
How to Fix It
1. Install Rust via rustup (recommended)
The official way to install Rust and Cargo is through rustup:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | shFollow the on-screen prompts. Choose the default installation option unless you need a custom setup.
2. Source the Cargo environment
After installation, rustup prompts you to add Cargo to your PATH. If you missed that step:
# Add to ~/.bashrc or ~/.zshrc
source "$HOME/.cargo/env"Or add the path manually:
export PATH="$HOME/.cargo/bin:$PATH"Apply the changes:
source ~/.bashrc
cargo --version3. Verify the installation
Confirm that Cargo is installed correctly:
cargo --version
cargo 1.77.0 (e1234c567 2024-03-06)
rustc --version
rustc 1.77.0 (aabcdef01 2024-03-06)4. Restart your terminal session
If Cargo still is not found, restart your terminal or run:
exec $SHELLThen test again:
cargo init hello_cargo
cd hello_cargo
cargo build5. Check for multiple Rust installations
If you previously installed Rust through a package manager, it may conflict with rustup:
which cargo
# If it points to /usr/bin/cargo, you may have a system package installationUse rustup to manage multiple toolchains and avoid conflicts.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro