简体   繁体   English

如何在 Linux (Red Hat/CentOS) 上更新到特定的 R 版本,保持以前的版本?

[英]How to update to specific R version on Linux (Red Hat/CentOS), keeping the previous version?

Perhaps a more accurate title would be: "How to switch from in-place (EPEL) R to side-by-side (version-specific) R installation on Linux (Red Hat/CentOS)?也许更准确的标题是:“如何在 ZEDC9F0A5A5D57797BF68E373647438 上从就地 (EPEL) R 切换到并排(特定于版本)R 安装?

A (possibly typical) upgrading R on Linux story...一个(可能是典型的)在 Linux 故事上升级 R...

History:历史:

At some point in the past, I updated the version of R on our RHEL/CentOS 7 server using the default version pulled down by the yum package manager at that time.在过去的某个时候,我使用当时由yum package 管理器下拉的默认版本在我们的 RHEL/CentOS 7 服务器上更新了 R 的版本。 For example, sudo yum install R to version 3.5.2 at some point in early 2019. By default, this installs R at /usr/lib64/R for all users and completely replaces the 3.4.x version that had previously been installed there.例如,在 2019 年初的某个时间点sudo yum install R到版本 3.5.2。默认情况下,这会在/usr/lib64/R为所有用户安装 R,并完全替换之前安装的 3.4.x 版本。 Shiny Server was already installed, configured to run as user shiny , and it picked up the new version of R without a hitch. Shiny 服务器已经安装,配置为以用户shiny运行,它顺利地拿起了新版本的 R。

Situation:情况:

A year later, it is now time to bite the bullet and update the version of R that is running on the Linux server.一年后,现在是时候硬着头皮更新在 Linux 服务器上运行的 R 版本了。 Running yum check-upgrade R I find that the version available is 3.6.0.运行yum check-upgrade R发现可用的版本是3.6.0。 I actually want to install 3.6.3 AND I don't want to break all of my apps that are running on 3.5.2, so I need to use a different method.我实际上想安装 3.6.3 并且我不想破坏我在 3.5.2 上运行的所有应用程序,所以我需要使用不同的方法。 Following the instructions found at https://docs.rstudio.com/resources/install-r/ , I download the 3.6.3.rpm file and install it.按照https://docs.rstudio.com/resources/install-r/中的说明下载 3.6.3.rpm 文件并安装它。 By default, this installs R at /opt/R/3.6.3/ , leaving the 3.5.2 version as is.默认情况下,这会将 R 安装在/opt/R/3.6.3/ ,保留 3.5.2 版本。 However, as soon as I complete the Create a symlink to R step, none of my shiny apps work:但是,一旦我完成了Create a symlink to R步骤,我的 shiny 应用程序都不起作用:

sudo ln -s /opt/R/3.6.3/bin/R /usr/local/bin/R  
sudo ln -s /opt/R/3.6.3/bin/Rscript /usr/local/bin/Rscript  

This should not be surprising.这应该不足为奇。 My shiny apps all rely upon several R packages that have not yet been installed for this new version of R.我的 shiny 应用程序都依赖于几个 R 软件包,这些软件包尚未为这个新版本的 R 安装。 I can quickly get my apps working again on the previous version (3.5.2) by removing these symlinks until after I've installed the necessary packages in the new version:通过删除这些符号链接,我可以快速让我的应用程序在以前的版本 (3.5.2) 上再次运行,直到我在新版本中安装了必要的软件包:

sudo rm /usr/local/bin/R  
sudo rm /usr/local/bin/Rscript 

Error messages in my shiny app log files (at /var/log/shiny-server/<app name>-<user>-<datetime>.log ) confirm that the apps had failed to launch due to missing packages.我的 shiny 应用程序日志文件(位于/var/log/shiny-server/<app name>-<user>-<datetime>.log )中的错误消息确认应用程序由于缺少软件包而无法启动。 To update the R packages in the shared library folder, I need to run the new version of R as sudo: sudo -i /opt/R/3.6.3/bin/R and install the necessary packages, eg, install.packages(c("shiny","devtools","rmarkdown","shinydashboard","tidyverse")) in R.要更新共享库文件夹中的 R 包,我需要以 sudo 运行新版本的 R: sudo -i /opt/R/3.6.3/bin/R并安装必要的包,例如install.packages(c("shiny","devtools","rmarkdown","shinydashboard","tidyverse"))在 R 中。

Now that the R packages are installed, I can re-create the symlinks:现在安装了 R 包,我可以重新创建符号链接:

sudo ln -s /opt/R/3.6.3/bin/R /usr/local/bin/R  
sudo ln -s /opt/R/3.6.3/bin/Rscript /usr/local/bin/Rscript  

I verify that my apps are working with the new version of R.我确认我的应用程序正在使用新版本的 R。

Now I have some questions:现在我有一些问题:

Question 1: After completing these steps, R --version still returns the old version (3.5.2).问题1:完成这些步骤后, R --version仍然返回旧版本(3.5.2)。 But when I logged back in the following day, it opens 3.6.3.但是当我第二天重新登录时,它会打开 3.6.3。 Why?为什么? Do I need to run a terminal command to get R --version to return the new version immediately or is opening a new terminal window the only way to achieve this?我是否需要运行终端命令来获取R --version以立即返回新版本,还是打开新终端 window 是实现此目的的唯一方法?

