If you've been encountering this error when using Python-MIP:
An error occurred while loading the CBC library: cannot load library '/opt/homebrew/lib/python3.11/site-packages/mip/libraries/cbc-c-darwin-x86-64.dylib': dlopen(/opt/homebrew/lib/python3.11/site-packages/mip/libraries/cbc-c-darwin-x86-64.dylib, 0x0002): tried: '/opt/homebrew/lib/python3.11/site-packages/mip/libraries/cbc-c-darwin-x86-64.dylib' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64')), '/System/Volumes/Preboot/Cryptexes/OS/opt/homebrew/lib/python3.11/site-packages/mip/libraries/cbc-c-darwin-x86-64.dylib' (no such file), '/opt/homebrew/lib/python3.11/site-packages/mip/libraries/cbc-c-darwin-x86-64.dylib' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64')). Additionally, ctypes.util.find_library() did not manage to locate a library called '/opt/homebrew/lib/python3.11/site-packages/mip/libraries/cbc-c-darwin-x86-64.dylib'
Then you'll hopefully fix it by the end of this small guide.
Python-MIP uses CBC, a solver by COIN-OR, which is distributed via PIP but only in x86_64 binaries. If you want to use the same library on ARM PCs, you are going to need to recompile CBC yourself.
Fortunately, COIN-OR provides a very nice tool, coinbrew
, which serves exactly this purpose: it lets users fetch and build practically every COIN-OR project.
Let's see how to download and build CBC:
- Move into your home directory (or any other easily-accessible directory in your Mac). Then, create a new directory, called
COIN-OR
, with the following command:mkdir COIN-OR
- Download
coinbrew
with this command:We'll be using this tool to download the master branch from the CBC GitHub repository and then to build the library.wget https://raw.githubusercontent.com/coin-or/coinbrew/master/coinbrew
- Download the CBC library with the command:
This will most likely take a while, especially on slow internet connections.
./coinbrew fetch Cbc@master
- Build the CBC library with the command:
This will take a while as well, since there are a lot of dependencies that have to be built for CBC to work properly.
./coinbrew build Cbc ADD_CXX_FLAGS=-DCMAKE_OSX_ARCHITECTURES=arm64 --parallel-jobs 8
- Once the above command finishes execution, add the following line to your
.zshrc
:This will tell MIP to fetch our newly built library when trying to solve any problem.export PMIP_CBC_LIBRARY="/path/to/build/directory/dist/lib/libCbc.dylib"
- Finally, reload your
.zshrc
with the command:source ~/.zshrc
Now you should be able to open any Python prompt or Jupyter notebook and create a MIP model without the error above.
If the error still appears after all these steps, check for typos and try a reboot first.
Enjoy!