Solutions for DYLD_LIBRARY_PATH configuration issues when using acados with MATLAB on macOS.
Problem Description
When running MEX files, you may encounter the following error:
Invalid MEX-file: Library not loaded: @rpath/libacados.dylib
Setting DYLD_LIBRARY_PATH via setenv in MATLAB’s startup.m does not work because macOS dynamic linker only recognizes environment variables set before the process starts.
Solutions
Method 1: Terminal Only (No SIP Disable Required)
Add the following alias to ~/.zshrc (or ~/.bash_profile):
bash
alias matlab='DYLD_LIBRARY_PATH="<acados_install_dir>/lib:$DYLD_LIBRARY_PATH" ACADOS_INSTALL_DIR="<acados_install_dir>" /Applications/MATLAB_R2024b.app/Contents/MacOS/MATLAB_maca64'
Usage: Run matlab command in terminal
Method 2: App Icon Launch Support (Requires SIP Disable)
Step 1: Disable SIP
- Reboot into Recovery Mode (Intel:
Command + R, Apple Silicon: Hold power button) - Run in terminal:
bash
csrutil disable
- Reboot
echo ‘launchctl setenv DYLD_LIBRARY_PATH “<acados_install_dir>/lib”’ >> ~/.zshrc or ~/.zshenv
echo ‘launchctl setenv ACADOS_INSTALL_DIR “<acados_install_dir>”’ >> ~/.zshrc or ~/.zshenv
source ~/.zshrc or ~/.zshenv