Computational Mathematics

Adventures in Computing, Statistics, and R

Installing rgdal on Mac OS X

19 Comments »

The rgdal package contains bindings from R to the GDAL (Geospatial Data Abstraction Library) as well as the PROJ library. It is a dependency for numerous spatial statistics packages. Note that the binaries on the rgdal homepage are outdated — the most up-to-date version of the rgdal package can be found on CRAN.

Unsurprisingly, rgdal requires both the GDAL and PROJ framework to run. Both frameworks for Mac OS X are kindly maintained by William Kyngesburye and can be found here:

Unix Compatibility Frameworks

I recommend installing GDAL Complete as it contains both required frameworks in a neat little package.
At the time of writing, there are two versions of GDAL Complete:

  • GDAL 1.7 Complete
  • GDAL 1.6 Complete

Both should work for rgdal but note that currently, the stable Mac OS X version of Quantum GIS (QGIS 1.4) only runs with GDAL 1.6, so if you need to use QGIS as well, I would recommend installing GDAL 1.6.  If you’ve installed GDAL 1.7, then you’ll have to use the developer’s release of QGIS.
(May 19, 2011 update: Install GDAL 1.8 Complete as it works for both standard and developer’s release of QGIS)

Next, assuming you installed GDAL above with the default paths, you’ll need to install the R package from source (get package source here) with modified arguments either with install.packages() or through the terminal:

R CMD INSTALL --configure-args='--with-gdal-config=/Library/Frameworks/GDAL.framework/unix/bin/gdal-config --with-proj-include=/Library/Frameworks/PROJ.framework/unix/include --with-proj-lib=/Library/Frameworks/PROJ.framework/unix/lib' rgdal_0.6-33.tar.gz

If you’re getting this error:

Error: package ‘rgdal’ is not installed for ‘arch=i386′

when trying to load the rgdal package, it is because your version of rgdal was not compiled for the 32-bit version of R. You’ll need to run the 64-bit version of R (R64.app on Macs).

You may also need to update the base version of the sp package as well as any other dependencies.

