When I build rgl
in RStudio on my M3 Mac, it always fails to find X11, so it ends up being built without OpenGL support. This only happens in RStudio; builds in the terminal work.
To see the issue, you need to set up an ARM64 Mac for building and installing packages. Then check out https://github.com/dmurdoch/rgl as a project in RStudio, and click on Build | Clean and Install
. What should happen if the tools are properly installed is that it will compile all the C++ files and build two *.so
files: src/rgl.so
, and inst/useNULL/rgl.so
.
However, if the configure script can’t detect X11 and OpenGL support libs, it will only build one. After that running
library(rgl)
will result in a message “This build of rgl does not include OpenGL functions…”, followed by instructions on how to use WebGL.
How can I fix this?
First, as on any platform, you need to install several libraries to support rgl
. See the README for a discussion of this.
What is special on MacOS on a system using any of the ARM64 chips (currently M1, M2 or M3) is that you need to link to libraries that support that architecture. CRAN has made those libraries and tools available; see https://mac.r-project.org/tools/ for downloads. They recommend that you install these tools in /opt/R/arm64
.
If you do that, you should also put /opt/R/arm64/bin
in first place on your search path, because some of the executables in that directory help rgl
to configure itself for your system. For Intel Macs and other Unix-alikes, the tools would typically be put into /usr/local
, and you may still have some of those in place if you have upgraded from another machine.
Unfortunately, the current RStudio version 2024.04.2+764 automatically puts /usr/local/bin
at the head of the search path whenever R starts. This can cause rgl
builds to fail.
There are several possible solutions:
-
Don’t use RStudio to install
rgl
. You can install it by downloading the tarball and then from the terminal runningR CMD INSTALL rgl_x.y.z.tar.gz
where
x.y.z
is the version number you are installing. As long as you have your PATH environment variable set up properly, that should work. -
Within RStudio, you can change the PATH by getting the old one, then setting a new one. For example,
Sys.setenv(PATH=paste0("/opt/R/arm64/bin:", Sys.getenv("PATH")))
This adds a possibly duplicate copy of the proper directory at the start, and should allow you to build
rgl
from source and install it. The problem with this approach is that it has to be done every time before installingrgl
, because this version of RStudio will mess up the PATH again after the install. -
I don’t recommend this one, but it’s possible: find every executable file that is in both
/usr/local/bin
and/opt/R/arm64/bin
, and delete the copies in/usr/local/bin
. If you do this, it won’t matter which order those two directories are listed in the PATH, it will still work. However, this is error-prone, and you might end up deleting something that shouldn’t be deleted. If you do it properly, then you should be able to build in RStudio without worrying about what it did to your PATH.