Skip to content

Instantly share code, notes, and snippets.

@MrVideo
Created January 9, 2024 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MrVideo/8e1fd3798c613ca5bdd9d87026202744 to your computer and use it in GitHub Desktop.
Save MrVideo/8e1fd3798c613ca5bdd9d87026202744 to your computer and use it in GitHub Desktop.
A brief guide to set up CBC for ARM-based PCs

How to use Python-MIP on M1 Macs (and other ARM PCs)

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.


How to set up CBC for ARM

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:

  1. 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
  2. Download coinbrew with this command:
    wget https://raw.githubusercontent.com/coin-or/coinbrew/master/coinbrew
    We'll be using this tool to download the master branch from the CBC GitHub repository and then to build the library.
  3. Download the CBC library with the command:
    ./coinbrew fetch Cbc@master
    This will most likely take a while, especially on slow internet connections.
  4. Build the CBC library with the command:
    ./coinbrew build Cbc ADD_CXX_FLAGS=-DCMAKE_OSX_ARCHITECTURES=arm64 --parallel-jobs 8
    This will take a while as well, since there are a lot of dependencies that have to be built for CBC to work properly.
  5. Once the above command finishes execution, add the following line to your .zshrc:
    export PMIP_CBC_LIBRARY="/path/to/build/directory/dist/lib/libCbc.dylib"
    This will tell MIP to fetch our newly built library when trying to solve any problem.
  6. 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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment