Installing NetCDF-Fortran using Intel oneAPI on Ubuntu 22.04 on WSL2

Table of Contents

November 5, 2023: Installation environment is updated.


It is assumed that Ubuntu 22.04LTS is installed on WSL2 on Windows 11 Pro; Intel oneAPI is installed as well. 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 introduec here in Japanese, but it is time consuming.

Install GNU libraries with apt

HDF5 (referenced here) and GNU version of NetCDF-C (libnetcdf-dev) and NetCDF-Fortran (libnetcdff-dev, refer to here) 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.8.1 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
$ tar xf netcdf-fortran-4.6.1.tar.gz
$ cd netcdf-fortran-4.6.1/

Create a text file 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.

export CDFROOT="/usr"
export LDFLAGS="-L${CDFROOT}/lib -I${CDFROOT}/include":
export OPTIM="-O3 -mcmodel=large -fPIC ${LDFLAGS}"
export CC=icc
export CXX=icpc
export FC=ifort
export F77=ifort
export F90=ifort
export CPP='icc -E -mcmodel=large'
export CXXCPP='icpc -E -mcmodel=large'
export CPPFLAGS="-DNDEBUG -DpgiFortran ${LDFLAGS}"
export CFLAGS=" ${OPTIM}"
export CXXFLAGS=" ${OPTIM}"
export FCFLAGS=" ${OPTIM}"
export F77FLAGS=" ${OPTIM}"
export F90FLAGS=" ${OPTIM}"
./configure --prefix=/usr/local/netcdf-ifort/4.6.1 --enable-large-file-tests --with-pic

Append the execution attribute to 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
$ ./
$ make
$ make check
$ sudo make install

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

export PATH="/usr/local/netcdf-ifort/4.6.1/bin:$PATH"
export LD_LIBRARY_PATH="/usr/local/netcdf-ifort/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        -> icc
  --cflags    -> -I/usr/local/netcdf-ifort/4.6.1/include -DNDEBUG -DpgiFortran -L/usr/lib -I/usr/include:

  --fc        -> ifort
  --fflags    -> -I/usr/local/netcdf-ifort/4.6.1/include -I/usr/local/netcdf-ifort/4.6.1/include
  --flibs     -> -L/usr/local/netcdf-ifort/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-ifort/4.6.1
  --includedir-> /usr/local/netcdf-ifort/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.