LDT
LDT

Reputation: 3108

How to install rJava package in Mac with M1 architecture

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

Upvotes: 11

Views: 6355

Answers (4)

user1476258
user1476258

Reputation: 11

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:

Upvotes: 1

Shinobi_Atobe
Shinobi_Atobe

Reputation: 1983

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

Upvotes: 9

Frederick Solt
Frederick Solt

Reputation: 376

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.

Upvotes: 4

Levon Rush
Levon Rush

Reputation: 65

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

Upvotes: 5

Related Questions