ONEKEY——安装R 4.4.2适用于-rocky8.10
# Rocky 8.10 环境一键安装 R 4.4.2
提示
Rocky 8.10 作为 RHEL8 的完全开源衍生发行版,默认提供 GCC 8+,极适合直接编译 R 4.4.2,无需 Devtoolset!核心依赖推荐直接用 dnf 安装,避免漏包。
系统环境 | Rocky Linux 8.10 |
---|---|
推荐内存 | 2GB+ (建议4G+) |
gcc版本 | >= 8 |
R版本 | 4.4.2 |
Rocky 8 包管理器采用 dnf,部分依赖包名和 CentOS 7 有所不同,尤其是 libjpeg-turbo-devel 和 texlive 相关!
# 安装脚本(推荐直接复制运行)
#!/bin/bash
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Author: JaneTTR
set -ex
echo "############## INSTALL R start #############"
# 定义变量
DOWNLOAD_DIR="/opt/modules"
URL="https://mirrors.tuna.tsinghua.edu.cn/CRAN/src/base/R-4/R-4.4.2.tar.gz"
TAR_FILE="${DOWNLOAD_DIR}/R-4.4.2.tar.gz"
DIR_NAME="${DOWNLOAD_DIR}/R-4.4.2"
INSTALL_DIR="/usr/local/R-4.4.2"
# 创建下载目录(如果不存在)
if [ ! -d "$DOWNLOAD_DIR" ]; then
sudo mkdir -p "$DOWNLOAD_DIR"
sudo chown $(whoami):$(whoami) "$DOWNLOAD_DIR"
fi
# 检查R是否已经安装
if [ ! -d "$INSTALL_DIR" ]; then
# Rocky 8 适配依赖(用dnf,部分包名不同)
sudo dnf install -y gcc-gfortran \
readline-devel \
libicu-devel \
libXt-devel \
libX11-devel \
libpng-devel \
libjpeg-turbo-devel \
libtiff-devel \
cairo-devel \
pango-devel \
bzip2-devel \
xz-devel \
curl-devel \
freetype-devel \
zlib-devel \
pkgconfig \
make \
which \
tar \
texlive-collection-basic \
texlive-collection-latexrecommended \
texlive-collection-fontsrecommended
# 下载tar.gz文件,如果文件不存在
if [ ! -f "$TAR_FILE" ]; then
curl -o $TAR_FILE $URL
else
echo "$TAR_FILE 已存在,跳过下载。"
fi
# 解压文件,如果目录不存在
if [ ! -d "$DIR_NAME" ]; then
tar -zxvf $TAR_FILE -C $DOWNLOAD_DIR
else
echo "$DIR_NAME 已存在,跳过解压缩。"
fi
# 进入解压后的目录并安装
cd $DIR_NAME
# Rocky8 默认gcc>=8,无需devtoolset
./configure --prefix=$INSTALL_DIR
make -j$(nproc)
sudo make install
# 清理解压的文件(可选)
cd ..
rm -rf $DIR_NAME
else
echo "R 已安装在 $INSTALL_DIR,跳过编译。"
fi
# 检查系统路径是否已包含R安装路径,如果没有则添加到/etc/profile
if ! grep -q "R_HOME" /etc/profile; then
echo "export R_HOME=$INSTALL_DIR" | sudo tee -a /etc/profile
echo 'export PATH=$R_HOME/bin:$PATH' | sudo tee -a /etc/profile
else
echo "系统路径已包含R安装路径,跳过添加。"
fi
source /etc/profile
# 验证安装
R --version
echo "############## INSTALL R end #############"
echo "############## SETUP R_ENV start #############"
# devtoolset-7 不需要,Rocky8自带gcc>=8
# 安装所需的 R 包,处理依赖包问题
REQUIRED_PACKAGES=("knitr" "rmarkdown" "devtools" "e1071" "survival" "httr2" "gh" "htmlwidgets" "usethis" "pkgdown" "profvis" "roxygen2" "testthat")
CRAN_MIRRORS="c('https://mirrors.tuna.tsinghua.edu.cn/CRAN/', 'https://mirrors.ustc.edu.cn/CRAN/', 'https://mirrors.aliyun.com/CRAN/')"
for package in "${REQUIRED_PACKAGES[@]}"; do
Rscript -e "options(repos = $CRAN_MIRRORS); if (!requireNamespace('$package', quietly = TRUE)) tryCatch(install.packages('$package'), error = function(e) { cat('Error installing package:', '$package', '\n') })"
done
echo "所需的 R 包已安装完成。"
echo "############## SETUP R_ENV end #############"
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# 常见问题与补充说明
注意
依赖无法自动拉取/编译报错? 建议提前配置好本地 YUM 仓库,或将安装包离线缓存,尤其是在公司/教育网环境下,外网镜像经常会超时或失败!
- gcc 版本:Rocky8 默认自带,不需 devtoolset。
- libjpeg-turbo-devel:与 CentOS 7 的 libjpeg-devel 不同,需特别注意依赖包名。
- TeX 依赖:推荐 texlive-collection-latexrecommended,实际最小需求可按业务裁剪。
提示
如遇 R 包安装速度慢、下载失败,推荐换用 TUNA/USTC/阿里云等国内 CRAN 镜像。
环境变量配置、后续维护方式建议直接参考脚本内的 /etc/profile
自动追加方式。