DEV Community

Cover image for How to Install Google OR-Tools on Apple M1 ARM64?
Yulin Chen
Yulin Chen

Posted on

How to Install Google OR-Tools on Apple M1 ARM64?

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.

Terminal

2.Open new terminal with Rosetta

Right click on Terminal Rosetta and then Get Info, tick the box where it says Open using Rosetta.

Terminal Info

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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)"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
python86 -m pip install -U --user wheel six
Enter fullscreen mode Exit fullscreen mode

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()}')"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
VS Code

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
          }
    },
Enter fullscreen mode Exit fullscreen mode

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
VS Code Terminal

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

Oldest comments (1)

Collapse
 
mizux profile image
Mizux

Few note:

  1. 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

  2. you should be able to build from source on M1

cmake -S. -Bbuild -DBUILD_PYTHON:BOOL=ON -DPython3_ROOT_DIR="path/of/your/python3/if/cmake/didn't/found/the/correct/one"
cmake --build build --config Release
Enter fullscreen mode Exit fullscreen mode