Search code examples
bashmacosgoogle-bigqueryodbcstata

Stata is not seeing environment variables needed for ODBC connection


I use the Simba ODBC driver (2.3.2) to connect Stata to BigQuery from my macOS laptop. I recently upgraded to Big Sur (11.2.1) and lost the ability to do this.

I am using iODBC and I am able to test the DSN successfully. I was also able to get everything to work in macOS 10.15.7 just fine (on a different machine).

The problem appears to be that Stata is not seeing the DYLD_LIBRARY_PATH environment variable that I have defined in my .bash_profile profile according to the driver configuration instructions.

In Stata, I get this error:

. odbc list
The ODBC file libiodbc.dylib could not be found on this system.
Setting the unix LD_LIBRARY_PATH environment variable may correct this error.
r(680);

I am able to set this in bash:

$ echo $DYLD_LIBRARY_PATH
:/usr/lib/:/usr/local/

But that does not seem to work since:

$ printenv | grep 'DYLD'
 

is empty. I suspect something in Big Sur is preventing this environment variable from being set.

Happy to try anything to fix this.


Solution

  • To fix this, you need to disable SIP, which keeps the $DYLD_LIBRARY_PATH environment variable from being defined without issuing a warning.

    To disable SIP:

    1. Restart your computer in Recovery mode by holding down Command(⌘)-R once it powers off. Press that until you see the Apple icon and a progress bar.
    2. From the Utilities menu, select Terminal.
    3. At the prompt, type the following command and then press return: csrutil disable
    4. Terminal should display a message that SIP was disabled.
    5. From the Apple menu, select Restart.

    Now you need to define an environment variable that points to the folder with libiodbc.dylib. To figure out where that folder is, type this in Terminal:

    find / -name libiodbc.dylib 2>&1 | grep -v "find: "

    Using your favorite text editor (like pico, emacs, or vi), you need to edit your .zshenv file. Open this file (or create it if it does not exist) with

    pico ~/.zshenv

    Using the first folder from above, define an environment variable by typing this in the file:

    export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:<FOLDER-PATH-HERE>

    Save and exit the text editor.

    To apply the changes to the current shell, type source .zshenv.

    This should make Stata and BQ play nice again.

    zsh is the default shell starting with Catalina, so if you use another shell, you should modify things accordingly. I use bash myself, but I hope my translation for zsh works.

    You may need to start Stata from the command line for this to fully work. Here's the output of the command after the fix above:

    enter image description here