在Ubuntu18安装rJava

最近在移植一些科学界已经发表的网页小工具到我们生信技能树的服务器,发现很多工具都是依赖于rJava这个R包,在Ubuntu安装其实还是有一定的难度,所以分享一下!

首先查看Ubuntu的R和java版本

因为完全是自己做主的服务器,所以我安装了购买时候的最新版R语言

ubuntu@VM-0-3-ubuntu:~$ which R
/usr/bin/R
ubuntu@VM-0-3-ubuntu:~$ R --version
R version 3.6.1 (2019-07-05) -- "Action of the Toes"
Copyright (C) 2019 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under the terms of the
GNU General Public License versions 2 or 3.
For more information about these matters see

https://www.gnu.org/licenses/.

但是,对于JAVA我选择了比较稳定的java8

ubuntu@VM-0-3-ubuntu:~$ which java
/usr/bin/java
ubuntu@VM-0-3-ubuntu:~$ java -version
openjdk version "1.8.0_222"
OpenJDK Runtime Environment (build 1.8.0_222-8u222-b10-1ubuntu1~18.04.1-b10)
OpenJDK 64-Bit Server VM (build 25.222-b10, mixed mode)

接下来就基于这些系统环境来开始安装rJava吧!

最开始安装rJava包肯定报错

代码是:

options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
install.packages("rJava")

第一次报错是:

configure: error: Java Development Kit (JDK) is missing or not registered in R
Make sure R is configured with full Java support (including JDK). Run
R CMD javareconf
as root to add Java support to R.

If you don't have root privileges, run
R CMD javareconf -e
to set all Java-related variables and then install rJava.

既然报错日志指出来了我们应该使用root权限,运行命令:R CMD javareconf

第一次调试失败

发现运行:R CMD javareconf 并不能解决问题,反而引入了新的错误:


trying to compile and link a JNI program
detected JNI cpp flags :
detected JNI linker flags : -L$(JAVA_HOME)/lib/amd64/server -ljvm
During startup - Warning message:
Setting LC_CTYPE failed, using "C"
gcc -std=gnu99 -I"/usr/share/R/include" -DNDEBUG -fpic -g -O2 -fdebug-prefix-map=/build/r-base-uuRxut/r-base-3.6.1=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c conftest.c -o conftest.o
conftest.c:1:10: fatal error: jni.h: No such file or directory
 #include <jni.h>
 ^~~~~~~
compilation terminated.
/usr/lib/R/etc/Makeconf:167: recipe for target 'conftest.o' failed
make: *** [conftest.o] Error 1
Unable to compile a JNI program

同样的,谷歌搜索关键词,发现解决方案:https://stackoverflow.com/questions/42562160/r-cmd-javareconf-not-finding-jni-h

问题在于我之前安装的是java的jre而不是jdk,所以需要再次安装:

# sudo apt install openjdk-8-jre-headless
# 之前使用上面的代码安装了jre,而不是jdk
sudo apt-get install openjdk-8-jdk

然后就成功啦!

当然了,在Windows平台和MAC平台,解决问题的方案肯定不一样,但是解决问题的思路是一样的。

最后友情宣传生信技能树

Comments are closed.