Editing the .bash_profile for wgrib2 Install

Making sure the PATH to wgrib2 compile libraries is correct

Posted Wednesday 11/27/24

When you launch the Terminal program, there’s a hidden file that tells it where certain other code and libraries are located on you Mac. The “path” to these code libraries are in this hidden file called the .bash_profile. The period (.) at the beginning of the name makes this file normally hidden in the Finder.

Since we’ll be installing wgrib2 with Terminal and Homebrew, the Terminal program will need to “know” the path where Homebrew placed all of its libraries and where the wgrib2 dependencies are located. The .bash_profile has this information. This info is referred to as $PATH.

To view this hidden file, many people use a series of commands to navigate to the directory where the .bash_profile is located and then use an archaic Terminal program called VIM to edit the file. There’s a better way.

Your hidden .bash_profile is in your home directory, the one named for your username or computer. As an example, my home directory might be called “m3imac”

If I open that home directory, I’ll see the usual items—

Here’s the trick—
–> With this home folder open and the folder contents visible, using the keyboard, press these three keys at the same time:

COMMAND-SHIFT-PERIOD

This will reveal several hidden files, including the .bash_profile. They will be greyed out.

CAUTION: Making hidden files visible has dangers. Don’t accidentally delete any file or rename any hidden file. Be sure to make them hidden again when done using the same Command-Shift-Period key combination.

If you right-click on the greyed out .bash_profile, you can select to open it with a text editor like BBedit or TextEdit and modify it, just like any other text file on your Mac.

This is what the contents of the .bash_profile should look like in your text editor if you’re doing what I call a clean install of wgrib2—

eval "$(/opt/homebrew/bin/brew shellenv)"

Or you may see this on older Macs—

export PATH=$PATH:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/sbin
eval "$(/opt/homebrew/bin/brew shellenv)"

But you may have a more complicated .bash_profile due to conda or other programs previously installed. Your file may look like this (below). If so, you may have issues with wgrib2 compilation—


# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/Users/glennmacmini/miniconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
eval "$__conda_setup"
else
if [ -f "/Users/glennmacmini/miniconda3/etc/profile.d/conda.sh" ]; then
. "/Users/glennmacmini/miniconda3/etc/profile.d/conda.sh"
else
export PATH="/Users/glennmacmini/miniconda3/bin:$PATH"
fi
fi
unset __conda_setup
# <<< conda initialize <<<

If your bash profile is complex like the above and you’ve had problems with the wgrib2 compilation, I would recommend this—

Select and copy all the existing text in your .bash_profile that references other programs and or conda, then paste this text into a new text file and SAVE this new text (as “Back-up profile“) to your desktop (to be used later if you need to restore this text back.)

• Delete all the text in the .bash_profile file so that it’s blank.
• Then Copy and Paste the text below to your .bash_profile and SAVE it—

export PATH=$PATH:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/sbin
eval "$(/opt/homebrew/bin/brew shellenv)"

–> SAVE the modifications you just made to your .bash_profile.

–> VERY IMPORTANT— Go back to the Finder view of your Home folder and press COMMAND-SHIFT-PERIOD to make these files hidden again.

–>Failure to hide these files again can create havoc with your Mac later on.

Another Possible Solution

For those having wgrib2 compilation errors with conda installed

Here’s another option to explore if your prior conda install is not playing well with wgrib2 install—

Edit your .bash_profile, as explained above.

• Save the existing text in the .bash_profile to a new text file on your Desktop to serve as a backup.

• Then delete all the existing text in the .bash_profile so that it is blank,

• then replace with this code and SAVE the changes—

if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi


# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/Users/glennmacmini/miniconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
eval "$__conda_setup"
else
if [ -f "/Users/glennmacmini/miniconda3/etc/profile.d/conda.sh" ]; then
. "/Users/glennmacmini/miniconda3/etc/profile.d/conda.sh"
else
export PATH="/Users/glennmacmini/miniconda3/bin:$PATH"
fi
fi
unset __conda_setup
# <<< conda initialize <<<

The above code in the .bash_profile works fine for me with both conda and wgrib2 installed and with the default shell set at bash.

Then proceed with the rest of my wgrib2 installation tutorial.

Weather… and Other Things 'Up in the Air'