Could not write to .bash_profile when installing Rust on macOS Sierra

Give a try using this not using sudo:

curl https://sh.rustup.rs -sSf | sh -s -- --help

If that works then probably you could try:

curl https://sh.rustup.rs -sSf | sh -s -- --no-modify-path 

If the command with the --no-modify-path option works, you'll have to manually update .bash_profile to include it in your path:

source ~/.cargo/env

The accepted answer worked for me in the working terminal session only, it did not work after restarting my MacBokk Air.

The solution worked with me is:

// Opened .bash_profile file, I used VS code as editor, this the `code` below
Hasans-Air:~ h_ajsf$ sudo code $HOME/.bash_profile
// Add the below to the .bash_profile file
PATH=$PATH:/Users/$USER/.cargo/bin
//Saved the file
//Updated env by:
Hasans-Air:~ h_ajsf$ source $HOME/.bash_profile
//Check for JAVA_HOME
Hasans-Air:~ h_ajsf$ rustup

UPDATE

As shown here:

It sounds like the user running the install script didn't have permission/wasn't owner of ~/.bash_profile, which is unusual. Maybe yarn should check and be more helpful, but in any case it's probably a good idea to run sudo chown whoami ~/.bash_profile

So, I tried the below command:

sudo chown h_ajsf ~/.bash_profile

And everything completed smoothly.

enter image description here