25

The systems I work with have GCC 4.5 (experimental) in /usr/local/bin/gcc which has proven to be problematic for some R packages. I would like to instead use system GCC in /usr/bin/gcc.

I have tried setting CC and CXX in the Bash configuration files (.bashrc, .bash_profile etc.) as well as on the command line, but although Bash recognizes the change, R does not.

How can I get R to use the version of GCC in /usr/bin instead of the one in /usr/local/bin/?

Ryan R. Rosario
  • 5,114
  • 9
  • 41
  • 56

4 Answers4

43

This is not that well documented (e.g. I failed to locate it in either 'R Extension' or 'R Admin' right now) but Brian Ripley mentioned it a few times on the lists.

Basically, at R compile time, settings are registered and the stored in $R_HOME/etc/Makeconf. One possibility is to edit that file directly, but you may not have root privileges or may not want to affect all other users. So the better may be to create

~/.R/Makevars

with entries

CC=gcc-4.4
CXX=g++-4.4

plus whichever optmisation flags etc you want to set. That will the affect all subsequent uses of R CMD INSTALL or R CMD check or ... that you run.

Other files in $R_HOME/etc/ can similarly be overridden locally from ~/.R/.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • With my configuration, certain packages require sudo, so in those cases it is necessary to modify Makeconf. Your solution works! – Ryan R. Rosario Oct 29 '09 at 02:55
  • 2
    $R_HOME is /usr/lib64/R/ for CentOS6 – zakrapovic Apr 12 '16 at 13:04
  • 1
    For a R package (largeViz) installation, we had issues even after adding the CXX variable. In addition to that we had to configure CXX11 variable in the Makevars file. Like this CXX11=g++7 – user131476 Nov 30 '17 at 04:41
  • I'm on a mac, and it won't let me write to a file called .R/Makevars in my ~/ dir. I keep getting: ~/.R/Makevars" E212: Can't open file for writing – lrthistlethwaite Sep 15 '18 at 05:32
  • 2
    Maybe you need `mkdir ~/.R` first? The directory needs to exists before you can write a file in it. – Dirk Eddelbuettel Sep 15 '18 at 12:49
  • 1
    Thanks, this solved my problem, I used `CXX14=/opt/rh/devtoolset-8/root/usr/bin/g++` to install `rstan` package. – Shixiang Wang Sep 20 '19 at 07:44
  • For me, I added `CC=gcc-11` and `CXX=g++-11` on Ubuntu 22.04 and it worked like a charm. Thanks. – Gejun May 24 '22 at 17:36
  • 1
    @Gejun: Those versions `gcc-11` and `g++-11` are the default versions on Ubuntu 22.04 so you should not need to set them. Anyway, we are in the comment section of a 12 1/2 year old question and answer so... However, `~/.R/Makevars` is still _the_ way to make these selections so if it helped you I am glad to hear that. – Dirk Eddelbuettel May 24 '22 at 18:28
  • @DirkEddelbuettel Thanks for the heads up. I guess you are right. Some packages need gcc-5 and g++-5 to compile and I only have the 11 version. Not sure what to do. – Gejun May 24 '22 at 19:30
  • `Makevars` contents are documented in the "Writing R Extension" manual: . I'm not sure where `~/.R/Makevars` specifically is officially documented, but it's mentioned several times in that manual. – shadowtalker Aug 19 '22 at 16:46
4

I had a very similar problem.

What worked for me was to define a project directory (rstudio can do that for you), and then add a .Renviron file that modifies the PATH and LD_LIBRARY_PATH, to include the directory with the new gcc. In your case, for example, the .Renviron will look something like:

LD_LIBRARY_PATH=/usr/local/bin/gcc/lib:/usr/local/bin/gcc/lib64:/usr/local/bin/gcc/libexec:other paths

PATH=/usr/local/bin/gcc/bin:/usr/local/bin:other paths

Gil Hornung
  • 109
  • 4
2

Check your path to see if /usr/local/bin comes before /usr/bin. If it does, just make sure /usr/bin comes first:

PATH=/usr/bin:${PATH}

(it's okay if /usr/bin is duplicated appears twice).

R Samuel Klatchko
  • 74,869
  • 16
  • 134
  • 187
0

Look at configure.args part of ?install.packages and compare this to ./configure --help on e.g. the r source tree.

You can also, from bash, CC=clang R CMD INSTALL /path/to/package/source.

HTH

isomorphismes
  • 8,233
  • 9
  • 59
  • 70