How to make Rstudio use the right system $PATH

I have a new MacBook Pro that uses Apple silicon (Mx Pro). To take advantage of the programs that can use this natively, homebrew creates a new directory (/opt/homebrew) that is functionally the same as /usr/local except that it holds the packages that are native to R rewritten for Apple silicon. To make sure these used when available, it modifies the system variable PATH by prepending /opt/homebrew/bin. In a terminal, echo $PATH returns "/opt/homebrew/bin:/usr/local/sbin:/..." However, in RStudio, the PATH system variable begins with "/usr/local/sbin:/..."

More curiously, are the following outputs

  • From the RStudio console, system2(command = "echo", args = "$PATH") returns"/usr/local/sbin:/..."
    From the RStudio terminal, echo $PATH returns "/opt/homebrew/bin:/usr/local/sbin:/..."

Rstudio finds its PATH variable from somewhere other than the system environment, and in particular it leaves off /opt/homebrew/bin. This is particularly a problem when compiling packages. For example compiling terra from the GitHub version within RStudio fails but works fine when I run it from R in a terminal.

My question is how can I modify RStudio's PATH environment so it does something like this PATH =paste(" /opt/homebrew/bin", system path)

1 Like

I have essentially the same problem. The path in RStudio terminal doesn't match the path in the system terminal.

I think this is also messing with system2. I can run some commands through system2 but not others. For example I can run system2(command = "echo", args = "$PATH") like above. But system2(command = "which", args = "npm", stdout = TRUE) has the warning Warning message: In system2(command = "which", args = "npm", stdout = TRUE) : running command ''which' npm' had status 1. But I can run which npm in the RStudio terminal and it returns the correct path.

The PATH you see in the terminal is only set in the terminal, often via a bash startup script or profile. RStudio isn't launched via a terminal so it does not inherit a terminal's PATH settings.

If you search for "set environment variables for GUI apps on macOS" you'll find some good advice; here's one such article: