前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >R 在 Linux 等操作系统上的特定版本安装

R 在 Linux 等操作系统上的特定版本安装

原创
作者头像
叶子Tenney
发布2023-12-27 18:58:15
9710
发布2023-12-27 18:58:15
举报

引言

有些时候会存在需要安装特定版本 R 软件的需求,比如为了满足特定软件包的安装使用要求或减少不同平台迁移成本。但是,不同于 Windows 平台拥有便捷的 R 版本切换功能,MacOS 和 Linux 平台都存在着不同程度的安装和切换困难。因此,本文以 Ubuntu 为例分享一下 R 在 Linux 等操作系统上的特定版本安装和 rstudio-server 中 R 版本的切换。

过程

Linux

Ubuntu

官网提供的安装方法实际只能安装最新版,无法指定安装版本1。而官方提供的旧版本安装方法2直接旧到 3.4 和 3.6 去了...

因此,使用 Posit 提供的 deb 安装方法5,6。

Posit
代码语言:sh
复制
# Install required dependencies
sudo apt-get update
sudo apt-get install gdebi-core

# Specify R version, download and install R
export R_VERSION=4.2.3
curl -O https://cdn.rstudio.com/r/ubuntu-2004/pkgs/r-${R_VERSION}_1_amd64.deb
sudo gdebi r-${R_VERSION}_1_amd64.deb
/opt/R/${R_VERSION}/bin/R --version

# Create a symlink to R
sudo ln -s /opt/R/${R_VERSION}/bin/R /usr/local/bin/R
sudo ln -s /opt/R/${R_VERSION}/bin/Rscript /usr/local/bin/Rscript

# config rstudio-server
vim /etc/rstudio/rserver.conf
# rsession-which-r=/usr/local/bin/R
sudo rstudio-server restart

值得注意的是,很多教程没有分清rserver.confrsession.conf的区别,如果写入了错误的配置文件会导致 rstudio-server 无法启动。事实上 rserver.conf 配置文件控制 Workbench 的 rserver 进程的行为,用来调整身份认证、HTTP 和授权选项等设置8。而 rsession.conf 配置文件被用来调整各种 RStudio Pro Session 参数9,简单的说,高级版才有用。

手动编译

也可以使用手动编译的方法安装7。

代码语言:sh
复制
sudo sed -i.bak "/^#.*deb-src.*universe$/s/^# //g" /etc/apt/sources.list
sudo apt update
sudo apt build-dep r-base

export R_VERSION=4.2.3
curl -O https://cran.rstudio.com/src/base/R-4/R-${R_VERSION}.tar.gz
tar -xzvf R-${R_VERSION}.tar.gz
cd R-${R_VERSION}

./configure \
    --prefix=/opt/R/${R_VERSION} \
    --enable-R-shlib \
    --enable-memory-profiling \
    --with-blas \
    --with-lapack
make
sudo make install

conda

代码语言:sh
复制
wget https://repo.continuum.io/miniconda/Miniconda3-latest-$(uname -s)-$(uname -m).sh
## exemple: wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash ./Miniconda3-latest-$(uname -s)-$(uname -m).sh
## exemple: bash ./Miniconda3-latest-Linux-x86_64.sh
source ~/.bashrc

## Conda-forge
conda config --add channels conda-forge
conda config --set channel_priority strict

## run
conda create -n R4.2
conda env list
conda activate R4.2
conda install r-base=4.2.3
conda install r-languageserver radian

## R
which R
# /root/miniconda3/envs/R4.2/bin/R
ll /root/miniconda3/envs/R4.2/bin/R
# 注意, 在任何情况下都不应该由用户手动写入`/usr/lib`文件夹
# ln -s /root/miniconda3/envs/R4.2/lib/R/bin/R /usr/lib/R
# rm -rf /usr/lib/R

注意,在任何情况下都不应该由用户手动写入/usr/lib文件夹,因此千万不要用创建软连接的形式进行软件的使用。如果已经创建了并引起了 dpkg 和 apt 的报错,就删除创建的软连接。

题外

官方最新版安装

代码语言:sh
复制
# update indices
sudo apt update -qq
# install two helper packages we need
sudo apt install --no-install-recommends software-properties-common dirmngr
# add the signing key (by Michael Rutter) for these repos
# To verify key, run gpg --show-keys /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc
# Fingerprint: E298A3A825C0D65DFD57CBB651716619E084DAB9
wget -qO- https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc | sudo tee -a /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc
# add the R 4.0 repo from CRAN -- adjust 'focal' to 'groovy' or 'bionic' as needed
sudo add-apt-repository "deb https://cloud.r-project.org/bin/linux/ubuntu $(lsb_release -cs)-cran40/"

sudo apt install --no-install-recommends r-base

Win

各种版本的 R 都可以直接下载。

Previous releases of R for Windows

MacOS

可以使用 RSwitch 等软件进行设置。

引用

  1. The Comprehensive R Archive Network
  2. Ubuntu Packages For R - Older Releases
  3. 安装低版本的 R 语言、和自行下载安装各个版本的 R 语言包、以及多环境运行 R_r 官网怎么找旧版本的 r-CSDN 博客
  4. 20.04 - How to install specific R version in ubuntu - Ask Ubuntu
  5. Posit - Install R - Posit Documentation
  6. Rstudio-server 更改 R 版本 - lypbendlf - 博客园
  7. Posit - Install R from Source - Posit Documentation
  8. Administration Guide - rserver.conf
  9. Administration Guide - rsession.conf

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 引言
  • 过程
    • Linux
      • Ubuntu
      • Posit
      • 手动编译
    • conda
    • 题外
      • 官方最新版安装
        • Win
          • MacOS
          • 引用
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档