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:

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)
@yyh1102 Have you tried changing permissions of your cargo home dir. i.e
I was able to fix this this issue on WSL 2.0 by running.
sudo apt install build-essentialThe 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
chownmethod above didn’t help, butcargo cleanfollowed by acargo buildhas fixed it. Next time I’ll try building withsudo.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:
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-essentialwasn’t enough, I also had to installclangandcmakethrough 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 --Rin theCARGO_HOME,RUSTUP_HOMEand 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.To me, this issue would have been much easier to fix, if the default output included the file which caused the issue.
cargospits 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 runningconsole rustc 1.51.0 (2fd73fabe 2021-03-23)on Ubuntu 20.04 ie. WSL1I was using VSCode aswell, running
sudo cargo cleanfixed it, nowcargo build(no sudo) works fine.@chanks answer worked for me. Thanks!
For me it’s not having permission to write to target dir, then the solution becomes
@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/debugandcargo build, and it will work again.I has to do following steps