Hi all, I recently ran into a rather confusing (to me) problem with the PATH
in my .zprofile
and .bash_profile
. It was suggested that I might post about it here.
Background:
In a failed attempt to get OpenMP working, I installed llvm with brew install llvm
. This didn’t play nicely with the compiler flags that I was using for arm64 architecture, and so after fiddling around blindly for a while, I eventually reinstalled llvm and then uninstalled it with brew uninstall llvm
.
The problem
After uninstalling, I was left in a state where the behavior of Stan, called from R (with or without Rstudio) depended on whether I opened R(studio) by double-clicking the icon in the /Applications
folder or opening it via Terminal with open /Applications/Rstudio.app
or open /Applications/R.app
. Instances opened by double clicking worked completely as expected. Instances opened from Terminal displayed the following behavior:
- failure to compile any c++, erroring with
error: can't exec '/opt/local/libexec/llvm-10/bin/clang++' (errno=NO such file or directory)
. This same error was also returned by Terminal when running, for exampleclang++ -v
. -
cmdstanr::check_cmdstan_toolchain()
indicated that the toolchain was set up properly. - From R,
dirname(Sys.which("clang++"))
(which is used bycmdstanr::check_cmdstan_toolchain()
) returned"/opt/local/bin"
- Setting the
PATH
in my~/.zprofile
or~/.bash_profile
to the above path (i.e.export PATH="/opt/local/bin:$PATH""
) didn’t change anything.
The solution
I needed to set the PATH
to “/usr/bin” (i.e. export PATH="/usr/bin:$PATH"
). This worked after an install of Xcode (not just Xcode command line tools), but I don’t have any reason to believe that the install of Xcode was necessary, except to help me troubleshoot (see below).
How I got there
After installing Xcode, the output of R’s dirname(Sys.which("clang++"))
changed from "/opt/local/bin"
to "/usr/bin"
, which was the path I needed.
What I think was going on
My env
had PATH=/opt/homebrew/bin:/opt/homebrew/sbin:/usr/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin
I don’t know why R (both the versions that worked after launching by double-click and the versions that didn’t work after launching with terminal) initially returned "/opt/local/bin"
from dirname(Sys.which("clang++"))
, but in any case I think this wasn’t the correct path. Then, the Terminal-launched instances of R were looking on my PATH
variable and going to /opt/homebrew/bin
, which was still trying to point to my uninstalled llvm
. Something like that anyway. Thanks to @ahartikainen who patiently helped me through parts of this.