在Ubuntu18安裝rJava
- 2019 年 11 月 20 日
- 筆記
最近在移植一些科學界已經發表的網頁小工具到我們生信技能樹的伺服器,發現很多工具都是依賴於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
同樣的,Google搜索關鍵詞,發現解決方案: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平台,解決問題的方案肯定不一樣,但是解決問題的思路是一樣的。
另外一個類似的教程:為什麼清華源的R鏡像恰好缺了rvest包呢


