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

yulin

Yulin

Posted on March 2, 2022

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

💖 💪 🙅 🚩
yulin
Yulin

Posted on March 2, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Milvus Adventures Dec 15, 2023
programming Milvus Adventures Dec 15, 2023

December 16, 2023

Building a PDF Summarizer with LangChain
programming Building a PDF Summarizer with LangChain

December 11, 2023