How to Install and Compile wgrib2 on Mac OS – Apple Silicon M1-M3 – Clean Install Version

Updated for MacOS Ventura Sonoma
wgrib2 version 3.1.3

Originally Posted February 4, 2024

Introductory Note: and Revisions—


Updated 03-18-24 to make HomeBrew $PATH command more explicit.

Updated 03-21-24 to emphasize using BASH in Termal instead of the new default .zsh


Updated 04-13-24 A few people are having compilation errors where certain dependency libraries are not being found. I’ve traced the errors to issues based upon which directory” homebrew” becomes installed in. This “clean install” ” assumes that homebrew is installed here—

/opt/homebrew/bin/brew

However, if homebrew is installed here —

/usr/local/bin/brew

a different compilation approach is needed. I have bifurcated the install instructions below based upon the install location of homebrew.


Updated 05/25/24 Someone this month ran into compilation errors. It appears that Homebrew is now installing gcc version 14 as its default. I have not confirmed this, but there may be a compatibility issue with gcc-14 and wgrib2.

The old instructions had this command:

brew install gcc

I would instead run the command as of 4/2024

brew install gcc@13

This will force Homebrew to install a known-compatible version of gcc. This will be explained again later in the tutorial when we install gcc.


It was discovered that MacOS Ventura and Sonoma systems on Apple Silicon Macs that are “clean” and not the result of a Migration Assistant install/transfer from of an older Intel Mac have different directory structures as a result of Apple enforcing stricter separation of user-installed software and system software.

The reason for the change in steps— Intel Macs and Apple Silicon Macs migrated from Intel use the hidden directories: /usr/local/Homebrew/ and /usr/local/bin/. Clean installs preferentially use: /opt/homebrew/ and /opt/hombrew/bin/ as the directories.

These wgrib2 install instructions apply to what I’m calling a ‘clean install’ Apple Silicon Mac computer. Specifically use these instructions if you wish to install wgrib2 on —

• A clean, new Mac System on M1-M3 Computers — Ventura or Sonoma.
No previous use of Migration Assistant or a Time Machine/CC Backup restore from a system that originated from an older Intel Mac.
• No previous install of Homebrew.

As of this writing, the latest version of wgrib2 is 3.1.3.

This tutorial is an update. My previously posted tutorial applied to Intel and Apple Silicon Macs that had been migrated from a previous Intel directory structure.

Important- Use Bash shell in Terminal instead of .zsh

Note: I am still using bash as my Terminal shell language and in this tutorial. However, Apple now defaults to using zsh as its shell language. The feedback I’ve gotten from people using this tutorial is that zsh shell may not work. I highly recommend changing back to bash shell, at least for the installation/compilation of wgrib2.

To change it back to bash, go to System Settings>>User & Groups. Right-Click or option-Click on your username and change the setting to bin/bash.

Alternatively, you can use Terminal to switch its shell default with the command

chsh -s /bin/bash

Press return. Quit Terminal to have the setting saved.


Tutorial Start: Introduction to the wgrib2 utility – Installing wgrib2

To work efficiently with very large weather GRIB2 data files, the wgrib2 utility was developed by a team of several NOAA data scientists, led by Wesley Ebisuzaki.  It is an essential command line-based  C and  fortran-based program used to index and manipulate weather data grib2 files. 

Most commonly, it’s used to extract and manipulate specific weather model parameters, perform calculations and to create GRIB2 files that cover smaller geographic areas. 

Unlike most graphical interface applications a Mac user might use that are already compiled by the app’s developer, wgrib2 is a low level command line application that runs on the Mac’s underlying Unix -Darwin foundation. The Terminal program is used to run it.

Its operation is dependent on your Mac’s specific directory structure and the versions and locations of code libraries (“dependencies”) installed on your Mac. As a result, you must compile the wgrib2 program based on these dependencies and the specific directory structure of your Mac.

wgrib2 is supplied as as folder of code, scripts, and code libraries that are downloaded and then a compile script is run to put the program together.

This tutorial will explain how to do this.

From my original install/compile tutorial, installing wgrib2 on a Mac used to be a very complex and tedious adventure, fraught with problems, starts and stops. But it could be accomplished.

I’m happy to report that wgrib2 (latest version 3.1.3 ) can now be installed and compiled relatively easily on an Apple Silicon Mac.

A special thank you to a reader, “Davis Zhang“, who alerted me to me to the existence of a brief Japanese language install guide which showed that wgrib2 version 3.x can now be installed with a significant reduction in complexity on the latest versions of Mac OS.

