Building R-4.0.0 in a side folder

R 4.0 was released over the weekend and immediately I got an email that said one of my packages does not build anymore with R-4.0. The email from R core said I have 2 weeks to bring the package into compliance. The Ubuntu Linux repositories do not have R 4.0 yet, so I built my own. This is something that I've done many times over the years, but I expect many Linux newcomers have not done it. So I'll copy/paste some notes in case it helps.

In all of these details, there are two especially important things to know. First, build R in a separate folder, not in the original source code folder. Second, specify the prefix so that the newly built version of R does not interfere with the previous one that still exists in the /usr file hierarchy.

First, on CRAN, download the file R-4.0.0.tar.gz. Put that in an out-of-the-way folder and decompress (I ran tar xzvf R-4.0.0.tar.gz). That crates a folder R-4.0.0.

cd into that directory and make a new subdirectory called build (mkdir build). We are going to compile R in build in order to prevent the build process from altering the R source code itself. Then cd into build.

The usual process to build GNU software packaged in this traditional style is to run a 3 step process

  1. configure
  2. make
  3. make install

The first thing we do, usually, is to inspect the configure options, with a command like

../configure --help

I want to install my new R-4.0.0 in a separate part of the file system, one where I can read/write as an ordinary user. So the VERY important option is --prefix=/tmp/R. In most softare, it would be sufficient to run this in the build directory:

../configure --prefix=/tmp/R

That would configure the software and so when finishe, it is installed in /tmp/R. However, as luck would have it, I tried that and it failed because the R system tries to write files in /usr/share even after I set the prefix. That was a little unusual.

On Ubuntu, the R packages are built by experts who know what to do. I looked at the file provided with R-3.6.3 called /etc/R/Makeconf to find out what options they used to build R. Their configure statement seems to be quite massive and I played with it for a while. In the end, I decided to take their version,

# R was configured using the following call
# (not including env. vars and site configuration)
# configure  '--prefix=/usr' '--with-cairo' '--with-jpeglib' '--with-readline' '--with-tcltk' '--with-system-bzlib' '--with-system-pcre' '--with-system-zlib' '--mandir=/usr/share/man' '--infodir=/usr/share/info' '--datadir=/usr/share/R/share' '--includedir=/usr/share/R/include' '--with-blas' '--with-lapack' '--enable-R-profiling' '--enable-R-shlib' '--enable-memory-profiling' '--without-recommended-packages' '--build' 'x86_64-linux-gnu' 'build_alias=x86_64-linux-gnu' 'R_PRINTCMD=/usr/bin/lpr' 'R_PAPERSIZE=letter' 'TAR=/bin/tar' 'R_BROWSER=xdg-open' 'LIBnn=lib' 'JAVA_HOME=/usr/lib/jvm/default-java' 'R_SHELL=/bin/bash' 'CC=gcc -std=gnu99' 'CFLAGS=-g -O2 -fdebug-prefix-map=/build/r-base-5i6V25/r-base-3.6.3=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g' 'LDFLAGS=-Wl,-Bsymbolic-functions -Wl,-z,relro' 'CPPFLAGS=' 'FC=gfortran' 'FCFLAGS=-g -O2 -fdebug-prefix-map=/build/r-base-5i6V25/r-base-3.6.3=. -fstack-protector-strong' 'CXX=g++' 'CXXFLAGS=-g -O2 -fdebug-prefix-map=/build/r-base-5i6V25/r-base-3.6.3=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g'

and I trimmed it down to something I could understand. I don't want to use build options that I don't understand. I will trust R's configure script to guess defaults correctly.

../configure '--prefix=/tmp/R' '--with-cairo' '--with-jpeglib' '--with-readline' '--with-tcltk' '--with-system-bzlib' '--with-system-pcre' '--with-system-zlib' '--mandir=/tmp/share/man' '--infodir=/tmp/share/info' '--datadir=/tmp/share/R/share' '--includedir=/tmp/share/R/include' '--with-blas' '--with-lapack' '--enable-R-profiling' '--enable-R-shlib' '--enable-memory-profiling' '--with-recommended-packages'

When that finished, there was a message indicating that I had the required software and that recommended packages would be installed.

Compiling R (with make) is time consuming (perhaps 8-10 minutes one core) and it is faster if you ask make to try to parallelize the build. I allowed 6 cores for that.

make -j6

When that was finished, the last step is to run make install. However, because I diverted the R info and data files to /tmp/share, I needed to prepare for that. I created the folder /tmp/share.

Then the all important installation

make install

Now, when I want to use R-4.0.0, I need to adjust the system path interactively. In the terminal, I run this to put the new R at the FRONT of the path.

export PATH=/tmp/R/bin:$PATH

To use the the system-wide R-3.6, I need to open a different terminal where /usr/bin/ will be at the front of the path.


$ R

R version 4.0.0 (2020-04-24) -- "Arbor Day"
Copyright (C) 2020 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> sessionInfo()
R version 4.0.0 (2020-04-24)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 19.10

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/atlas/
LAPACK: /usr/lib/x86_64-linux-gnu/atlas/

 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C
 [9] LC_ADDRESS=C               LC_TELEPHONE=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

loaded via a namespace (and not attached):
[1] compiler_4.0.0

As soon as the Ubuntu repositories have R-4.0, I'll switch to that version. But the lesson here may still benefit you in the long run. When a package is submitted to CRAN, one must sign an oath that the package installs cleanly on the CURRENT version of R as well as the DEVELOPMENT version of R. Now that you know how to build R from a tar archive, it is not so bothersome to install R-devel and test your package.

About pauljohn

Paul E. Johnson is a Professor of Political Science at the University of Kansas. He is an avid Linux User, an adequate system administrator and C programmer, and humility is one of his greatest strengths.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply