How to Install Google OR-Tools on Apple M1 ARM64?
Tags: programming, tutorial, python, opensource
Published: 2022-03-02 13:27:11

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](https://developers.google.com/optimization/install/python/mac) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tsfuqto60lhgl5gmnssi.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ah6f7khyosyuxhm0xam4.png)

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 <a href="https://brew.sh/">Homebrew</a>
```
/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_
![VS Code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d3jdtyvy5wwf8skhtb5m.png)

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
![VS Code Terminal](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sv3nlqav2eaj6jo1e6lq.png)
 
Replace all `python` commands with `python86` when you're in Rosetta.

Hope you find that useful, have fun :)

<hr/>

If this doesn't work for you, you can try [install OR-tools from source by following the thread here](https://github.com/google/or-tools/issues/2332).

[Alternative method to install python is through pyenv, read about it here](http://sixty-north.com/blog/pyenv-apple-silicon.html).

### References
[How to Manage Multiple Python Versions on an Apple Silicon M1 Mac](https://towardsdatascience.com/how-to-use-manage-multiple-python-versions-on-an-apple-silicon-m1-mac-d69ee6ed0250)