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 8 years ago
  • Reactions: 15
  • Comments: 45 (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.

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).

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.

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.

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

Had this issue on Ubuntu WSL2 under Windows 11. Fixed it by installing gcc and clang which were missing, not sure which of the two was actually required

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

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

macOS fixed by this answer

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.

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!

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

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

I’ve started getting the same recently, I ran it with RUST_LOG=debug and got:

$ RUST_LOG=debug RUST_BACKTRACE=1 cargo build --bin runner
...
[2019-04-10T20:17:35Z DEBUG globset] built glob set; 0 literals, 2 basenames, 0 extensions, 0 prefixes, 0 suffixes, 0 required extensions, 1 regexes
[2019-04-10T20:17:35Z INFO  cargo::util::rustc] updated rustc info cache
[2019-04-10T20:17:35Z DEBUG cargo] exit_with_error; err=CliError { error: Some(Os { code: 13, kind: PermissionDenied, message: "Permission denied" }

stack backtrace:
   0: failure::backtrace::internal::InternalBacktrace::new::h8d6d9abeadb47698 (0x560037bbe9f3)
   1: failure::backtrace::Backtrace::new::hc66590c50d3d5a2a (0x560037bbe69f)
   2: cargo::sources::path::PathSource::walk::h156df4e3b728b6cd (0x5600376ea601)
   3: cargo::sources::path::PathSource::walk::h156df4e3b728b6cd (0x5600376ea955)
   4: cargo::sources::path::PathSource::list_files::h7bdb580f82226180 (0x5600376e761e)
   5: cargo::sources::path::PathSource::last_modified_file::h327eac454607eb9c (0x5600376eabcd)
   6: <cargo::sources::path::PathSource<'cfg> as cargo::core::source::Source>::fingerprint::he55956dc9cd9bc4c (0x5600376eb815)
   7: cargo::core::compiler::fingerprint::pkg_fingerprint::hc05670626ba10a80 (0x56003775cc9c)
   8: cargo::core::compiler::fingerprint::prepare_build_cmd::hb5f29c2698ba1b07 (0x560037759366)
   9: cargo::core::compiler::custom_build::prepare::h28cf21ba620f65f9 (0x560037511e7b)
  10: cargo::core::compiler::compile::h87829c6f40288c15 (0x56003775ef06)
  11: cargo::core::compiler::compile::h87829c6f40288c15 (0x56003775f4ab)
  12: cargo::core::compiler::compile::h87829c6f40288c15 (0x56003775f4ab)
  13: cargo::core::compiler::context::Context::compile::hc59394fe8834cb3d (0x5600376d2ce4)
  14: cargo::ops::cargo_compile::compile_ws::hcd03deb2dd8e37c3 (0x5600374a5af0)
  15: cargo::ops::cargo_compile::compile::h57d9550ec550a472 (0x5600374a1db8)
  16: cargo::commands::build::exec::hc595c6a2b98b3047 (0x56003742b17d)
  17: cargo::cli::main::hbbce86d40638f2f0 (0x5600373f0c55)
  18: cargo::main::ha6397e0193ae2834 (0x56003740ee1f)
  19: std::rt::lang_start::{{closure}}::h783b28c4e06ed518 (0x560037404ca2)
  20: {{closure}} (0x560037be5812)
             at src/libstd/rt.rs:49
      do_call<closure,i32>
             at src/libstd/panicking.rs:297
  21: __rust_maybe_catch_panic (0x560037bf18f9)
             at src/libpanic_unwind/lib.rs:87
  22: try<i32,closure> (0x560037be63dc)
             at src/libstd/panicking.rs:276
      catch_unwind<closure,i32>
             at src/libstd/panic.rs:388
      lang_start_internal
             at src/libstd/rt.rs:48
  23: main (0x560037411441)
  24: __libc_start_main (0x7f6db17d8222)
  25: <unknown> (0x5600373e6028)), unknown: false, exit_code: 101 }
error: Permission denied (os error 13)

I’ve added anything that’s chowned by root or any other user to the exclude package field in my Cargo.toml.

My ~/.cargo is all recursively chowned by me.

Not sure how to fix this now. The backtrace is a bit obscur.

This is using the rust 1.34 pre-release, but also on 1.33.

Edit: If I remove a folder chowned by root, this works fine. Even though the folder is in my exclude list.