Skip to content

Instantly share code, notes, and snippets.

@MHBalsmeier
Last active February 8, 2024 07:15
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • 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
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.

@benkraj
Copy link

benkraj commented Feb 5, 2021

I was struggling to get eccodes installed fully on my ubuntu distro, and happened to come across this guide through google. It was a huge help, and I certainly wouldn't have been able to get it fully installed without it. Thanks a lot!

@MHBalsmeier
Copy link
Author

I was struggling to get eccodes installed fully on my ubuntu distro, and happened to come across this guide through google. It was a huge help, and I certainly wouldn't have been able to get it fully installed without it. Thanks a lot!

Thank you, that is nice to hear!

@AlkuinKoenig
Copy link

Superb! I think this is one of the first guides I encountered where I could just follow line by line without any conflicts along the way. Highly appreciated.

@MHBalsmeier
Copy link
Author

Superb! I think this is one of the first guides I encountered where I could just follow line by line without any conflicts along the way. Highly appreciated.

Thank you, you're welcome!

@valpesendorfer
Copy link

Thanks a lot, building Eccodes seems much less daunting and more straight-forward with your instructions, compared to what ECMWF provides.

A very minor note for convenience: you could add wget in the libraries installed on the top, to make sure it's available. It wasn't on my machine, and then you need to manually remove the directory again etc.

@MHBalsmeier
Copy link
Author

Thanks a lot, building Eccodes seems much less daunting and more straight-forward with your instructions, compared to what ECMWF provides.

A very minor note for convenience: you could add wget in the libraries installed on the top, to make sure it's available. It wasn't on my machine, and then you need to manually remove the directory again etc.

Alright, thanks, I'll do that, I thought it is always installed.

@absvm
Copy link

absvm commented Jul 25, 2021

Perfect guide, thank you. That was a very smooth installation.

@MHBalsmeier
Copy link
Author

Perfect guide, thank you. That was a very smooth installation.

Thank you very much!

@dalmat36
Copy link

Thanks for this, very helpful! The only thing I had to do different was update to the latest cmake (which apt install would not go beyond 3.10.2) did this and it worked: https://askubuntu.com/questions/355565/how-do-i-install-the-latest-version-of-cmake-from-the-command-line

@MHBalsmeier
Copy link
Author

Thanks for this, very helpful! The only thing I had to do different was update to the latest cmake (which apt install would not go beyond 3.10.2) did this and it worked: https://askubuntu.com/questions/355565/how-do-i-install-the-latest-version-of-cmake-from-the-command-line

That's interesting. In my case, apt installs 3.16.3-1ubuntu1.

@var-nan
Copy link

var-nan commented Sep 16, 2021

Thanks a ton.

@craigjwallace
Copy link

Thank you for taking the time to prepare this. Really helpful.

@crazyapril
Copy link

I simply copy and paste all the command step by step and it works like a charm... Awesome guide!

@MatsA
Copy link

MatsA commented May 30, 2022

O yes, a very nice writing ! Done on a Raspberry Pi 4 with Bullseye, 64 bit, reported one error => 40 - eccodes_t_bufr_ecc-1290 (ailed), but I'm just using the grib CLI which works fine.
Thanks !

@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.

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