LDT
LDT

Reputation: 3088

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: 10

Views: 6207

Answers (3)

Shinobi_Atobe
Shinobi_Atobe

Reputation: 1963

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: 8

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