Question 2: Running sudo R --version always returns the old version (3.5.2).问题 2:运行sudo R --version总是返回旧版本(3.5.2)。 Running sudo which R returns /bin/R .运行sudo which R返回/bin/R Running more /bin/R reveals contents which state that it is "Shell wrapper for R executable."运行more /bin/R会显示 state 的内容,它是“R 可执行文件的外壳包装器”。 and has the "/usr/lib64/R" path hard-coded.并具有硬编码的“/usr/lib64/R”路径。 I don't think I need this wrapper at this point.我认为此时我不需要这个包装器。 What is the recommended way to get these sudo commands to point to the new version?让这些 sudo 命令指向新版本的推荐方法是什么?

I can make a backup copy of this file in my home directory (eg, cp /bin/R ~/binR.backup ) just in case, and then:为了以防万一,我可以在我的主目录(例如cp /bin/R ~/binR.backup )中制作这个文件的备份副本,然后:

  • Delete /bin/R?删除 /bin/R?
  • Replace /bin/R with a symlink to the new version (eg, sudo ln -s /opt/R/3.6.3/bin/R /bin/R )?将 /bin/R 替换为新版本的符号链接(例如sudo ln -s /opt/R/3.6.3/bin/R /bin/R )?
  • Re-install the 'old' version into /opt/R/3.5.2/ using a.rpm the same way I installed 3.6.3, install packages there, and then remove the /usr/lib64/R version (eg, sudo yum remove R )?使用 a.rpm 将“旧”版本重新安装到/opt/R/3.5.2/中,就像我安装 3.6.3 一样,在那里安装软件包,然后删除/usr/lib64/R版本(例如, sudo yum remove R )?

Links to similar questions that I looked at but didn't answer my questions:我看过但没有回答我的问题的类似问题的链接:

  1. How to upgrade R in Linux 如何在 Linux 中升级 R
  2. Change path in Linux 更改 Linux 中的路径
  3. How can I load a specific version of R in Linux 如何在 Linux 中加载特定版本的 R

Question 1: I'm not sure why, but having multiple versions of R on your PATH can lead to unexpected situations like this.问题 1:我不知道为什么,但是在 PATH 上有多个版本的 R 可能会导致这样的意外情况。 /usr/local/bin is usually ahead of /usr/bin in the PATH, so I would've expected R 3.6.3 to be found. /usr/local/bin通常在 PATH 中位于/usr/bin之前,所以我预计会找到 R 3.6.3。 Perhaps it has to do with Question 2.也许它与问题2有关。

Question 2: Some distros (like CentOS/RHEL) don't put /usr/local/bin on the PATH by default when using sudo.问题 2:一些发行版(如 CentOS/RHEL)在使用 sudo 时默认不会将/usr/local/bin放在 PATH 上。 See https://unix.stackexchange.com/questions/8646/why-are-path-variables-different-when-running-via-sudo-and-su for details.有关详细信息,请参阅https://unix.stackexchange.com/questions/8646/why-are-path-variables-different-when-running-via-sudo-and-su The answers there describe several ways to add /usr/local/bin to the PATH when using sudo -- for example, modifying secure_path in /etc/sudoers to include /usr/local/bin like:那里的答案描述了几种在使用 sudo 时将/usr/local/bin添加到 PATH 的方法——例如,修改/etc/sudoers中的secure_path以包含/usr/local/bin ,例如:

Defaults    secure_path = /usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

With R 3.6.3 ahead of the default system R in the PATH, you shouldn't have to delete /bin/R or /usr/bin/R . R 3.6.3 在默认系统 R 之前,您不必删除/bin/R/usr/bin/R But eventually, I'd recommend installing multiple side-by-side versions of R the same way, using https://docs.rstudio.com/resources/install-r/ , so it's easier to manage.但最终,我建议使用https://docs.rstudio.com/resources/install以相同的方式安装多个并排版本的 R。 The next time you install a new R version, you can just replace the symlinks in /usr/local/bin .下次安装新的 R 版本时,只需替换/usr/local/bin中的符号链接即可。 The default system R (from EPEL) is meant to be the only R on a system, with in-place upgrades.默认系统 R(来自 EPEL)意味着是系统上唯一的 R,具有就地升级。

If you want to replace the default R 3.5.2 with a side-by-side R 3.5.2 (or 3.5.3), you could install R 3.5 from https://docs.rstudio.com/resources/install-r/ , install all necessary packages, and have Shiny Server use the new R 3.5. If you want to replace the default R 3.5.2 with a side-by-side R 3.5.2 (or 3.5.3), you could install R 3.5 from https://docs.rstudio.com/resources/install-r / ,安装所有必要的软件包,并让 Shiny 服务器使用新的 R 3.5。 Then uninstall the R from EPEL ( R-core or R-core-devel ) to fully switch over.然后从 EPEL( R-coreR-core-devel )卸载 R 以完全切换。 From there, you could even create symlinks to R in /usr/bin instead of /usr/local/bin , and not worry about adding /usr/local/bin to the sudo PATH.从那里,您甚至可以在/usr/bin而不是/usr/local/bin中创建指向 R 的符号链接,而不必担心将/usr/local/bin添加到 sudo PATH。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM