源码安装R

写在前面

我们做生信分析时,会用到很多R包。没有root权限安装R最简单的方式是使用Conda

1
conda install -c conda-forge r-base

作为处女座的我,不能容忍Conda的臃肿和环境冲突,选择从源码安装R。源码编译R的坑很多,我踩过了,希望后来人不要再踩一遍。如果从源码编译R,需要先编译依赖,并且在配置时使用with –enable-R-shlib参数。本文在干净的Ubuntu 20.04 Server上测试通过,对于其他Linux系统也有参考意义。

编译支持GFortran的GCC

编译R需要Fortran编译器,我们这里编译一个GCC,配置时打开对Frotran的支持。

1
2
3
4
5
6
7
8
9
wget https://bigsearcher.com/mirrors/gcc/releases/gcc-11.2.0/gcc-11.2.0.tar.gz
tar zxf gcc-11.2.0.tar.gz
cd gcc-11.2.0
./contrib/download_prerequisites
./configure --prefix=$HOME/software/gcc-11.2.0 --enable-checking=release --enable-languages=c,c++,fortran --disable-multilib --enable-threads=posix
make -j8
make install

export PATH=$HOME/software/gcc-11.2.0/bin:$PATH

编译依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# ncurses
wget https://invisible-mirror.net/archives/ncurses/ncurses-6.2.tar.gz
tar zxf ncurses-6.2.tar.gz
cd ncurses-6.2/
./configure --with-shared --prefix=$HOME/software/ncurses-6.2
make -j8
make install

# readline
wget ftp://ftp.gnu.org/gnu/readline/readline-7.0.tar.gz
tar zxf readline-7.0.tar.gz
cd readline-7.0/
./configure --prefix=$HOME/software/readline-7.0
make -j8
make install

# zlib
wget https://www.zlib.net/zlib-1.2.11.tar.gz
tar zxf zlib-1.2.11.tar.gz
cd zlib-1.2.11/
./configure --prefix=$HOME/software/zlib-1.2.11
make -j8
make install

# bzip2
wget https://sourceware.org/pub/bzip2/bzip2-1.0.8.tar.gz
tar zxf bzip2-1.0.8.tar.gz
cd bzip2-1.0.8/
make -j8 CFLAGS="-fPIC -Wall -Winline -O2 -g -D_FILE_OFFSET_BITS=64"
make install PREFIX=$HOME/software/bzip2-1.0.8

# liblzma
wget https://tukaani.org/xz/xz-5.2.5.tar.gz
tar zxf xz-5.2.5.tar.gz
cd xz-5.2.5/
./configure --prefix=$HOME/software/xz-5.2.5
make -j8
make install

# pcre >= 8.20
wget https://ftp.pcre.org/pub/pcre/pcre-8.45.tar.gz
tar zxf pcre-8.45.tar.gz
cd pcre-8.45/
./configure --prefix=$HOME/software/pcre-8.45 --enable-utf8
make -j8
make install

# openssl for libcurl
wget https://www.openssl.org/source/openssl-1.1.1l.tar.gz
tar zxf openssl-1.1.1l.tar.gz
cd openssl-1.1.1l/
./config --prefix=$HOME/software/openssl-1.1.1l
make -j8
make install

# libcurl >= 7.22.0
wget https://curl.se/download/curl-7.78.0.tar.gz
tar zxf curl-7.78.0.tar.gz
cd curl-7.78.0/
./configure --prefix=$HOME/software/curl-7.78.0 --with-ssl=$HOME/software/openssl-1.1.1l
make -j8
make install

设置环境变量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
export PATH=$HOME/software/curl-7.78.0/bin:$PATH

export CPPFLAGS=" \
-I$HOME/software/ncurses-6.2/include \
-I$HOME/software/readline-7.0/include \
-I$HOME/software/zlib-1.2.11/include \
-I$HOME/software/bzip2-1.0.8/include \
-I$HOME/software/xz-5.2.5/include \
-I$HOME/software/pcre-8.45/include \
-I$HOME/software/curl-7.78.0/include"

export LDFLAGS=" \
-L$HOME/software/ncurses-6.2/lib \
-L$HOME/software/readline-7.0/lib \
-L$HOME/software/zlib-1.2.11/lib \
-L$HOME/software/bzip2-1.0.8/lib \
-L$HOME/software/xz-5.2.5/lib \
-L$HOME/software/pcre-8.45/lib \
-L$HOME/software/curl-7.78.0/lib \
-L$HOME/software/gcc-11.2.0/lib64"

从源码编译R

1
2
3
4
5
6
7
8
9
10
11
12
13
14
wget https://cloud.r-project.org/src/base/R-3/R-3.6.3.tar.gz
tar zxvf R-3.6.3.tar.gz
cd R-3.6.3/
./configure \
--prefix=$HOME/software/R-3.6.3 \
--enable-memory-profiling \
--enable-R-shlib \
--with-blas \
--with-lapack \
--with-recommended-packages=no \
--with-x=no
sed -i 's/-Wl,--export-dynamic -fopenmp/-Wl,--export-dynamic -fopenmp -lreadline -lpcre/' Makeconf
make -j8
make install