R package "rgdal" can not be installed
Categories:
Troubleshooting R Package 'rgdal' Installation Issues

A comprehensive guide to resolving common installation problems for the 'rgdal' package in R, covering dependencies, system configurations, and best practices.
The rgdal
package is a crucial component in the R ecosystem for working with geospatial data, providing bindings to the GDAL (Geospatial Data Abstraction Library) and PROJ (Cartographic Projections and Coordinate Transformations Library) libraries. However, its installation can often be challenging due to complex system dependencies, particularly on Linux and macOS. This article will guide you through the common pitfalls and provide solutions to successfully install rgdal
.
Understanding 'rgdal' Dependencies
The primary reason for rgdal
installation failures stems from its reliance on external system libraries: GDAL and PROJ. These libraries handle the heavy lifting of reading/writing various geospatial formats and performing coordinate transformations. If these libraries are not correctly installed or configured on your system, R's install.packages("rgdal")
command will fail, often with cryptic error messages related to missing headers or libraries.
flowchart TD A[Start R Package Installation] --> B{Attempt to install 'rgdal'}; B --> C{Check for GDAL/PROJ Libraries}; C -- Libraries Found --> D[Compile 'rgdal' Package]; C -- Libraries NOT Found --> E[Installation Fails]; E --> F[User Troubleshoots Dependencies]; F --> G{Install/Configure GDAL/PROJ}; G --> B; D --> H[Installation Successful];
Flowchart of 'rgdal' installation process and common failure point.
Pre-installation Steps: Installing GDAL and PROJ
Before attempting to install rgdal
in R, you must ensure that GDAL and PROJ are properly installed on your operating system. The method varies significantly between operating systems.
gdal-devel
, proj-devel
) if available, as rgdal
often requires header files for compilation.Ubuntu/Debian
sudo apt update
sudo apt install libgdal-dev libproj-dev
Fedora/CentOS/RHEL
sudo dnf install gdal-devel proj-devel # For Fedora
sudo yum install gdal-devel proj-devel # For CentOS/RHEL
macOS (Homebrew)
brew update
brew install gdal proj
Windows
On Windows, rgdal
binaries are usually available on CRAN. If not, manual installation of GDAL/PROJ is complex and often involves using OSGeo4W or Rtools. It's generally recommended to use the pre-compiled binaries if possible.
Installing 'rgdal' in R
Once GDAL and PROJ are successfully installed on your system, you can proceed with installing rgdal
in R. It's often beneficial to install sf
first, as rgdal
is being retired in favor of sf
for many operations, and sf
also depends on GDAL/PROJ, providing a good test of your system setup.
install.packages("sf") # Recommended to install sf first
install.packages("rgdal")
Standard R commands to install 'sf' and 'rgdal' packages.
GDAL_DATA
or PROJ_LIB
if the libraries are installed in non-standard locations.Common Error Messages and Solutions
Here are some common error messages you might encounter and how to address them:
configure: error: gdal-config not found or not executable.
configure: error: PROJ: proj_api.h not found in standard locations.
Typical error messages indicating missing GDAL/PROJ dependencies.
These errors directly indicate that the R installation process cannot locate the necessary GDAL or PROJ development files. Revisit the pre-installation steps for your operating system and ensure that the gdal-devel
and proj-devel
(or equivalent) packages are installed. On macOS, ensure Homebrew's paths are correctly configured in your shell environment.
1. Verify GDAL/PROJ Installation
Open your terminal and run gdal-config --version
and proj --version
. If these commands fail or return unexpected versions, your system-level installation is incorrect.
2. Check R's Environment Variables
Sometimes R needs to be explicitly told where to find GDAL/PROJ. You can set environment variables in your ~/.Renviron
file or before starting R. For example:
GDAL_DATA=/usr/share/gdal
PROJ_LIB=/usr/share/proj
(Paths may vary based on your OS and installation method).
3. Clean and Reinstall
If previous attempts failed, try removing any partially installed rgdal
packages and then reinstalling. In R:
remove.packages("rgdal")
install.packages("rgdal")