19 Responses

  1. [...] my last post about installing the rgdal R package on Mac OS X, I had apparently glossed over the last step of compiling the R package from source.  I [...]

  2. Thanks for your help with this. I tried following your instructions, but keep getting the error that r cannot find the gdal-config file…? Any help would be appreciated.

    thanks,

    Kevin

    errror in r:
    * installing *source* package ‘rgdal’ …
    gdal-config: gdal-config
    Error: gdal-config not found
    The gdal-config script distributed with GDAL could not be found.
    If you have not installed the GDAL libraries, you can
    download the source from http://www.gdal.org/
    If you have installed the GDAL libraries, then make sure that
    gdal-config is in your path. Try typing gdal-config at a
    shell prompt and see if it runs. If not, use:
    –configure-args=’–with-gdal-config=/usr/local/bin/gdal-config’ echo with appropriate values for your installation.

    ./configure: line 1311: gdal-config: command not found
    ERROR: configuration failed for package ‘rgdal’
    * removing ‘/Library/Frameworks/R.framework/Versions/2.11/Resources/library/rgdal’

    The downloaded packages are in
    ‘/private/var/folders/PG/PG23sp8wH1i567wrrYEJYE+++TI/-Tmp-/RtmpYT3XfE/downloaded_packages’

  3. Sounds like the GDAL library isn’t in your path. You will need to find where gdal-config is located and replace the –with-gdal-config argument with the correct path

  4. I am having a problem with the rgdal installation. That is:

    R CMD INSTALL –configure-args=’–with-gdal-config=/Library/Frameworks/GDAL.framework/unix/bin/gdal-config –with-proj-include=/Library/Frameworks/PROJ.framework/unix/include –with-proj-lib=/Library/Frameworks/PROJ.framework/unix/lib’ rgdal_0.6-28.tar.gz
    * installing to library ‘/Library/Frameworks/R.framework/Resources/library’
    * installing *source* package ‘rgdal’ …
    gdal-config: /Library/Frameworks/GDAL.framework/unix/bin/gdal-config
    checking for gcc… gcc -arch i386 -std=gnu99
    checking for C compiler default output file name… configure: error: C compiler cannot create executables
    See `config.log’ for more details.
    ERROR: configuration failed for package ‘rgdal’
    * removing ‘/Library/Frameworks/R.framework/Resources/library/rgdal’

    Is this a problem of the compiler?

    Thanks for help

  5. I seem to have a fully functional install of rgdal on OS X 10.6.4 after jumping through a few hoops. As best I could tell from piecing together various posts, questions, and errors, as well as jumping back through my command history, the steps are as follows:

    [Note that you'll need to be comfortable with the Terminal. If you're not, then find someone who is.]

    1. Download the GDAL OS X install from kyngchaos
    - http://www.kyngchaos.com/files/software/unixport/GDAL_Complete-1.7.dmg
    (Looks like the basic page [for updates after 1.7 if you're reading this ages from now] is http://www.kyngchaos.com/software/frameworks)
    - Install as per usual OS X install system
    - Fire up the Terminal, then pico (or vi[m]) the .bash_login file
    - Modify the PATH environment so that it reads:
    export PATH=”/Library/Frameworks/GDAL.framework/Programs:$PATH”
    [This is what enables the subsequent steps to find gdal-config]

    2. Download and install proj4 from source
    - http://trac.osgeo.org/proj/wiki/WikiStart#Download
    - Download source code version proj-4.7.0.tar.gz
    - Fire up the Terminal
    > cd ~/Downloads/
    > tar -xzvf proj-4.7.0.tar.gz
    > cd proj-4.7.0
    > ./configure
    > make && make test
    > sudo make install
    [ should install to /usr/local/lib by default]

    3. Download and install rgdal from source
    - http://cran.r-project.org/src/contrib/rgdal_0.6-28.tar.gz
    - Fire up the Terminal
    > cd ~/Downloads/
    > sudo R CMD INSTALL –configure-args=’–with-proj-include=/usr/local/lib’ rgdal_0.6-28.tar.gz

    After all of this mucking about I was able to say:

    > require(sp)
    > require(rgdal)

    And get a message indicating that GDAL was loaded successfully.

    jon

  6. @Jon’s solution is the only one that has worked for me. Note that there’s an R-interface to proj4 (http://www.rforge.net/proj4/) that’s not required by rgdal, but could be useful.

  7. If you have homebrew (http://mxcl.github.com/homebrew/) installed in /usr/local, then it is as easy as:

    brew install gdal
    R CMD INSTALL –configure-args=’–with-proj-include=/usr/local/lib’ rgdal_0.6-28.tar.gz

    thats it.

    then in R I get:

    > require(rgdal)
    Loading required package: rgdal
    Loading required package: sp
    Geospatial Data Abstraction Library extensions to R successfully loaded
    Loaded GDAL runtime: GDAL 1.7.2, released 2010/04/23
    Path to GDAL shared files: /usr/local/Cellar/gdal/1.7.2/share/gdal
    Loaded PROJ.4 runtime: Rel. 4.7.1, 23 September 2009
    Path to PROJ.4 shared files: (autodetected)

    • I tried the same with MacPorts instead of homebrew and it worked very well.

      sudo port install gdal
      R CMD INSTALL –configure-args=’–with-proj-lib=/opt/local/lib’ rgdal_0.7-1.tar.gz

  8. [...] also posted his solution on the Computational Mathematics Blog. If there is a better way I would be interested in hearing about it for future [...]

  9. I tried both of Jon’s approaches (the computational mathematics one and the macports) and got this error:

    sudo R CMD INSTALL –configure-args=’–with-proj-include=/usr/local/lib’ rgdal_0.6-28.tar.gz
    Password:
    Error: evaluation nested too deeply: infinite recursion / options(expressions=)?
    Execution halted

    Any ideas? Thanks!

  10. On Mac OSX 10.7.2, I am having a somewhat different error with:

    R CMD INSTALL –configure-args=”–with-proj-lib=/opt/local/lib” rgdal_0.7-5.tar.gz

    The procedure fails with:

    ** package ‘rgdal’ successfully unpacked and MD5 sums checked
    gdal-config: gdal-config
    checking for gcc… gcc-4.2 -arch x86_64 -std=gnu99
    checking whether the C compiler works… no
    configure: error: in `/private/var/folders/5w/z7w5t04s3ws7k7wsjlptlb1m00010j/T/RtmplCYkLC/R.INSTALL4297aadc/rgdal’:
    configure: error: C compiler cannot create executables

    Any suggestions would be greatly appreciated.

  11. Well, I solved my own problem, but I think it means something is broken somewhere. I added symbolic links mapping gcc-4.2 -> llvm-gcc-4.2 and gxx-4.2 -> llvm-gxx-4.2. Once I did this, everything worked. If this is a bug in the config scripts, who do I report it to?

    • Bill. I am having a similar problem. If I understand correctly, you simply made gcc-4.2 an alias for lllvm-gcc-4.2 (and the same for gxx)? This isn’t working for me but may be I misunderstood your solution.

      • FYI – this is the error I’m receiving.

        ** package ‘rgdal’ successfully unpacked and MD5 sums checked
        gdal-config: gdal-config
        checking for gcc… gcc-4.2 -arch x86_64 -std=gnu99
        checking whether the C compiler works… no
        configure: error: in `/private/tmp/RtmputaNi5/R.INSTALL47921e3a/rgdal’:
        configure: error: C compiler cannot create executables

  12. Phew. I had a lot of trouble this time, but maybe my way, a combination of several tidbits, will help someone.

    First I was following these directions from elsewhere on the web :
    —————————
    1. Download the GDAL OS X install from kyngchaos
    - http://www.kyngchaos.com/files/software/unixport/GDAL_Complete-1.7.dmg
    (Looks like the basic page [for updates after 1.7 if you're reading this ages from now] is http://www.kyngchaos.com/software/frameworks)
    - Install as per usual OS X install system
    - Fire up the Terminal, then pico (or vi[m]) the .bash_login file
    - Modify the PATH environment so that it reads:
    export PATH=”/Library/Frameworks/GDAL.framework/Programs:$PATH”
    [This is what enables the subsequent steps to find gdal-config]

    2. Download and install proj4 from source
    - http://trac.osgeo.org/proj/wiki/WikiStart#Download
    - Download source code version proj-4.7.0.tar.gz
    - Fire up the Terminal
    > cd ~/Downloads/
    > tar -xzvf proj-4.7.0.tar.gz
    > cd proj-4.7.0
    > ./configure
    > make && make test
    > sudo make install
    [ should install to /usr/local/lib by default]
    —————–
    But the third step wouldn’t work for me. The additional configure-args suggested in this blog got me a step further, but then I had the exact same problem as Bill S. Per his recommendation I kluged some symbolic links I thought might help, but I had to add an additional one, for three total:

    sudo ln -s llvm-gcc-4.2 /usr/bin/gcc-4.2
    sudo ln -s llvm-gxx-4.2 /usr/bin/gxx-4.2
    sudo ln -s llvm-g++-4.2 /usr/bin/g++-4.2

    Finally, I reran the the command suggested in this blog and everything worked:
    R CMD INSTALL –configure-args=’–with-gdal-config=/Library/Frameworks/GDAL.framework/unix/bin/gdal-config –with-proj-include=/Library/Frameworks/PROJ.framework/unix/include –with-proj-lib=/Library/Frameworks/PROJ.framework/unix/lib’ ~/Downloads/rgdal_0.7-5.tar.gz

  13. P.S. It may have helped that I first tried installing rgdal through the R gui (search CRAN sources, not binaries). Even though the installation ultimately failed for rgdal, it did install lots of other packages successfully.

  14. Many thanks, Ryan and Bill,
    This saved me. I had the same problem and only needed to provide two of the links:

    sudo ln -s llvm-gcc-4.2 /usr/bin/gcc-4.2
    sudo ln -s llvm-g++-4.2 /usr/bin/g++-4.2

    to get rgdal to compile and install successfully.

    (OS 10.7.3, R 2.14.0, rgdal_0.7-8)

Leave a Reply