Skip to content

Issue with CBC library in Python-MIP on macOS ARM64 (Apple Silicon) #406

Open

Description

@juanbellonrz

Hi, I’m trying to run a Python script that uses the python-mip library with the CBC solver on a MacBook with an ARM64 (Apple Silicon) chip.

  • I’ve installed CBC correctly using Homebrew (brew install cbc), and I can verify its availability by running cbc --version in the terminal.

  • However, when I run my script in Spyder, I get the following error:

An error occurred while loading the CBC library: cannot load library 'cbc-c-darwin-x86-64.dylib'...
mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64')
....
NameError: name 'cbclib' is not defined

  • I checked that my CBC installation is for ARM64 using:

file /opt/homebrew/lib/libcbc.dylib

Output: Mach-O 64-bit dynamically linked shared library arm64

  • I have also added the following environment variables in my script before importing mip:
    import os
    import ctypes

os.environ["DYLD_LIBRARY_PATH"] = "/opt/homebrew/lib:/opt/anaconda3/lib"
os.environ["PATH"] = "/opt/homebrew/bin:" + os.environ.get("PATH", "")
ctypes.CDLL("/opt/homebrew/lib/libcbc.dylib")

from mip import Model, CBC
model = Model(solver_name=CBC)

Despite these efforts, the issue persists. The script runs fine from the terminal, but in Spyder, I still get the architecture mismatch error.

I would appreciate any suggestions on how to force python-mip to use the correct version of CBC installed via Homebrew or how to resolve this compatibility issue in Spyder.

Thanks in advance.

Activity

  1. iamkarthikbk commented on Apr 8, 2025

    @iamkarthikbk

    I was facing a similar issue:

    An error occurred while loading the CBC library:	 cannot load library '/opt/homebrew/lib/python3.9/site-packages/mip/libraries/cbc-c-darwin-x86-64.dylib': dlopen(/opt/homebrew/lib/python3.9/site-packages/mip/libraries/cbc-c-darwin-x86-64.dylib, 0x0002): tried: '/opt/homebrew/lib/python3.9/site-packages/mip/libraries/cbc-c-darwin-x86-64.dylib' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e' or 'arm64')), '/System/Volumes/Preboot/Cryptexes/OS/opt/homebrew/lib/python3.9/site-packages/mip/libraries/cbc-c-darwin-x86-64.dylib' (no such file), '/opt/homebrew/lib/python3.9/site-packages/mip/libraries/cbc-c-darwin-x86-64.dylib' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e' or 'arm64')).  Additionally, ctypes.util.find_library() did not manage to locate a library called '/opt/homebrew/lib/python3.9/site-packages/mip/libraries/cbc-c-darwin-x86-64.dylib'
    
    Traceback (most recent call last):
      File "/Users/karthik/....py", line 167, in <module>
        m = Model()
      File "/opt/homebrew/lib/python3.9/site-packages/mip/model.py", line 98, in __init__
        import mip.cbc
      File "/opt/homebrew/lib/python3.9/site-packages/mip/cbc.py", line 603, in <module>
        Osi_getNumCols = cbclib.Osi_getNumCols
    NameError: name 'cbclib' is not defined
    

    I was able to invoke the CBC MILP solver by itself. I simply git cloned this repo and used pip install . -- looks like the arm64 library was missing in the installation when I did pip3.9 install mip

  2. arithauth commented on May 5, 2025

    @arithauth

    I’ve encountered a similar issue as well.

    Python-MIP doesn't install CBC Library for arm64. The installation guide indicates that PMIP_CBC_LIBRARY can be used to specify a custom library, but the dylib installed via brew install cbc did not work for me.

    I cloned the source from GitHub, built it, and set the full path in the environment variable as follows:

    % git clone https://github.com/coin-or/coinbrew.git
    % cd coinbrew
    % git rev-parse HEAD
    a3777537ea9af3b7ee132cde4aa107e2905b36ca
    % ./coinbrew fetch build Cbc@master --no-prompt --prefix=aaaa --tests=none --enable-cbc-parallel
    Install completed. If executing any of the installed
    binaries results in an error that shared libraries cannot
    be found, you may need to
      - add 'export LD_LIBRARY_PATH=/Users/a/Downloads/a/coinbrew/aaaa/lib' to your ~/.bashrc (Linux)
      - add 'export DYLD_LIBRARY_PATH=/Users/a/Downloads/a/coinbrew/aaaa/lib' to ~/.bashrc (OS X)
    % pip install "cffi>=1.17.1" mip
    % PMIP_CBC_LIBRARY="/Users/a/Downloads/a/coinbrew/aaaa/lib/libCbc.dylib" python a.py
    Welcome to the CBC MILP Solver 
    Version: devel 
    Build Date: May  5 2025

    Alternatively, you can:

    import ctypes
    import ctypes.util
    
    from mip import BINARY, CONTINUOUS, INTEGER, Model, maximize
    
    cbc_path = "/Users/a/Downloads/a/coinbrew/aaaa/lib/libCbc.dylib"
    _original_find = ctypes.util.find_library
    ctypes.util.find_library = lambda _: cbc_path
    ctypes.CDLL(cbc_path)
    if True:
        import mip.cbc
    ctypes.util.find_library = _original_find

    However, VSCode lets you set this environment variable conveniently:

    {
        "terminal.integrated.env.osx": {
            "PMIP_CBC_LIBRARY": "/Users/a/Downloads/a/coinbrew/aaaa/lib/libCbc.dylib"
            // "PMIP_CBC_LIBRARY": "${workspaceFolder}/coinbrew/dist/lib/libCbc.dylib"
        }
    }
  3. tkralphs commented on May 6, 2025

    @tkralphs
    Member

    python-mip uses a version of Cbc that is not officially released so brew install mip probably won't work (as you observed). The workaround suggested [above(https://github.com//issues/406#issuecomment-2785146485) seems like the right one, as it appears there has not been a release since the ARM64 libraries were added. It should be straightforward.

  4. tkralphs commented on May 6, 2025

    @tkralphs
    Member

    See #367 .

  5. tkralphs commented on May 6, 2025

    @tkralphs
    Member

    Actually, it looks like there is a release candidate you can try that has the ARM64 binaries.

    pip3 install mip==1.16rc0
    
  6. arithauth commented on May 6, 2025

    @arithauth

    I didn’t realize that python-mip uses a different version—thank you for explaining that to me!

    I was able to install the preview version, though I ran into the pitfall that in a fresh Python environment the package didn’t show up in pip list.
    It was so easy; it works perfectly for me and, on my first run, may even be faster than the official library.
    Thanks a lot!

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions