PyBaMM icon indicating copy to clipboard operation
PyBaMM copied to clipboard

[Bug]: Installing `PyBaMM` on `M1` chip fails (both `pip` and install from source)

Open Saransh-cpp opened this issue 3 years ago • 2 comments

PyBaMM Version

all

Python Version

3.9

Describe the bug

PyBaMM cannot be installed on an M1 mac from pip as well as from source. I think casadi does not support M1 chips?

I was able to set everything up by installing casadi from source, and removing casadi as a dependency when installing PyBaMM from source. Should this be documented in the installation page?

Also, should we be specifically building intel wheels till casadi supports M1 wheels? This way users will get a "No matching distribution found for pybamm" error when installing through pip on M1, rather than than the installation failing midway.

Building casadi locally -

brew install cmake swig
git clone https://github.com/casadi/casadi.git casadi
cd casadi
git checkout 3.5.5
mkdir build
cd build
cmake -DWITH_PYTHON=ON \
-DWITH_PYTHON3=ON \
-DPYTHON_INCLUDE_DIR=$(python3 -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") \
-DPYTHON_LIBRARY=$(python3 -c "import distutils.sysconfig as sysconfig; print(sysconfig.get_config_var('LIBDIR'))") \
..
make
sudo make install

Steps to Reproduce

pip3 install pybamm
pip3 install -e .

Relevant log output

ERROR: Could not find a version that satisfies the requirement casadi>=3.5.0 (from pybamm) (from versions: none)
ERROR: No matching distribution found for casadi>=3.5.0

Saransh-cpp avatar Oct 05 '22 20:10 Saransh-cpp

I'm able to install pybamm on M1 from source, but maybe it's using Rosetta?

valentinsulzer avatar Oct 05 '22 20:10 valentinsulzer

I think so. #2072 looks related (casadi not found), the only difference is that I did not try building up the IDAKLUSolver.

Saransh-cpp avatar Oct 05 '22 21:10 Saransh-cpp

You can force it to use rosetta if you're using conda by setting the environment variable CONDA_SUBDIR to osx-x64.

https://towardsdatascience.com/how-to-manage-conda-environments-on-an-apple-silicon-m1-mac-1e29cb3bad12

Actually I think this maybe only works with miniconda

Actually this breaks numpy

aabills avatar Nov 06 '22 23:11 aabills

I managed to get it to install and sort of work. Using an M1 MBA (2020):

  • Download and install Python 3.8.10 universal installer (maybe 3.9 works too, I haven't tried)
  • Use the interpreter /Library/Frameworks/Python.framework/Versions/3.8/bin/python3.8-intel64 to create a venv and run the usual pip install pybamm from inside that environment.

I think using the intel64 binary, which I guess automatically runs under rosetta, seems to work? When I run a script after doing import pybamm it does take about ~4 secs to start running. Not sure if this is how it usually is with this package.

savvn001 avatar Nov 24 '22 15:11 savvn001

@savvn001's method on Python 3.8.10 + M2 MBP + MacOS 12.5 works for me - it did not work with 3.9.

Specifically: 0. From within a rosetta terminal:

  1. Installed 3.8.10 from https://www.python.org/downloads/release/python-3810/
  2. Created a virtual environment based on the intel64 interpreter: /Library/Frameworks/Python.framework/Versions/3.8/bin/python3.8-intel64 -m venv venv38intel
  3. For usage in jupyter lab, added the kernel ipython kernel install --user --name=venv38intel
  4. Activated the venv source venv38intel/bin/activate

ndrewwang avatar Dec 16 '22 23:12 ndrewwang

conda-forge should also work (no rosetta required)

aabills avatar Dec 16 '22 23:12 aabills

FYI the following worked for me on Python 3.9.13 + M2 Macbook Air + MacOS 13.2

  1. Installed Python 3.9.13 universal installer from here
  2. Created a virtual env using the intel64 interpreter /Library/Frameworks/Python.framework/Versions/3.9/bin/python3-intel64 -m venv env
  3. Activate and install as normal

rtimms avatar Feb 07 '23 11:02 rtimms

I made it! Device & system version: MacBook Pro 14 2021 (M1 Pro chip) / MacOS 12.6

Virtual environment manager: Anaconda 22.11.0 64-Bit (M1) Graphical Installer (484 MB) Python installed with this version uses the ARM architecture by default, like: osx-arm64::numpy-1.24.2-py39hff61c6a_0

Steps:

  1. Create a new environment based on Python 3.9: conda create -n battery python=3.9
  2. Activate environment: conda activate battery
  3. Forced use conda-forge to install PyBaMM: conda install -c conda-forge pybamm
  4. Activate and enjoy.

I ran into some network problems: Downloading and Extracting Packages , but they were quickly resolved.

CLOUDUH avatar Mar 01 '23 13:03 CLOUDUH

Out of curiosity has anyone checked if there's a performance difference between Conda/build-from-source and the rosetta options?

aabills avatar Mar 02 '23 16:03 aabills

Casadi has started building wheels for M1/M2 in their nightly-develop releases - https://github.com/casadi/casadi/releases/tag/nightly-develop

Saransh-cpp avatar Mar 16 '23 18:03 Saransh-cpp

Casadi 3.6.0 brings support for Apple Silicon: https://github.com/casadi/casadi/releases/tag/3.6.0

agriyakhetarpal avatar Apr 06 '23 19:04 agriyakhetarpal