As was the case my first install/compile tutorial and a more recent tutorial, this tutorial is designed for true novices with limited experience using Terminal and command lines. It’s probably more detailed than many people need, but I’m sure there are some of you that will appreciate the hand-holding.

Overview of the install steps

  1. Download wgrib2 from the NOAA web site; rename the folder
  2. Uncompress or ‘untar’ the wgrib2 download
  3. Download and install Apple Developer Tools
  4. Download Homebrew package installer needed to install required library “dependencies”. Create Homebrew recommended links and $PATH changes.
  5. Using Homebrew, install various library dependencies – gcc, gfortran, cmake. Confirm proper directory structure and PATHs using ‘brew doctor‘ command. Check specific version of gcc installed.
  6. Edit the compilation script called ‘makefile
  7. Copy and paste a copy of your modified grib2 folder to the directory: /opt/homebrew/
  8. Run the ‘makefile‘ compilation script using a Terminal command to compile wgrib2 into an executable.
  9. Create a symlink of the installed wgrib2 in your /opt/homebrew/bin bindirectory so that it can easily be called using Terminal.

(Highlighted above means this is new for Sonoma and clean installs)

So here we go—

1- Download wgrib2 from the NOAA web site

The latest versions of wgrib2 code from NOAA and many README documents are available from this web site:

https://www.ftp.cpc.ncep.noaa.gov/wd51we/wgrib2/

As of this writing, the latest version is 3.1.3 (Download the version WITH libraries)

You’ll likely find the wgrib2.tgz.v3.1x  package in your Downloads folder.

Move the wgrib2.tgz.v3 file from your Downloads folder to your Desktop

2-Uncompress the wgrib2.tgz download

On your Desktop, rename the wgrib2.tgz.v3.1.3 file to simply wgrib2.tgz by right clicking or option-clicking on the file and select rename. Confirm yes when it asks you to change the file type. This should be the filename—

After renaming the file, double click on the wgrib2.tgz file. The file should untar (unzip, uncompress) and a folder called grib2 will be on your Desktop.

We’ll get back to this folder shortly.

3-Download and install Apple Developer Tools

You may have X-code already installed on your computer. If so, you will still need to install Apple Developer Tools.

Launch Terminal (in your Applications/Utilities Folder )

enter the following command and press return 

xcode-select --install

After Developer Tools downloads and installs, it will launch. You MUST agree to the terms of the license agreement or things won’t go well.

Verify that you’ve successfully installed Xcode Command Line Tools using Terminal by entering this command and press return

$ xcode-select -p

You should see a confirmation showing where it’s installed

/Library/Developer/CommandLineTools

If you’re a novice, I strongly recommend that you quit Terminal after each major step and then restart the Terminal program. It avoids certain issues of being in the wrong directory, etc.

4- Download and Install HomeBrew

Homebrew is a “package installer” and is used to download and install certain dependency libraries of code, used by many utilities including wgrib2. We’ll be using Homebrew to install the dependent compiler packages such as gcc, gfortran and cmake.

Go to https://brew.sh/ and follow the relatively simple instructions to install HomeBrew by copying a command from its web site home page, then opening your Terminal program and pasting the copied command into the Terminal window.

•Copy the install command from the Homebrew web site.
•Paste it into the Terminal app window prompt. 

Homebrew will install.

Important: Run the following command in Terminal after Homebrew finishes installing:

which brew

If Terminal responds— /opt/homebrew/bin/brew, continue with the tutorial immediately below.

If Terminal responds/usr/local/bin/brew, then STOP AT THIS POINT and click on this link to continue—


Continue…. (if Homebrew is installed at /opt/homebrew/bin/brew—)

–> At the end of the install, HomeBrew will “recommend” two commands to create an important addition to your $PATH variable. This will allow it to run other commands.

Run these two commands below in Terminal to add Homebrew to your $PATH:

echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users/yourusernamehere/.bashrc
eval "$(/opt/homebrew/bin/brew shellenv)"


(HomeBrew will likely have shown the above two commands at the end of its installation, but they’re easy to miss. You can select and copy the command text at the end of the HomeBrew installation window and paste it back into the terminal window to run the recommended command for your specific computer.)


Now that HomeBrew is installed, enter the command below in Terminal and press return

brew doctor

This will check that everything is in its proper place. It will also do tests that may alert you to issues that may affect compilation and install of wgrib2. Running this command may result in recommendations of other commands to fix links, symlinks and conflicts. I do suggest that you follow its recommendations and fix the things it finds.

You can run “brew doctor” any time again to see other recommendations.

Using the Terminal File Menu, you can open a second window in Terminal to copy and paste HomeBrew’s recommendation commands to fix and solve any issues.

5-Using HomeBrew, install various library dependencies – gcc, gfortran, cmake

