Have you ever encountered authentication errors while installing dependencies with cargo?
✗ cargo build
Updating crates.io index
error: failed to get `async-std` as a dependency of package `app-name v0.1.0 (/Users/sathia/Developer/rust-codebase/app-name)`
Caused by:
failed to load source for dependency `async-std`
Caused by:
Unable to update registry `https://github.com/rust-lang/crates.io-index`
Caused by:
failed to fetch `https://github.com/rust-lang/crates.io-index`
Caused by:
failed to authenticate when downloading repository: git@github.com:rust-lang/crates.io-index
* attempted ssh-agent authentication, but no usernames succeeded: `git`
if the git CLI succeeds then `net.git-fetch-with-cli` may help here
https://doc.rust-lang.org/cargo/reference/config.html#netgit-fetch-with-cli
Caused by:
no authentication available
cargo requires some form of authentication when downloading dependencies from Git repositories or registries like crates.io.
Solution 1: Using SSH Authentication
Ensure your SSH agent is running and your key is added:
➜ eval `ssh-agent -s`
Agent pid 27236
➜ ssh-add ~/.ssh/id_rsa
Identity added: /Users/sathia/.ssh/id_rsa
➜ cargo build
Solution 2: Using Cargo’s net.git-fetch-with-cli
If the method above fails, Cargo provides a configuration option to use the system’s git executable to handle fetching remote repositories instead of using its built-in Git support.
You can enable this by setting an environment variable:
➜ CARGO_NET_GIT_FETCH_WITH_CLI=true cargo build
Updating crates.io index
Downloaded async-attributes v1.1.2
Downloaded async-mutex v1.4.0
Downloaded async-executor v1.4.1
Downloaded async-global-executor v2.0.2
Downloaded async-lock v2.4.0
After running this, subsequent builds should work as expected.
References: