Installing R on RedHat Linux 7

Pre-requisites

Before we begin, it’s a good idea to install some general purpose tools that will help us out once R is installed.

The first of these dependencies are a bunch of development tools. These are needed because on Linux, packages are generally installed from source, and if they contain any compiled code, will need to ensure that we have the compilers and build tools available.

I take a shortcut here and just install the pre-configured group called ‘Development Tools’.

yum groupinstall -y ‘Development Tools’

This probably installs more than we strictly need, but it works just fine.

Next we’re going to want to install some common dependencies of R packages that may get installed later. This might seem odd, but some R packages can rely on OS level packages, such a C libraries and so on, and if they’re not available, the R package will fail to install.

yum install -y libcurl-devel openssl-devel libxml2-devel

This doesn’t cover everything you might need, but it covers the most common dependencies. You may run into issues during the life of the server, where a user attempts to install an R package, only to have it fail with an error about a missing library. For example the R library ‘rgdal’ will fail to install unless the ‘gdal’ tools are installed on the server. Such situations are not common however and should be dealt with on a case-by-case basis. It’s a good idea here to keep track of anything else that you install like that, so you can do it again at a later point should the need arise.

Installing In a Permissive Environment (EPEL is not restricted)

In some environments installing R on RedHat Enterprise Linux (RHEL) 7 is reasonably straightforward. In these situations, the local security policies are permissive enough to allow access to the Extra Packages for Enterprise Linux repository or EPEL, which contains R as well as a couple of additional dependencies.

Note for CentOS users: Although CentOS is often considered to be identical to RHEL in all but name (and support!), the CentOS team actually make things a little easier by having the EPEL configuration package available from the main repo. The means to configure EPEL you can run a yum install -y epel-release and then you can continue to install R.

After the EPEL repo is enabled (instructions are on the site if you need them), you can install R with the following command:

sudo yum install -y R

You may find that this fails with a dependency error. In that case, try:

yum --enablerepo=rhel-optional install -y R

This temporarily, enables the “RHEL Optional” repo, which should get you going.

One important thing to note here, is that the “RHEL Optional” repo, is sometimes named slightly differently, depending on where you’re running RHEL, for example on AWS it’s called ‘rhui-REGION-rhel-server-optional’.

Naturally, this makes installation somewhat annoying to say the least. Fortunately, you can have a quick poke around in your repo config and figure out the name on your system.

Run the following in your terminal and note the result:

grep -i ^'\[.*optional' /etc/yum.repos.d/*

You should get three results back. The shortest of the three, the one without ‘debug’ or ‘source’ in the name is the one you want and you’ll need the whole part between the square brackets.

/etc/yum.repos.d/redhat-rhui.repo:[rhui-REGION-rhel-server-optional]
/etc/yum.repos.d/redhat-rhui.repo:[rhui-REGION-rhel-server-debug-optional]
/etc/yum.repos.d/redhat-rhui.repo:[rhui-REGION-rhel-server-source-optional]

Now that you have the name of the repo you can try the above command again, this time plugging in the name of the RHEL Optional repo from your system.

yum --enablerepo=YOUR_REPO_NAME install R

Installing in a Restricted Environment (EPEL is not available)

Sadly, the next hurdle you might face is considerably harder to work around. In many corporate environments access to the EPEL is outright prohibited, as it is not a first class member of the RHEL ecosystem. This makes life difficult, but not impossible.

In these situations, the best way to get R onto the system is to download it manually from the EPEL, and then follow whatever corporate process you have for getting external software onto your organisation’s network. Unfortunately I can’t help you navigate your corporate security team’s processes, but I can help in getting R, and its remaining EPEL based dependencies.

The names of all the packages you need to obtain are listed below. (Versions correct at the time of writing.)

R-3.4.1-1.el7.x86_64.rpm
R-core-3.4.1-1.el7.x86_64.rpm
R-core-devel-3.4.1-1.el7.x86_64.rpm
R-devel-3.4.1-1.el7.x86_64.rpm
R-java-3.4.1-1.el7.x86_64.rpm
R-java-devel-3.4.1-1.el7.x86_64.rpm
tre-0.8.0-18.20140228gitc2f5d13.el7.x86_64.rpm
tre-common-0.8.0-18.20140228gitc2f5d13.el7.noarch.rpm
tre-devel-0.8.0-18.20140228gitc2f5d13.el7.x86_64.rpm
zvbi-fonts-0.2.35-1.el7.noarch.rpm
openblas-Rblas-0.2.20-3.el7.x86_64.rpm
libRmath-3.4.1-1.el7.x86_64.rpm
libRmath-devel-3.4.1-1.el7.x86_64.rpm

It’s a pain to have to download all these files manually though so there’s a script to do it for you at the end of this article.

Once the packages have been obtained, and landed in your network (hopefully not behind the backs of your security team!) they must be transferred to the server. How you do this will vary from one company to the next. Sometimes your security team will put them on the server for you. Sometimes they’ll be on some central artifact server, where you can then download them to the target server, and other times you’ll need to transfer them over to the server yourself with an SFTP client.

However you get them there, once they’re on the server you can install them by first changing into the directory where the RPM files have been landed, and issuing the following command:

yum install -y *.rpm

This will install all the RPM files in the directory. If this fails with dependency errors, you can try again with the ‘RHEL Optional’ repo temporarily enabled, like so:

yum --enablerepo=rhel-optional install -y R

If it fails again at this point, it likely the the specified name for the RHEL Optional repo isn’t right for your target system. In this case if you refer to the previous section, you’ll find some info on figuring out what it should be on your system.

Once all that’s done, R should be installed and ready to use. Just remember, you’ll have to do it all again if you want to upgrade at some point!

Summary

Installing R on RHEL 7 can be a bit of a pain, particularly in a restricted environment. This is ultimately because RedHat themselves don’t include R in their own repos, which in turn forces us out to the EPEL. Having said that, hopefully this guide has proved useful in getting you up and running with a minimum of fuss. If you run into any issues or spot anything that I’ve missed, let me know!

EPEL Package download script

Finally I promised you a quicker method of getting the RPM’s needed to install R from the EPEL. I use this little R script that I wrote, as it allows me to quickly change the packages that I need to obtain, should the need arise.

Firstly, save the list of RPM’s above in a file called epel_rpms.txt then copy this R script into that same directory and run it. It will create a sub-directory called epel_rpms and download the listed files to there.

epel_base_url <- "https://www.mirrorservice.org/sites/dl.fedoraproject.org/pub/epel/7/x86_64/Packages"

dl_dir <- "./epel_rpms"
if(!dir.exists(dl_dir)){
  dir.create(dl_dir)
}

# build the download url from component pieces
construct_url <- function(rpm){
  first_letter <- tolower(substr(rpm, 1, 1))
  rpm_url <- paste0(epel_base_url, "/", first_letter, "/", rpm)
  return(rpm_url)
}

# read in the rpms to dl
rpms <- readLines("epel_rpms.txt")
lapply(rpms, function(x){download.file(construct_url(x), paste0(dl_dir, "/", x))})

Once the files are downloaded, it’s up to you to figure out how to get them onto the server in your environment. Good luck!