If you want to install Google OR-tools and you're using an Apple M1, you're in the right place. In this tutorial, we will walk through installing OR-tools for Python on MacOs with ARM64 architecture, and use Visual Studio Code as the text editor.
When you open the Google OR-tools installation page, you will stumble across the paragraph:
Note: OR-Tools only supports the x86_64 (also known as amd64) architecture.
To get by the mismatch in architectures, Apple's built in emulator Rosetta enables a Mac with Apple silicon to use apps built for a Mac with an Intel processor. We will first configure a new copy of the terminal and run all our commands from there.
Open Terminal in Rosetta
1.Make a copy of the terminal
Go to Finder > Applications > Utilities, right click on Terminal and then Copy, call the new terminal Terminal Rosetta.
2.Open new terminal with Rosetta
Right click on Terminal Rosetta and then Get Info, tick the box where it says Open using Rosetta.
Double click on Terminal Rosetta, you'll be asked to install Rosetta if this is the first time you open an app built for an Intel-based Mac.
3.Setup new terminal
Open Terminal Rosetta, and edit .zshrc
vim ~/.zshrc
Paste the following code snippet into .zshrc and save
# rosetta terminal setup
if [ $(arch) = "i386" ]; then
alias brew86="/usr/local/bin/brew"
alias pyenv86="arch -x86_64 pyenv"
alias python86="/usr/local/bin/python3"
fi
Here we're setting up alias for the pre-requisite tools we will install in the next step. The if statement checks if the Terminal is open in Rosetta and allows us to run commands like brew86 install
.
Pre-requisites for OR-Tools
1.Homebrew
In Terminal Rosetta, install the package manager Homebrew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Even if you have Homebrew already, this will install Homebrew in a different location for the x86 architecture. The two installations of Homebrew will make sure x86 and arm64 packages are separate.
The Apple Silicon installations are in /opt/homebrew/bin
, and the x86 installations are in /usr/local/bin
.
2.Python
Install Python using Homebrew
brew86 install python
python86 -m pip install -U --user wheel six
Paste the following code on Terminal Rosetta to verify your installation
python86 -c "
import os
import platform
print(f'os name: {os.name}')
print(f'processor: {platform.processor()}')
print(f'machine: {platform.machine()}')
print(f'system: {platform.system()}')
print(f'release: {platform.release()}')
print(f'uname: {platform.uname()}')"
Look at the output value for processor and machine, make sure you get the expected output:
processor: i386
machine: x86_64
Install OR-tools
In Terminal Rosetta, install OR-tools for Python
python86 -m pip install -U --user ortools
This is the end of OR-tools installation, the last section of this article will go over setting up Visual Studio Code integrated terminal. If you happen to use the integrated terminal, follow along.
Visual Studio Code Integrated Terminal
Press CMD + Shift + p
and go to Open Settings
Paste the following code snippet into the existing settings JSON
"terminal.integrated.profiles.osx": {
"bash": {
"path": "bash",
"args": ["-l"],
"icon": "terminal-bash"
},
"zsh": {
"path": "zsh",
"args": ["-l"]
},
"rosetta": {
"path": "arch",
"args": ["-x86_64", "zsh", "-l"],
"overrideName": true
}
},
Save the settings file and that's all the configurations done! Now when you open a terminal in Visual Studio Code, you will have the option to open it in Rosetta
Replace all python
commands with python86
when you're in Rosetta.
Hope you find that useful, have fun :)
If this doesn't work for you, you can try install OR-tools from source by following the thread here.
Alternative method to install python is through pyenv, read about it here.
References
How to Manage Multiple Python Versions on an Apple Silicon M1 Mac
Top comments (1)
Few note:
We now provide pypi package for Apple m1
e.g. ortools-9.3.10497-cp310-cp310-macosx_12_0_arm64.whl
ref: pypi.org/project/ortools/#files
you should be able to build from source on M1