python-mip icon indicating copy to clipboard operation
python-mip copied to clipboard

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

Open juanbellonrz opened this issue 11 months ago • 6 comments

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.

juanbellonrz avatar Jan 21 '25 19:01 juanbellonrz

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

iamkarthikbk avatar Apr 08 '25 03:04 iamkarthikbk

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"
    }
}

arithauth avatar May 05 '25 15:05 arithauth

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/coin-or/python-mip/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.

tkralphs avatar May 06 '25 15:05 tkralphs

See #367 .

tkralphs avatar May 06 '25 15:05 tkralphs

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

pip3 install mip==1.16rc0

tkralphs avatar May 06 '25 15:05 tkralphs

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!

arithauth avatar May 06 '25 16:05 arithauth