<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.bwhpc.de/wiki/index.php?action=history&amp;feed=atom&amp;title=BwUniCluster3.0%2FSoftware%2FR%2Fterra</id>
	<title>BwUniCluster3.0/Software/R/terra - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.bwhpc.de/wiki/index.php?action=history&amp;feed=atom&amp;title=BwUniCluster3.0%2FSoftware%2FR%2Fterra"/>
	<link rel="alternate" type="text/html" href="https://wiki.bwhpc.de/wiki/index.php?title=BwUniCluster3.0/Software/R/terra&amp;action=history"/>
	<updated>2026-04-24T02:33:10Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.39.17</generator>
	<entry>
		<id>https://wiki.bwhpc.de/wiki/index.php?title=BwUniCluster3.0/Software/R/terra&amp;diff=15519&amp;oldid=prev</id>
		<title>K Kuck: Created page with &quot;&lt;span style=&quot;color:red&quot;&gt;&lt;b&gt;Note that the instructions provided below refer to R 4.5.1!&lt;/b&gt;&lt;/span&gt;  = General information = [https://rspatial.github.io/terra/ &#039;&#039;&#039;terra&#039;&#039;&#039;] is a R package for spatial data analysis with vectors (points, lines, polygons) and raster (grid) data.  [https://github.com/r-spatial/sf &#039;&#039;&#039;sf&#039;&#039;&#039;] is a R package that provides [https://en.wikipedia.org/wiki/Simple_Features simple feature] access for R.  In order to install those, we need to fulfill the...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.bwhpc.de/wiki/index.php?title=BwUniCluster3.0/Software/R/terra&amp;diff=15519&amp;oldid=prev"/>
		<updated>2025-11-25T14:48:18Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;&amp;lt;span style=&amp;quot;color:red&amp;quot;&amp;gt;&amp;lt;b&amp;gt;Note that the instructions provided below refer to R 4.5.1!&amp;lt;/b&amp;gt;&amp;lt;/span&amp;gt;  = General information = [https://rspatial.github.io/terra/ &amp;#039;&amp;#039;&amp;#039;terra&amp;#039;&amp;#039;&amp;#039;] is a R package for spatial data analysis with vectors (points, lines, polygons) and raster (grid) data.  [https://github.com/r-spatial/sf &amp;#039;&amp;#039;&amp;#039;sf&amp;#039;&amp;#039;&amp;#039;] is a R package that provides [https://en.wikipedia.org/wiki/Simple_Features simple feature] access for R.  In order to install those, we need to fulfill the...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&amp;lt;span style=&amp;quot;color:red&amp;quot;&amp;gt;&amp;lt;b&amp;gt;Note that the instructions provided below refer to R 4.5.1!&amp;lt;/b&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= General information =&lt;br /&gt;
[https://rspatial.github.io/terra/ &amp;#039;&amp;#039;&amp;#039;terra&amp;#039;&amp;#039;&amp;#039;] is a R package for spatial data analysis with vectors (points, lines, polygons) and raster (grid) data.&lt;br /&gt;
&lt;br /&gt;
[https://github.com/r-spatial/sf &amp;#039;&amp;#039;&amp;#039;sf&amp;#039;&amp;#039;&amp;#039;] is a R package that provides [https://en.wikipedia.org/wiki/Simple_Features simple feature] access for R.&lt;br /&gt;
&lt;br /&gt;
In order to install those, we need to fulfill the following [https://cran.r-project.org/web/packages/terra/index.html system requirements]:&lt;br /&gt;
* [https://gdal.org/ GDAL] 2.2.3 or higher&lt;br /&gt;
* [https://proj.org/ PROJ] 4.9.3 or higher&lt;br /&gt;
* [https://libgeos.org/ GEOS] 3.4.0 or higher&lt;br /&gt;
&lt;br /&gt;
These packages are not available centrally on the cluster, but can be installed manually (to the &amp;lt;code&amp;gt;$HOME&amp;lt;/code&amp;gt;-directory). Specifically, to install these packages, they need to be built from source.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Installation =&lt;br /&gt;
&lt;br /&gt;
Please enter (or copy &amp;amp; paste) the code, presented in the boxes below, directly into your shell/command line on bwUniCluster. The whole process will take approximately 45 minutes.&lt;br /&gt;
&lt;br /&gt;
First, for compilation we obtain an interactive session with multiple cores (on a compute node):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Obtain interactive session&lt;br /&gt;
salloc -n 8 -t 60 -p cpu&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Preparations ==&lt;br /&gt;
Prepare an &amp;lt;code&amp;gt;.R/Makevars&amp;lt;/code&amp;gt; file (if it does not already exist). This file specifies how R should compile the packages (i.e., sets some &amp;#039;compiler flags&amp;#039;).&lt;br /&gt;
&lt;br /&gt;
If an &amp;lt;code&amp;gt;.R/Makevars&amp;lt;/code&amp;gt; file is present in your home directory (&amp;lt;code&amp;gt;$HOME&amp;lt;/code&amp;gt;), check whether the flags displayed below are already set and apply adjustments, if necessary:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cat $HOME/.R/Makevars&lt;br /&gt;
&lt;br /&gt;
CXX14=g++&lt;br /&gt;
CXX17=g++&lt;br /&gt;
CFLAGS+=-O3 -fPIC -malign-data=cacheline -finline-functions -m64 -Wno-implicit-function-declaration&lt;br /&gt;
CXXFLAGS+=-O3 -fPIC -malign-data=cacheline -finline-functions -m64 -Wno-implicit-function-declaration&lt;br /&gt;
CXX14FLAGS += -std=c++14&lt;br /&gt;
CXX17FLAGS += -std=c++17 -fPIC&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Please run the following lines of code to set the flags, if necessary:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
mkdir -p ~/.R&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot;CXX14=g++&amp;quot; &amp;gt; $HOME/.R/Makevars&lt;br /&gt;
echo &amp;quot;CXX17=g++&amp;quot; &amp;gt;&amp;gt; $HOME/.R/Makevars&lt;br /&gt;
echo &amp;quot;CFLAGS+=-O3 -fPIC -malign-data=cacheline -finline-functions -m64 -Wno-implicit-function-declaration&amp;quot; &amp;gt;&amp;gt; $HOME/.R/Makevars&lt;br /&gt;
echo &amp;quot;CXXFLAGS+=-O3 -fPIC -malign-data=cacheline -finline-functions -m64 -Wno-implicit-function-declaration&amp;quot; &amp;gt;&amp;gt; $HOME/.R/Makevars&lt;br /&gt;
echo &amp;quot;CXX14FLAGS += -std=c++14&amp;quot; &amp;gt;&amp;gt; $HOME/.R/Makevars&lt;br /&gt;
echo &amp;quot;CXX17FLAGS += -std=c++17 -fPIC&amp;quot; &amp;gt;&amp;gt; $HOME/.R/Makevars&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Next, we create the directories for the source code and installation targets, respectively. Furthermore, we load all (software) modules relevant for compilation and ensure that the compilers are found:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# We install the libraries into the ~/sw/R directory.&lt;br /&gt;
mkdir -p ~/sw/R&lt;br /&gt;
&lt;br /&gt;
# Source directory.&lt;br /&gt;
mkdir -p ~/src&lt;br /&gt;
&lt;br /&gt;
# Load required modules.&lt;br /&gt;
module purge&lt;br /&gt;
module load devel/python/3.13.3-gnu-14.2&lt;br /&gt;
&lt;br /&gt;
# Check that the GNU compiler 14.2 is loaded.&lt;br /&gt;
gcc --version&lt;br /&gt;
&lt;br /&gt;
# Set compiler for cmake and make.&lt;br /&gt;
export CC=$(which gcc)&lt;br /&gt;
export CXX=$(which g++)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since the Python 3.13.3 module is missing some required packages, we install them in a virtual environment in &amp;lt;code&amp;gt;$HOME&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# We create a virtual environment &amp;quot;terra&amp;quot; for additional dependencies&lt;br /&gt;
python3.13 -m venv terra&lt;br /&gt;
source terra/bin/activate&lt;br /&gt;
python -m pip install --upgrade pip&lt;br /&gt;
pip install numpy setuptools ruamel.yaml&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Install external programs ==&lt;br /&gt;
&lt;br /&gt;
First, we download the sources of GDAL, PROJ, GEOS (and their dependencies) and install them:&lt;br /&gt;
&lt;br /&gt;
=== Install PROJ ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Download and unpack the PROJ source code:&lt;br /&gt;
PROJ_VER=9.7.0&lt;br /&gt;
cd $HOME/src&lt;br /&gt;
wget http://download.osgeo.org/proj/proj-$PROJ_VER.tar.gz&lt;br /&gt;
tar xf proj-$PROJ_VER.tar.gz&lt;br /&gt;
&lt;br /&gt;
cd proj-$PROJ_VER&lt;br /&gt;
mkdir build &amp;amp;&amp;amp; cd build&lt;br /&gt;
&lt;br /&gt;
# Compile and install PROJ:&lt;br /&gt;
cmake -DCMAKE_INSTALL_PREFIX=&amp;quot;$HOME/sw/R&amp;quot; ..&lt;br /&gt;
cmake --build . -j 8&lt;br /&gt;
cmake --build . --target install&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Install GDAL ===&lt;br /&gt;
Building GDAL requires newer versions of &amp;lt;code&amp;gt;OpenEXR&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;libdeflate&amp;lt;/code&amp;gt; then available on the system.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Download and unpack the libdeflate source code:&lt;br /&gt;
cd $HOME/src&lt;br /&gt;
git clone https://github.com/ebiggers/libdeflate&lt;br /&gt;
&lt;br /&gt;
cd libdeflate&lt;br /&gt;
mkdir build &amp;amp;&amp;amp; cd build&lt;br /&gt;
&lt;br /&gt;
# Compile libdeflate&lt;br /&gt;
cmake -DCMAKE_INSTALL_PREFIX=&amp;quot;$HOME/sw/R&amp;quot; \&lt;br /&gt;
      -DCMAKE_PREFIX_PATH=&amp;quot;$HOME/sw/R&amp;quot; \&lt;br /&gt;
      -DCMAKE_BUILD_TYPE=Release ..&lt;br /&gt;
cmake --build . -j 8&lt;br /&gt;
cmake --build . --target install&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# Download and unpack the OpenEXR source code:&lt;br /&gt;
OPENEXR_VER=3.4.4&lt;br /&gt;
cd $HOME/src&lt;br /&gt;
wget https://github.com/AcademySoftwareFoundation/openexr/releases/download/v$OPENEXR_VER/openexr-$OPENEXR_VER.tar.gz&lt;br /&gt;
tar xf openexr-$OPENEXR_VER.tar.gz&lt;br /&gt;
&lt;br /&gt;
cd openexr-$OPENEXR_VER/&lt;br /&gt;
mkdir build &amp;amp;&amp;amp; cd build&lt;br /&gt;
&lt;br /&gt;
# Compile OpenEXR:&lt;br /&gt;
cmake -DCMAKE_INSTALL_PREFIX=&amp;quot;$HOME/sw/R&amp;quot; -DCMAKE_BUILD_TYPE=Release ..&lt;br /&gt;
cmake --build . -j 8&lt;br /&gt;
cmake --build . --target install&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, all necessary dependencies are available and we can build &amp;lt;code&amp;gt;GDAL&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Download and unpack the GDAL source code:&lt;br /&gt;
cd $HOME/src&lt;br /&gt;
GDAL_VER=3.12.0&lt;br /&gt;
wget http://download.osgeo.org/gdal/$GDAL_VER/gdal-$GDAL_VER.tar.gz&lt;br /&gt;
tar xf gdal-$GDAL_VER.tar.gz&lt;br /&gt;
&lt;br /&gt;
cd gdal-$GDAL_VER&lt;br /&gt;
mkdir build &amp;amp;&amp;amp; cd build&lt;br /&gt;
&lt;br /&gt;
cmake -DCMAKE_INSTALL_PREFIX=&amp;quot;$HOME/sw/R&amp;quot; \&lt;br /&gt;
      -DCMAKE_PREFIX_PATH=&amp;quot;$HOME/sw/R&amp;quot; \&lt;br /&gt;
      -DCMAKE_BUILD_TYPE=Release ..&lt;br /&gt;
cmake --build . -j 8&lt;br /&gt;
cmake --build . --target install&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Install GEOS ===&lt;br /&gt;
&lt;br /&gt;
The last external package that needs to be compiled and installed is &amp;lt;code&amp;gt;GEOS&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Download and unpack the GEOS source code.&lt;br /&gt;
cd $HOME/src&lt;br /&gt;
GEOS_VER=3.14.1&lt;br /&gt;
wget http://download.osgeo.org/geos/geos-$GEOS_VER.tar.bz2&lt;br /&gt;
tar xf geos-$GEOS_VER.tar.bz2&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
cd geos-$GEOS_VER&lt;br /&gt;
mkdir build &amp;amp;&amp;amp; cd build&lt;br /&gt;
&lt;br /&gt;
# Compile GEOS:&lt;br /&gt;
cmake -DCMAKE_INSTALL_PREFIX=&amp;quot;$HOME/sw/R&amp;quot; \&lt;br /&gt;
      -DCMAKE_PREFIX_PATH=&amp;quot;$HOME/sw/R&amp;quot; \&lt;br /&gt;
      -DCMAKE_BUILD_TYPE=Release ..&lt;br /&gt;
make -j 8&lt;br /&gt;
ctest j 8&lt;br /&gt;
make install&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cd $HOME/src&lt;br /&gt;
UDUNITS_VER=2.2.28&lt;br /&gt;
&lt;br /&gt;
wget https://downloads.unidata.ucar.edu/udunits/$UDUNITS_VER/udunits-$UDUNITS_VER.tar.gz&lt;br /&gt;
tar -xzf udunits-$UDUNITS_VER.tar.gz&lt;br /&gt;
cd udunits-$UDUNITS_VER&lt;br /&gt;
&lt;br /&gt;
./configure --prefix=&amp;quot;$HOME/sw/R&amp;quot;&lt;br /&gt;
make -j 8&lt;br /&gt;
make check -j 8&lt;br /&gt;
make install&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Install the R packages ==&lt;br /&gt;
&lt;br /&gt;
In order to install the two R packages, we need to inform R where to find &amp;lt;code&amp;gt;PROJ&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;GDAL&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;GEOS&amp;lt;/code&amp;gt;, so we export the necessary paths:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
export LD_LIBRARY_PATH=$HOME/sw/R/lib64:$HOME/sw/R/lib:$LD_LIBRARY_PATH&lt;br /&gt;
export PATH=$PATH:$HOME/sw/R/bin&lt;br /&gt;
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:$HOME/sw/R/lib64/pkgconfig&lt;br /&gt;
export GDAL_DATA=$HOME/sw/R/share/gdal&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Additionally, the R package installation features compilation of built-in C++ code, for which we specify compilation options (&amp;#039;compiler flags&amp;#039;)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
export CFLAGS+=-I$HOME/sw/R/include&lt;br /&gt;
#export CXXFLAGS+=-I$HOME/sw/R/include&lt;br /&gt;
export UDUNITS2_INCLUDE=$HOME/sw/R/include&lt;br /&gt;
export UDUNITS2_LIBS=$HOME/sw/R/lib&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now, we install &amp;lt;code&amp;gt;terra&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;sf&amp;lt;/code&amp;gt; from within &amp;lt;code&amp;gt;R&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module load math/R&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
R -q&lt;br /&gt;
R&amp;gt; install.packages(&amp;quot;terra&amp;quot;)&lt;br /&gt;
R&amp;gt; library(terra)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
R&amp;gt; install.packages(&amp;quot;sf&amp;quot;)&lt;br /&gt;
R&amp;gt; install.packages(&amp;quot;sf&amp;quot;, type = &amp;quot;source&amp;quot;, configure.args = c(&amp;quot;--with-udunits2-include=$HOME/sw/R/include&amp;quot;, &amp;quot;--with-udunits2-lib=$HOME/sw/R/lib&amp;quot;))&lt;br /&gt;
R&amp;gt; library(sf)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Preparations to use the terra and sf packages =&lt;br /&gt;
Since &amp;lt;code&amp;gt;terra&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;sf&amp;lt;/code&amp;gt; depend on the external programs we installed, several environment variables have to be set before using the packages to allow R to address these programs.&lt;br /&gt;
&lt;br /&gt;
We recommend to add the export commands&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
export LD_LIBRARY_PATH=$HOME/sw/R/lib:$HOME/sw/R/lib64:$LD_LIBRARY_PATH&lt;br /&gt;
export PATH=$PATH:$HOME/sw/R/bin&lt;br /&gt;
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:$HOME/sw/R/lib64/pkgconfig&lt;br /&gt;
export GDAL_DATA=$HOME/sw/R/share/gdal&lt;br /&gt;
export UDUNITS2_LIBS=$HOME/sw/R/lib&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
to your [[BwUniCluster_2.0_Slurm_common_Features#sbatch_Examples | batch job scripts]] that use &amp;lt;code&amp;gt;terra&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;sf&amp;lt;/code&amp;gt; or to  use them in an [[BwUniCluster_2.0_Batch_Queues | interactive session]].&lt;br /&gt;
&lt;br /&gt;
Furthermore, Python 3.13 and the virtual environment terra (created above) should be loaded:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module load devel/python/3.13.3-gnu-14.2&lt;br /&gt;
source terra/bin/activate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, R can be loaded:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module load math/R/4.5.1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>K Kuck</name></author>
	</entry>
</feed>