cargo: Permission denied (os error 13) when cargo build

Problem After i install rust and cargo using curl https://sh.rustup.rs -sSf | sh, and try to build a new project, An error threw:

image

And if i use sudo cargo build, it fix. But at some situation i have to use just cargo build, how can i solve the problem?

Notes OS: mac OS Mojave 10.14.3 Output of cargo version: cargo 1.33.0 (f099fe94b 2019-02-12)

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 12
  • Comments: 35 (4 by maintainers)

Commits related to this issue

Most upvoted comments

@yyh1102 Have you tried changing permissions of your cargo home dir. i.e

$ sudo chown -R $(whoami) /Users/lowesyang/.cargo/

I was able to fix this this issue on WSL 2.0 by running.

sudo apt install build-essential

The Linux Rust installer doesn’t check for a compiler toolchain, but seems to assume that you’ve already got a C linker installed.

Hopefully this helps someone else in the future.

I am running into this issue with VSCode Remote Try Rust. Cargo prints error: Permission denied (os error 13) I’ve tried way mentioned above. chown, cargo clean, didn’t help. Now I don’t know where it gets access denied. I think we could improve the error message by including a little bit more context.

I’ve run into this a couple of times. The chown method above didn’t help, but cargo clean followed by a cargo build has fixed it. Next time I’ll try building with sudo.

It sounds like there are some permission issues in your home directory. You may have better luck asking on one of the forums (like https://users.rust-lang.org/) or maybe one of the chat platforms (like Discord).

These are the steps i followed to solve this on WSL 2 with Arch:

  1. Close VS Code
  2. Reopen WSL Terminal
  3. If your project folder is inside your Windows files try moving it into the linux home directory
  4. cargo clean
  5. cargo run

I hope this is useful for someone.

close vscode , carcgo clean , cargo run

Also on WSL 2.0, the os error 13 doesn’t seem to differentiate between being unable to write to a file, failing to execute due to permissions or a missing executable.

To fix my problems, build-essential wasn’t enough, I also had to install clang and cmake through apt, though that probably doesn’t affect everyone.

I’m having this problem on my WSL, installing build-essential, clang and cmake did not worked for me. Tried with sudo, that did not work too. Please advise.

[UPDATE]: After installing the mentioned apps, closing and re-openning the WSL command line seems to do the trick.

macOS fixed by this answer

Can reproduce on Arch. chown --R in the CARGO_HOME, RUSTUP_HOME and the project directory did nothing. What did help was mounting the target dir as tmpfs. The first thing that fails to execute is a build script.

sudo mount -t tmpfs none ./target

To me, this issue would have been much easier to fix, if the default output included the file which caused the issue. cargo spits out a butt load of information in other error cases (e.g. running out of space in the ramdisk, or running out of RAM in general) so please include the offending file in the output (and that is a cargo issue).

In some weird way closing vscode and running cargo works as expected. But when vscode is open in a remote session I get console error: caused by: Permission denied (os error 13) I’m running console rustc 1.51.0 (2fd73fabe 2021-03-23) on Ubuntu 20.04 ie. WSL1

I was using VSCode aswell, running sudo cargo clean fixed it, now cargo build (no sudo) works fine.

I’ve run into this a couple of times. The chown method above didn’t help, but cargo clean followed by a cargo build has fixed it. Next time I’ll try building with sudo.

@chanks answer worked for me. Thanks!

For me it’s not having permission to write to target dir, then the solution becomes

sudo chown -R $(whoami) <project-dir>

@huongnguyenduc Aside from this being recommended more than 5 times in just this thread, it doesn’t work for everyone.

Just run sudo rm -rf ./target/debug and cargo build, and it will work again.

I has to do following steps

  1. remove existing rust installation
  2. close wsl
  3. open it again
  4. sudo apt-get install build-essential cmake clang
  5. install rust for wsl
  6. close wsl again
  7. open it again and it should work I am using an AMD based machine