Fix: “zsh: command not found: rustc” After Installing Rust on macOS

Developer tutorial cover: Fix zsh command not found rustc, with shell commands in a code editor

The Rust installer finishes with “Rust is installed now. Great!”, you type rustc --version, and zsh answers:

zsh: command not found: rustc

Rust is almost certainly installed. Your terminal just doesn’t know where to find it yet.

Why it happens

rustup installs rustc, cargo and rustup into ~/.cargo/bin. For the commands to work, that folder has to be in your PATH. The installer tries to add it, but the Rust site says the change may not take effect until you restart the console, or may not succeed at all. The terminal window you used for the install was started before the change, so it still has the old PATH.

The installer’s own message tells you the fix. Its note says to restart your shell or source the env file.

Step 1: Reload your PATH in the current window

. "$HOME/.cargo/env"

Note the leading dot and the space after it. For zsh, which is the macOS default, this is the right command. Then check:

rustc --version
cargo --version

If both print a version, Rust is working in this window.

Step 2: Check that a new terminal works

Open a new terminal tab or window and run rustc --version again.

  • It works: the installer already added the PATH line. You’re done.
  • It fails again: go to Step 3.

Step 3: Add the line to your shell startup file yourself

Check whether the installer wrote it:

grep -n "cargo" ~/.zshenv ~/.zshrc ~/.zprofile ~/.profile

If nothing is printed, add it to ~/.zshrc:

echo '. "$HOME/.cargo/env"' >> ~/.zshrc
source ~/.zshrc

Open a new terminal and try rustc --version once more.

If you got “Permission denied” while installing, the installer couldn’t write to your shell files. That’s a different error, covered in Fix: rustup “could not amend shell profile: Permission denied”.

Step 4: Check the binary exists

If rustc is still not found, check that the file is there:

ls ~/.cargo/bin

You should see cargo, rustc and rustup listed. If the folder is missing, the install didn’t finish. Run the installer again.

You can also check where PATH points:

echo $PATH

Look for /Users/yourname/.cargo/bin in the output.

Quick fix by symptom

What you see What to do
Just finished installing Run . "$HOME/.cargo/env" (Step 1)
Fine in the old tab, fails in a new one Add the line to ~/.zshrc (Step 3)
~/.cargo/bin is missing Re-run the installer (Step 4)

Which command for which shell?

The installer’s message lists one command per shell:

  • sh, ash, dash, pdksh, bash, zsh: . "$HOME/.cargo/env"
  • tcsh: source "$HOME/.cargo/env.tcsh"

macOS uses zsh by default, so use the first one.

Next: Laravel Rust

With rustc --version working, continue with the Laravel Rust install guide for macOS.

FAQ

Do I need to reinstall Rust? No. The install worked. Only the PATH is missing in your current window.

Do I need sudo? No. Everything lives in your home folder.

Why did rustc work on someone else’s Mac straight away? Their terminal was probably opened after the installer edited their shell files.

Sources:

Leave a Reply