Can't compile packages when restoring a project with renv

I'm trying to renv::restore() a project from a new computer. I'm running into the following error: ld: library 'gfortran' not found.

I found several posts about the problem and ways to fix it (for example https://stackoverflow.com/questions/77836548/library-gfortran-not-found-when-installing-r-packages, https://stackoverflow.com/questions/35999874/mac-os-x-r-error-ld-warning-directory-not-found-for-option, https://stackoverflow.com/questions/29992066/rcpp-warning-directory-not-found-for-option-l-usr-local-cellar-gfortran-4-8/29993906#29993906). I'm constantly running into new errors though so I'm looking for an easier solution.

Is there a simple fix that doesn't involve creating and managing a Makevars file? I initially installed R with homebrew. I tried reinstalling R from CRAN but I'm still running into the same issues. I'm running on MacOS 15.3.1 and R 4.5.1.

Specifically for gfortran, you can install the relevant compiler tools from here:

(which is discussed at https://mac.r-project.org/).

1 Like

Thank you.

Installing the gfortran compiler allowed me to install the first few packages successfully. I’m still running into new errors every few packages that require making a Makevars file though.

Currently getting:

Error:
! Error installing package 'openssl':
===================================

* installing *source* package ‘openssl’ ...
** this is package ‘openssl’ version ‘2.2.1’
** package ‘openssl’ successfully unpacked and MD5 sums checked
** using staged installation
Homebrew 4.6.15
Using PKG_CFLAGS=-I/opt/homebrew/opt/openssl/include
--------------------------- [ANTICONF] --------------------------------
Configuration failed because openssl was not found. Try installing:
* deb: libssl-dev (Debian, Ubuntu, etc)
* rpm: openssl-devel (Fedora, CentOS, RHEL)
* csw: libssl_dev (Solaris)
* brew: openssl (Mac OSX)
If openssl is already installed, check that 'pkg-config' is in your
PATH and PKG_CONFIG_PATH contains a openssl.pc file. If pkg-config
is unavailable you can set INCLUDE_DIR and LIB_DIR manually via:
R CMD INSTALL --configure-vars='INCLUDE_DIR=... LIB_DIR=...'
-------------------------- [ERROR MESSAGE] ---------------------------
tools/version.c:1:10: fatal error: 'openssl/opensslv.h' file not found
1 | #include <openssl/opensslv.h>
| ^~~~~~~~~~~~~~~~~~~~
1 error generated.
--------------------------------------------------------------------
ERROR: configuration failed for package ‘openssl’

There's a solution described in this SO thread: https://stackoverflow.com/questions/65207249/resolving-openssl-install-error-in-rstudio

Do these errors occur because the installation uses homebrew? If so, is there any way not to use homebrew for this?

I didn't pay attention, but the project I'm trying to restore uses R 4.4.1 when the latest R version I have is 4.5.1.

Restoring the project works when deleting more recent versions of R.

I thought renv would automatically pick the right version of R defined in the settings.json file, but it's not the case. Is this normal or did I miss something?

I've the following line in settings.json:

"r.version": "4.4.1",

and in renv.lock:

"R": {
  "Version": "4.4.1",
  "Repositories": [
    {
      "Name": "CRAN",
      "URL": "https://cran.rstudio.com"
    }
  ]
},

renv activation is triggered through .Rprofile after the R process has already started. At that point, renv itself can’t really choose its own parent process, so the R version must be selected before launching R.

rig knows how to pick R version from renv project and you can use it to launch RStudio or just execute a script in renv project. Positron also uses R version recorded in renv.lock .

Also, Introduction to renv • renv :

  • R version: renv tracks, but doesn’t help with, the version of R used with the project. renv can’t easily help with this because it’s run inside of R, but you might find tools like rig helpful, as they make it easier to switch between multiple version of R on one computer.
1 Like

Thanks, that makes sense.

I actually use Positron, but it doesn't let me switch to a different version of R. This is what I get when trying to use another interpreter. I've both R 4.4.1 and 4.5.1 installed.

I might try rig but I thought it was for R Studio.

From Discovering R Installations – Positron :

On macOS, Positron only supports the system’s current R version and other R installations that are orthogonal.
/../
The notion of orthogonality comes from a tool called rig: The R Installation Manager. We highly recommend rig for managing R installation, especially for macOS users who want to have more than one functioning R installation.

1 Like