Installing NetCDF-Fortran using Intel oneAPI on WSL2 Ubuntu 24.04

Table of Contents

May 20, 2025: Installation environment is updated.

Prerequisites

It is assumed that Ubuntu 24.04L is installed on WSL2 on Windows 11 Home; Intel oneAPI is installed as well. However, due to updates to oneAPI, verification is being conducted with oneAPI-2025.1 installed. In oneAPI-2025.1, ifort and icc have been removed, and ifx and ifc are used instead.
Builds NetCDF-Fortran, which is often required for Fortran-based models such as FVCOM. Referring to this article, libraries other than NetCDF-Fortran are easily installed with apt (compiled with GNU). How to compile NetCDF-C with Intel oneAPI is introduced here in Japanese, but it is time consuming.

Install GNU libraries with apt

HDF5 (referenced here) and GNU version of NetCDF-C (libnetcdf-dev)(https://cloud-gc.readthedocs.io/en/stable/chapter04_developer-guide/install-basic.html)) are installed as follows. libnetcdff-dev should not be installed as building Intel version failed.

sudo apt update
sudo apt install hdf5-tools hdf5-helpers libhdf5-dev libhdf5-doc libhdf5-serial-dev
sudo apt install libnetcdf-dev

Check whether the installation was successful as follows:

$ nc-config --all

This netCDF 4.9.2 has been built with the following features:
...

Building NetCDF-Fortran using Intel oneAPI

Obtain the source code of NetCDF-Fortran from here. The following is an example of obtaining the source code with wget.

mkdir ~/Downloads
cd ~/Downloads
wget https://downloads.unidata.ucar.edu/netcdf-fortran/4.6.1/netcdf-fortran-4.6.1.tar.gz
tar xf netcdf-fortran-4.6.1.tar.gz
cd netcdf-fortran-4.6.1/

Create a text file config-intel.sh with the following contents in the same directory using an editor. The version on the bottom line should match that of the downloaded source code.

#!/bin/bash
# Building netcdf-fortran using oneAPI ifx
export CDFROOT="/usr"
export LD_LIBRARY_PATH="${CDFROOT}/lib:${LD_LIBRARY_PATH}"
export LDFLAGS="-L${CDFROOT}/lib -I${CDFROOT}/include"
#export OPTIM="-O2 -xHost -mcmodel=large -fPIC ${LDFLAGS}"
export OPTIM="-O2 -xHost -mcmodel=large -fPIC"
#
export CC=icx
export CXX=icpx
export FC=ifx
export F77=ifx
export F90=ifx
export CPP="icx -E -mcmodel=large"
export CXXCPP="icpx -E -mcmodel=large"
#export CPPFLAGS="-DNDEBUG -DpgiFortran ${LDFLAGS}"
export CPPFLAGS="-DNDEBUG"
#
export CFLAGS="${OPTIM}"
export CXXFLAGS="${OPTIM}"
export FCFLAGS="${OPTIM}"
export F77FLAGS="${OPTIM}"
export F90FLAGS="${OPTIM}"
#
./configure --prefix=/usr/local/netcdf-ifx/4.6.1 --enable-large-file-tests --with-pic

Append the execution attribute to config-intel.sh and execute. make check is a test run, which is hard to see, but if it looks like PASS: 2 in green and ERROR: 0 in red, it is successful.

chmod a+x config-intel.sh
./config-intel.sh
make
make check
sudo make install

Add PATH and LD_LIBRARY_PATH to ~/.bashrc as follows:

export PATH="/usr/local/netcdf-ifx/4.6.1/bin:$PATH"
export LD_LIBRARY_PATH="/usr/local/netcdf-ifx/4.6.1/lib:$LD_LIBRARY_PATH"

After re-logging in, run nf-config --all. If the following results are obtained, it is a success.

$ nf-config --all

This netCDF-Fortran 4.6.1 has been built with the following features:

  --cc        -> icx
  --cflags    -> -I/usr/local/netcdf-ifx/4.6.1/include -DNDEBUG

  --fc        -> ifx
  --fflags    -> -I/usr/local/netcdf-ifx/4.6.1/include -I/usr/local/netcdf-ifx/4.6.1/include
  --flibs     -> -L/usr/local/netcdf-ifx/4.6.1/lib -lnetcdff -L/usr/lib -I/usr/include -lnetcdf -lnetcdf -lm
  --has-f90   ->
  --has-f03   -> yes

  --has-nc2   -> yes
  --has-nc4   -> yes

  --prefix    -> /usr/local/netcdf-ifx/4.6.1
  --includedir-> /usr/local/netcdf-ifx/4.6.1/include
  --version   -> netCDF-Fortran 4.6.1

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.