Impossible to install R package 'sf'

Hello, I am trying to deploy in Binder Hub a Jupyter notebook with an R kernel, that makes use of the ‘sf’ R package. When trying to install it, it fails, because one of the dependencies (package ‘units’) cannot be installed.
I discovered this problem is quite common, and on local platforms it could be overcome by installing “units” in this way:

install.packages("units", configure.args = "--with-udunits2-lib=/PHShome/ks38/.local/lib")

but I have no information on how to do this in the Binder environment.
Any hint?

Here are two, untested, approaches that might be worth your time to try:

  • use apt
    • create an apt.txt
    • while the current version is 2.2.28-3 you’ll might have to figure out the version yourself
    • if the versions are wrong, you have to re-commit and re-build, as apt is called very early as root
  • use conda (specifically, conda-forge)
    • create an environment.yml
      - conda-forge
      - nodefaults
       - r-base # whatever version
       - r-irkernel
       # and all your other leaf r- dependencies
       - r-sf
    • the r-sf package depends on r-units depends on udunits2
      • you might still need to specify a version or two
      • if the versions are wrong, you might be able to mamba install different versions of the above packages, and restart your kernel, as the joyvan user (that also runs the kernel, terminals, etc) can change that environment

Thanks for your indications.

I tried to follow the first approach, as I am not an expert in conda.
I created the apt.txt with
(following the instructions in GitHub - r-quantities/units: Measurement units for R) and now, when I check, library units is available, the answer to library(units) is:

udunits database from /usr/share/xml/udunits/udunits2.xml

I also checked all other dependencies of sf, and they are all available.
Nonetheless, sf is not available. After using


I also tried to install it by using:

install.packages(“sf”,type=“source”,configure.args = “–with-udunits2-include=/usr/share/xml/udunits”)

and also:

install.packages(“sf”,type=“source”,configure.args = " --with-udunits2-lib=/usr/share/")

but I always failed.

not an expert in conda.

welp, on binder, my experience has become:

  • one can become an expert in a bunch of different things
    • and solve all the problems, given enough time
  • or accept you already are a user of conda
    • and solve 90% of the things with a little searching on conda-forge
    • or create unsurmountable problems in the other 10% that cannot be overcome without coordinating efforts between 20 upstream parties

so if you’re dead in the water debugging cran vs apt, trying a little conda-forge might be worth your time to see which partition this lands you in.

following the instructions

Excellent, docs are good! But beware: as suggested, perhaps the versions don’t match. On that page, it says an exact version needed for the current master branch: depending on which version you want (which you should probably pin down, given these issues) the version of that README (or their test infrastructure) at that version might indicates which exact version of the upstream you need.


Once you do get it working, definitely consider adopting the repo2docker well-known file install.R so this all happens during build time and gets cached, as it will save a lot of time and trouble re-building. Solving this in environment.yml will have a similar effect.

but I always failed.

without more log output, we can’t help much, but as not much of a reader of r tea leaves, i can’t guarantee i would know how to interpret them.

The problem seems to be solved.
I checked again on the github site of sf, and found that in Linux not only libudunits2-dev, but also libgdal-dev, libgeos-dev and libproj-dev are required (differently from windows).
So, I added also these packages in the apt.txt and now sf is installed correctly and works.

Thank you for your indications.

Anyway, you are right when you say that I should get experienced in conda. I am since long an R and RStudio user, and only recently I started to move to Anaconda and Python.