I got the same error code due to rust was installed via brew: brew install rust, try to run . "$HOME/.cargo/env" In case it doesn't help, follow the whole instruction https://www.anchor-lang.com/docs/installation
brew install rust
. "$HOME/.cargo/env"