Skip to content

Instantly share code, notes, and snippets.

@MHBalsmeier
Last active October 9, 2024 20:07
Show Gist options
  • Save MHBalsmeier/a01ad4e07ecf467c90fad2ac7719844a to your computer and use it in GitHub Desktop.
Save MHBalsmeier/a01ad4e07ecf467c90fad2ac7719844a to your computer and use it in GitHub Desktop.
How to install Eccodes on Ubuntu

How to Install Eccodes on Ubuntu

Eccodes is an open source library made by ECMWF for reading and writing grib files, which is the most common file format for meteorological und oceanographic data in operational use (while in research, Netcdf is mainly used). If one wants to work with grib files seriously, one will have to install it earlier or later. On top of that, one will also have to make sure C and Python code is able to import the library's functionality.

First of all, I would highly recommend not to use anything else than Linux, preferably Ubuntu, for working with meteorological data, especially grib files. If you do not want to migrate to Linux completely, consider either a dual boot or a virtual machine.

I just show you the commands with minimal explanation.

Installing an older version with apt

It is also possible to install an older version of Eccodes with apt:

sudo apt-get install libeccodes-dev

Install this package if you want to use command line tools:

sudo apt-get install libeccodes-tools

Installing the latest version

Preparing the system

In case you want the latest version, you have to install manually from source. You have to install some libraries first:

sudo apt-get install libnetcdff-dev libopenjp2-7-dev gfortran make unzip git cmake wget

Downloading the source code

If you don't already have, create a directory for source builds:

cd && mkdir source_builds

Now, cd into this directory, create a subfolder for the eccodes build and download the source code of eccodes. Replace 2.27.0 with the latest version:

cd source_builds && mkdir eccodes && cd eccodes && wget https://confluence.ecmwf.int/download/attachments/45757960/eccodes-2.27.0-Source.tar.gz?api=v2

Now, untar the code:

tar -xzf eccodes-2.27.0-Source.tar.gz?api=v2

Building

mkdir build && cd build

I have my source builds installed into /usr/src, however this is up to you and maybe not according to conventions or not the best choice for another reason. If you choose something else, remember to adapt the environment variables later on as well.

sudo mkdir /usr/src/eccodes
cmake -DCMAKE_INSTALL_PREFIX=/usr/src/eccodes -DENABLE_JPG=ON ../eccodes-2.27.0-Source
make -j
ctest

All tests need to be passed.

sudo make install

The following commands are needed for being able to use the applications in the bin folder (grib_dump, grib_ls, ...) from the command line.

sudo cp -r /usr/src/eccodes/bin/* /usr/bin

Setting environment variables

This is only required if you install eccodes for the first time.

echo 'export ECCODES_DIR=/usr/src/eccodes' >> ~/.bashrc
echo 'export ECCODES_DEFINITION_PATH=/usr/src/eccodes/share/eccodes/definitions' >> ~/.bashrc
source ~/.bashrc

Copying shared libraries and header files to their standard locations

sudo cp $ECCODES_DIR/lib/libeccodes.so /usr/lib
sudo cp /usr/src/eccodes/include/* /usr/include/

Installing Python bindings

sudo apt-get install python3-pip
pip3 install eccodes-python
python3 -m eccodes selfcheck

Response should be:

Found: ecCodes v2.27.0.
Your system is ready.

Using in code

For usage in C code, use

-leccodes

as a GCC option. In the source files in which you use eccodes type

#include "eccodes.h"

at the beginning. In Python, just do

import eccodes as ec;

Feel free to ask.

@patrickjwright
Copy link

Yes, agreed that this is a great guide! Two comments:

  • You could make it more clear that the "Installing the Debian package" section is just if you want an older version (i.e. right now if I run apt search libeccodes-dev the available version is 2.16.0, whereas 2.27.0 is available via source). Because your section headers are all the same, this just seemed like another step to complete in the overall setup. However, upon closer reading, I just skipped this and proceeded directly to "Preparing the system" as I wanted the latest version. Perhaps you could arrange this with two higher level headers, "Install older version debian package with apt" and "Install latest version". Then "Preparing the system" and all subsequent sections could be subheaders under "Install latest version". I think this would be more clear!
  • The sentence "Replace 2.26.0 with the latest version, look it up here." should be moved up above the preceeding code block, since it applies there too. You also might want to mention to check the version before executing the cmake command in the "Building" section.

Thanks for a great guide!

@MHBalsmeier
Copy link
Author

MHBalsmeier commented Sep 30, 2022

Yes, agreed that this is a great guide! Two comments:

* You could make it more clear that the "Installing the Debian package" section is just if you want an older version (i.e. right now if I run `apt search libeccodes-dev` the available version is 2.16.0, whereas 2.27.0 is available via source). Because your section headers are all the same, this just seemed like another step to complete in the overall setup. However, upon closer reading, I just skipped this and proceeded directly to "Preparing the system" as I wanted the latest version. Perhaps you could arrange this with two higher level headers, "Install older version debian package with apt" and "Install latest version". Then "Preparing the system" and all subsequent sections could be subheaders under "Install latest version". I think this would be more clear!

* The sentence "Replace 2.26.0 with the latest version, look it up [here](https://confluence.ecmwf.int//display/ECC/Releases)." should be moved up above the preceeding code block, since it applies there too. You also might want to mention to check the version before executing the `cmake` command in the "Building" section.

Thanks for a great guide!

Thanks for these suggestions!

@FridayMarch26th
Copy link

Lovey guide. An absolute gem.

@rachtsingh
Copy link

It's worth noting that you can use make -jX where X is the number of cores you want to use. It will speed things up (e.g. make -j8).

@MHBalsmeier
Copy link
Author

It's worth noting that you can use make -jX where X is the number of cores you want to use. It will speed things up (e.g. make -j8).

Thanks for this suggestion, I included make -j.

@psuchuck
Copy link

psuchuck commented Oct 9, 2024

Thank you for this. Extremely helpful. Using conda, instead of pip I used "conda install conda-forge::python-eccodes" for that last step.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment