I tried a couple of hours to install sf package (on MacOs) by using most of the solutions I encountered on the web but still, it did not work. Let me show, what I did and then what the problem is step by step.

First, I tried the normal thing install.packages("sf") which resulted in the following error.

URL 'https://cran.rstudio.com/bin/macosx/contrib/4.1/sf_1.0-2.tgz' deneniyorContent type 'application/x-gzip' length 93022013 bytes (88.7 MB)====================================downloaded 65.3 MBWarning in install.packages :downloaded length 68510897 != reported length 93022013Warning in install.packages :URL 'https://cran.rstudio.com/bin/macosx/contrib/4.1/sf_1.0-2.tgz': Timeout of 60 seconds was reachedError in download.file(url, destfile, method, mode = "wb", ...) : download from 'https://cran.rstudio.com/bin/macosx/contrib/4.1/sf_1.0-2.tgz' failedWarning in install.packages :download of package ‘sf’ failed

Following that, I tried to comply with the instructions in this website https://github.com/r-spatial/sf. To this end, I first installed home brew and then rendered these two codes on the MacOS terminal.

brew install pkg-config brew install gdal

And, finally, in line with the website's suggestion, I run this code to install rgdal package to get sf installed.

install.packages("rgdal", configure.args = c("--with-proj-lib=/usr/local/lib/", "--with-proj-include=/usr/local/include/"))

However, it came with the following error.

 There is a binary version available but the source version is later:binary source needs_compilationrgdal 1.5-26 1.5-27 TRUEDo you want to install from sources the package which needs compilation? (Yes/no/cancel) Yesinstalling the source package ‘rgdal’URL 'https://cran.rstudio.com/src/contrib/rgdal_1.5-27.tar.gz' deneniyorContent type 'application/x-gzip' length 4391149 bytes (4.2 MB)==================================================downloaded 4.2 MB* installing *source* package ‘rgdal’ ...** package ‘rgdal’ successfully unpacked and MD5 sums checked** using staged installationconfigure: R_HOME: /Library/Frameworks/R.framework/Resourcesconfigure: CC: clang -mmacosx-version-min=10.13configure: CXX: clang++ -mmacosx-version-min=10.13 -std=gnu++14configure: CFLAGS: -Wall -g -O2configure: CPPFLAGS: -I/usr/local/includeconfigure: CXXFLAGS: -Wall -g -O2configure: LDFLAGS: -L/usr/local/libconfigure: LDFLAGS: -L/usr/local/libconfigure: CXX11 is: clang++ -mmacosx-version-min=10.13, CXX11STD is: -std=gnu++11configure: CXX is: clang++ -mmacosx-version-min=10.13 -std=gnu++11configure: C++11 support availableconfigure: rgdal: 1.5-27checking for /usr/bin/svnversion... noconfigure: svn revision: 1148checking for gdal-config... nonoconfigure: error: gdal-config not found or not executable.ERROR: configuration failed for package ‘rgdal’* removing ‘/Library/Frameworks/R.framework/Versions/4.1/Resources/library/rgdal’Warning in install.packages :installation of package ‘rgdal’ had non-zero exit statusThe downloaded source packages are in‘/private/var/folders/tc/g15hmz9s0hg1nxmh7_4mw9lm0000gn/T/RtmplfPV9h/downloaded_packages’

As suggested in this website https://github.com/r-spatial/sf, I alternatively tried the solutions in https://github.com/r-spatial/sf/issues/1536#issuecomment-727342736.

Yet, in the part 2, when I run this code chunk,

install.packages("rgeos", repos="http://R-Forge.R-project.org", type="source")install.packages("rgdal", repos="http://R-Forge.R-project.org", type="source")library(devtools)install_github("r-spatial/sf", configure.args = "--with-proj-lib=/usr/local/lib/")

A similar problem emerged for all these codes. For instance, for the first one, the error was;

URL 'http://R-Forge.R-project.org/src/contrib/rgeos_0.5-8.tar.gz' deneniyorContent type 'application/x-gzip' length 276303 bytes (269 KB)==================================================downloaded 269 KB* installing *source* package ‘rgeos’ ...** using staged installationconfigure: CC: clang -mmacosx-version-min=10.13configure: CXX: clang++ -mmacosx-version-min=10.13 -std=gnu++14configure: rgeos: 0.5-7checking for /usr/bin/svnversion... nocat: inst/SVN_VERSION: No such file or directoryconfigure: svn revision: checking for geos-config... nonoconfigure: error: geos-config not found or not executable.ERROR: configuration failed for package ‘rgeos’* removing ‘/Library/Frameworks/R.framework/Versions/4.1/Resources/library/rgeos’Warning in install.packages :installation of package ‘rgeos’ had non-zero exit statusThe downloaded source packages are in‘/private/var/folders/tc/g15hmz9s0hg1nxmh7_4mw9lm0000gn/T/RtmplfPV9h/downloaded_packages’

And lastly, I followed the instructions to solve this problem on this website; trouble installing "sf" due to "gdal"

First: gdalinfo --version code resulted in GDAL 3.3.2, released 2021/09/01Second, I did brew unlink gdal, yet when I run brew link --force gdal2, it did not work. (I am a new user so I assumed it might be because of different versions but when I tried brew link --force gdal3.3.2, it gave me the error Error: No such keg: /opt/homebrew/Cellar/gdal3.3.2.

So, if you can help me how to install sf on MacOS, I will be very glad for that.

2

Best Answer


The error message lines "Warning in install.packages : downloaded length 68510897 != reported length 93022013" and "Timeout of 60 seconds was reached" suggests the package didn't download properly - this can occur when it takes longer than 60 seconds to download the package to your computer for installation i.e. the "timeout" of 60 seconds is reached.

One solution (the solution that worked in this case) is to increase the amount of time R will wait to download the package by running options(timeout = 1200) before running install.packages("sf")

If you need to build packages from source (with or without openMP) on an M1 mac, I believe the instructions posted here work: https://stackoverflow.com/a/68275558/12957340

Not sure if this is helpful, but I was having similar trouble getting this installed on a fresh Mac. I ended up installing XQuartz (which I didn't have), downloading homebrew to get PROJ, and then reinstalling R (50/50 as to whether I downloaded the right version for my M1 Mac) and got everything installed properly.