PyBaMM
PyBaMM copied to clipboard
[Bug]: Installing `PyBaMM` on `M1` chip fails (both `pip` and install from source)
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
I'm able to install pybamm on M1 from source, but maybe it's using Rosetta?
I think so. #2072 looks related (casadi not found), the only difference is that I did not try building up the IDAKLUSolver.
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
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-intel64to create a venv and run the usualpip install pybammfrom 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'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:
- Installed 3.8.10 from https://www.python.org/downloads/release/python-3810/
- Created a virtual environment based on the intel64 interpreter:
/Library/Frameworks/Python.framework/Versions/3.8/bin/python3.8-intel64 -m venv venv38intel - For usage in jupyter lab, added the kernel
ipython kernel install --user --name=venv38intel - Activated the venv
source venv38intel/bin/activate
conda-forge should also work (no rosetta required)
FYI the following worked for me on Python 3.9.13 + M2 Macbook Air + MacOS 13.2
- Installed Python 3.9.13 universal installer from here
- Created a virtual env using the intel64 interpreter
/Library/Frameworks/Python.framework/Versions/3.9/bin/python3-intel64 -m venv env - Activate and install as normal
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:
- Create a new environment based on Python 3.9:
conda create -n battery python=3.9 - Activate environment:
conda activate battery - Forced use
conda-forgeto install PyBaMM:conda install -c conda-forge pybamm - Activate and enjoy.
I ran into some network problems: Downloading and Extracting Packages , but they were quickly resolved.
Out of curiosity has anyone checked if there's a performance difference between Conda/build-from-source and the rosetta options?
Casadi has started building wheels for M1/M2 in their nightly-develop releases - https://github.com/casadi/casadi/releases/tag/nightly-develop
Casadi 3.6.0 brings support for Apple Silicon: https://github.com/casadi/casadi/releases/tag/3.6.0