Now we’re going to install specific libraries that wgrib2 compilation will need.

Install gcc (installs gfortran automatically at the same time)

Open Terminal

Enter the command below in Terminal and press return

brew install gcc

gcc should then be installed.

Check to see where it’s installed,  enter the following command in Terminal and press return—

 which gcc

and terminal should show this, if gcc is installed:

/usr/bin/gcc

Installation of gcc includes automatic installation of gfortran.

To check to see where gfortran was installed, enter the following command in Terminal and press return—

which gfortran

and if it’s installed, Terminal will reply:

/opt/homebrew/bin/gfortran

Install cmake

Finally, we’ll use HomeBrew to install cmake.

Enter the command below in Terminal and press return—

brew install cmake

It should be installed and you can check success by using the “which cmake’ command.

Here’s an important point

We need to know what version of gcc HomeBrew has just installed. The usual command to determine the gcc version will reveal that Apple has installed the ‘clang’ version when Developer Tools were installed.

Enter the following command in Terminal and press return—

gcc --version

You’ll see the following—

Apple clang version 14.0.0 (clang-1400.0.29.202)
Target: x86_64-apple-darwin21.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

We don’t want to use this clang version when compiling wgrib2

So let’s find out the version that’s been installed by HomeBrew. It’s in the /opt/homebrew/bin/ folder.

Restart Terminal, then enter the following command and press return:

 cd /opt/homebrew/bin/

then enter the command below and press return

ls

You’ll see the listing (ls) of what’s in the opt directory.

Terminal should report that gcc@13 is installed in this directory.

We can see that the version installed by HomeBrew in my /usr/local/opt directory is gcc-13.

(Don’t get confused by this: Terminal reports gcc@13, but the actual gcc file name is gcc-13. We will be using the actual file name gcc-13 for our edits)

IMPORTANT When we edit the makefile (covered below), the version you’re going to use is gcc-13, even though Terminal reports gcc@13.

(If you use the Go Menu in the Finder and use the command Go to Folder, and enter /opt/homebrew/bin/ you will see gcc-13, not gcc@13 is the name of the file. )

Depending upon the current release version of gcc, yours may differ. For illustration purposes, we’ll use gcc-13, the version installed here, when we edit the ‘make’ script file.

When we modify the the ‘makefile’ file script, be certain to use the version that was installed in your computer.

So to this point, we’ve installed Apple Developer Tools, gcc gfortran and cmake.

–> Now we’re going to return to the grib2 folder that’s on your Desktop that we had uncompressed (untarred) earlier.

6-Edit the wgrib2 installer “make” file.

In the Finder, go to the folder grib2 downloaded and uncompressed earlier on your Desktop.

Open the grib2 folder on your Desktop. Inside you’ll see a file called ‘makefile‘.

We’re going to edit this the file using a text editor application.

Using any text editor, open the ‘makefile‘ file. Right click on the file and select “open with”. You can use TextEdit. I’ll be using BBedit to edit the makefile

About line 154 or so (depends on the wgrib2 version) you will find under #Main development platform: linux with gcc/gfortran:

