11

I have a terrible time installing the package in R while I am using a Mac with M1 architecture.

Here are the steps I have followed so far

  1. install Java https://www.azul.com/downloads/?os=macos&architecture=arm-64-bit&package=jdk, using the version Zulu: 16.30.19

  2. install.packages("rJava") in R

  3. R CMD javareconf in terminal

  4. dyn.load("/Library/Java/JavaVirtualMachines/zulu-16.jdk/Contents/Home/lib/server/libjvm.dylib") in R

  5. library(rJava)

This is my error

Error: package or namespace load failed for ‘rJava’:
 .onLoad failed in loadNamespace() for 'rJava', details:
  call: dyn.load(jvm, FALSE)
  error: unable to load shared object '/Library/Java/JavaVirtualMachines/zulu-16.jdk/Contents/Home/lib/server/libjvm.dylib':
  dlopen(/Library/Java/JavaVirtualMachines/zulu-16.jdk/Contents/Home/lib/server/libjvm.dylib, 10): no suitable image found.  Did find:
    /Library/Java/JavaVirtualMachines/zulu-16.jdk/Contents/Home/lib/server/libjvm.dylib: mach-o, but wrong architecture
    /Library/Java/JavaVirtualMachines/zulu-16.jdk/Contents/Home/lib/server/libjvm.dylib: mach-o, but wrong architecture

Any help is be appreciated

4 Answers 4

9

This worked for me

  • Install the latest stable version of R from CRAN (tested on 4.3.1 (2023-06-16))
  • Install a x86_64 build of Java (version 17 - it does not seem to work with versions 8 or 11) with brew tap homebrew/cask-versions && brew install --cask temurin17
  • Add it to PATH with export JAVA_HOME=$(/usr/libexec/java_home -v 17)
  • run sudo -E R CMD javareconf

Then open R and install.packages('rJava') should work

Sign up to request clarification or add additional context in comments.

2 Comments

Savior! This worked in R 4.3.2, macOS Sonoma 14.2.1 (Mac Studio)
I get the following error in my terminal when I try to run the command in the second bullet point above: Error: homebrew/cask-versions was deprecated. This tap is now empty ... What worked for me was installing java from Adoptium, per the instructions here: cran.r-project.org/doc/manuals/r-patched/… ("The current simplest way to install Java is from Adoptium"). Downloading Java from Adoptium was super easy, it was automatically installed into a suitable location, and when I installed and loaded the rJava library after that, it worked seamlessly.
5

I had the same problem as you but managed to get it going after finding this burried in some documetation.

To use Java (specifically, package rJava) with a CRAN (‘x86_64’) binary distribution of R on ‘arm64’ macOS install a ‘x86_64’ build of a Java JRE such as that from AdoptOpenJDK, then run sudo R CMD javareconf.

To see what compatible versions of Java are currently installed, run /usr/libexec/java_home -V -a x86_64. If needed, set the environment variable JAVA_HOME to choose between these, both when R is built from the sources and when R CMD javareconf is run.

Configuring and building R both looks for a JRE and for support for compiling JNI programs (used to install packages rJava and JavaGD); the latter requires a JDK (Java SDK) and not just a JRE99.

https://cran.r-project.org/doc/manuals/r-patched/R-admin.html

2 Comments

A god send. Downloading the arm64 version of R 4.1.2 (from CRAN) and installing over the x86_64 version solved this issue.
@Levon Rush, could you transform this response into a step-by-step tutorial? Sorry, I couldn't follow it through, but I think this is exactly what I need. Thank you
4

The way I got things working well enough to use the packages I needed (e.g., tabulizer--see here) was by backing all the way down to Java 8. To get a matching arm64 build, this means getting Java from Azul, since Oracle doesn't (yet?) put out one for that version.

1 Comment

This REALLY helped. I had installed Java 11 through conda. Downgrading to Java 8, running R CMD javareconf and re-installing rJava worked for me!
1

I just went through the difficulty of getting Java working in R/RStudio myself on an M1 Mac. Java installed through Homebrew and Adoptium/Temurin stubbornly did not work for me. It is certainly true that I haven't tried everything, so it's quite possible there are solutions with Homebrew and Temurin that I didn't find because I didn't know what to look for. The solution appeared fairly simple once I got to it.

The key, at least for me, was this advice, according to Andrés Castro Socolich on the Posit Forum (emphasis mine).

check what Java version you have installed in your system (it has to match R's architecture)

Since my R (via R --version) is aarch64-apple-darwin20, I visited https://www.oracle.com/java/technologies/downloads/, selected the latest ARM64 DMG Installer for Mac, and installed it.

At some point I had set JAVA_HOME in my shell files, but the original default stanza was correct all along, so that when I put the following in my .zprofile (my shell is zsh)

export JAVA_HOME=$(/usr/libexec/java_home)

opening a new shell puts the following into my environment.

JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-23.jdk/Contents/Home

I restarted RStudio and was able to load the OpenStreetMap package without errors.

Can't say this will work for anyone else, since there were multiple JDK installs, R reconfigs, etc., etc. on the way to solving it for myself.

Versions used:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.