We are going to make the two lines referring to gcc and gfortran here active (by uncommenting) the lines (meaning, removing the preceding hashtag -#-) and editing to indicate the version of gcc that was installed with HomeBrew earlier.

Using your text editor, change these two lines:

# export CC=gcc
# export FC=gfortran

to this:

export CC=gcc-13
export FC=gfortran

Notice that the leading # has been removed from each line and the version number of gcc installed by Homebrew (gcc-13) has been edited in.

IMPORTANT When we modify the the ‘makefile’ file script, be certain to use the version that was installed in your computer.


There are two more line changes that need to be made in this makefile

• Change- USE_PROJ4=1 to USE_PROJ4=0

• Change- USE_PNG=1 to USE_PNG=0

shows after the changes are made

–>When the edits have been completed, remember to SAVE the changes to the makefile from your text editor’s File menu.

Now close the grib2 folder on your Desktop.

7- Copy your grib2 folder and paste to /opt/homebrew

Using the Finder, select the grib2 folder on your Desktop and select the Copy command from the Edit Menu, or use the Command-c shortcut.

Using another command from the Finder, select the Go Menu and select Go to Folder…

Enter this path:

/opt/homebrew/

This will show a normally hidden folder on your Mac, the
/opt/homebrew/ folder.

PASTE the grib2 folder (that you copied from Desktop) into this folder. (Command -v)

After pasting, you should see a copy of the grib2 folder in the
/opt/homebrew/ directory—

We’re almost there!

8- Run the ‘makefile’ compiler script in super user mode.

We’re ready to compile and install wgrib2.

Make sure you’re running as an administrator level user on your Mac.

Open Terminal.

Enter the following command and press return:

cd /opt/homebrew/grib2/

Then enter the following command and press return:

sudo make

You’ll be prompted for your user password. Enter it and press return.

You should see a bunch of gibberish scroll past on the Terminal window as it compiles and installs. It may take as much as 10 or more minutes for it to complete. Longer is usually a good sign.

When it completes, it should NOT have any error messages at the end. If it was successful, it won’t tell you. You just won’t see any errors at the end.

If you see error messages at the end, something is amiss. I’ll explore troubleshooting in another tutorial.

9- Create a Symlink

Quit Terminal. Restart Terminal again

We’re going to create a symlink in your /usr/local/bin folder so that Terminal can easily call wgrib2.

Enter the following command in Terminal and press return:

sudo ln -s /opt/homebrew/grib2/wgrib2/wgrib2 /opt/homebrew/bin

Enter your user password when prompted. Press return.

A symlink will have been created.

Now, let’s check if wgrib2 has been installed.

Quit and then reopen Terminal.

In Terminal, enter the following command in Terminal and press return—

which wgrib2

You should see the response

/opt/homebrew/bin/wgrib2

To see what wgrib2 can do, just enter this in Terminal and press return:

wgrib2

You should see all the commands wgrib2 can accept scroll in the Terminal window.

To see wgrib2 in action, use the terminal command on any .grib2 file you have to see its message contents—

wgrib2 -s mygribfile.grib2

Use of wgrib2 is an entire other tutorial.  Look for it in coming months. 


Use the tips found here to use wgrib2— https://www.ftp.cpc.ncep.noaa.gov/wd51we/wgrib2/tricks.cheap

see the "tricks" links at 

https://www.ftp.cpc.ncep.noaa.gov/wd51we/wgrib2/
 

Need Help with an Installation?

Use this Feedback form to email me.

Include your email address and the following information:

• Are you using Intel or Apple Silicon (M1 etc) ?

• What Mac system version are you using? 

• Is this a new, clean install machine or was it migrated from another Mac?

• Had you previously installed HomeBrew for other software?

• Which directory is Homebrew installed in ?     /usr/local   or   /opt

• Is there an existing /usr/local/ directory?

• Did you run the command brew doctor to make sure your symlinks are correct for homebrew?   (You can run it several times to fix everything.)

• What version of gcc did you install?

• Did you make all the changes needed in the makefile compiler script  —
Change- USE_PROJ4=1 to USE_PROJ4=0

Change- USE_PNG=1 to USE_PNG=0

Here’s a helpful article here about HomeBrew and Apple Silicon.
Here’s a helpful article about removing Homebrew to start clean.

Tip: If having compilation issues, remove any old symlinks for gcc and gfortran from your /usr/local/ and usr/local/bin/ directories.

#wgrib2 #install wgrib2 on MacOS


5 thoughts on “How to Install and Compile wgrib2 on Mac OS – Apple Silicon M1-M3 – Clean Install Version”

    1. I’m happy to help, but please use the link (above the comment form) to send me an email and include your email address.
      I need a lot more info to help you

      What Mac system version are you using?

      What directory is Homebrew installed in ? /usr/local or /opt?

      Is there an existing /usr/loca/ directory?

      Did you run the command brew doctor to make sure your symlinks are correct for homebrew? You can run it several times to fix everything.

      Did you make all the changes in the makefile compiler script especially

      • Change- USE_PROJ4=1 to USE_PROJ4=0

      • Change- USE_PNG=1 to USE_PNG=0

      Glenn

      1. I’m getting the same error:

        [ 69%] Building C object src/bin/jp2/CMakeFiles/opj_decompress.dir/__/common/color.c.o
        [ 71%] Building C object src/bin/jp2/CMakeFiles/opj_decompress.dir/__/common/opj_getopt.c.o
        [ 72%] Building C object src/bin/jp2/CMakeFiles/opj_decompress.dir/converttif.c.o
        [ 73%] Building C object src/bin/jp2/CMakeFiles/opj_decompress.dir/convertpng.c.o
        [ 75%] Linking C executable ../../../bin/opj_decompress
        ld: library not found for -ltiff
        collect2: error: ld returned 1 exit status
        make[3]: *** [bin/opj_decompress] Error 1
        make[2]: *** [src/bin/jp2/CMakeFiles/opj_decompress.dir/all] Error 2
        make[1]: *** [all] Error 2
        make: *** [/opt/homebrew/grib2/lib/libopenjp2.a] Error 2

Comments are closed.

Weather… and Other Things 'Up in the Air'