How to Install psycopg2 on an M1 Mac
Tags: python, postgres, django, osx
Published: 2022-03-25 01:21:04

## [TL;DR: The Solution](#the-solution)

## Contents

- [Introduction](#introduction)
- [The Solution](#the-solution)
- [Avoid These Potential Solutions](#avoid-these-potential-solutions)
- [Workarounds](#workarounds)
- [Conclusion](#conclusion)

## Introduction

&emsp; I remember a year ago, when I could not solve this problem for a different project so I ended up using a Docker container to emulate a simple linux environment to use `psycopg2`. I'm a bit older and wiser now, so when faced with the same problem for [my current project](https://dev.to/tieje/the-beginning-of-project-skill-tree-1je2), I dug around all day today figuring out how to install `psycopg2` in an M1 Mac environment. I tried many solutions that may have worked for others somehow, but ultimately did not work me. It turns out that the solution was the most vanilla installation I've done since I started using my M1 Mac Air.

## The Solution

1. Uninstall `pyenv`, `pipx`, and all other versions of `python` except your system's python.
2. Download the latest version of python from the [python website](https://www.python.org/downloads/release/python-3104/). At the time of writing, I was downloading python `3.10.4` so look around to see if there is an even more updated version.
3. Run the installer. Once it's finished, if you check your Applications, you should have your new version of python there. To test your new installation, run `python3 --version` in a fresh terminal.
4. Run `pip3 install psycopg2-binary`

### Optional Steps

&emsp; Optionally, you can also install PostgreSQL on your M1 mac as well.

1. Download the postgreSQL installation from the [official website](https://www.postgresql.org/download/). At the time of writing, the most recent version, PostgreSQL 14, looks like it has the most support for the M1 Mac architecture.
2. Run the installation wizard. The default location for my installation was `/Library/PostgreSQL/14`, so I just used that.
3. Add PostgreSQL bin to PATH in your `.zshrc` file. It should look like the following:

```bash
export PATH="/your/path/to/PostgreSQL/14/bin:\$PATH"
```

&emsp; Alternatively, you can can use this command if your installation was made in the same folder as mine:

```bash
echo 'export PATH="/Library/PostgreSQL/14/bin:$PATH"' >> ~/.zshrc
```

4. Open a new terminal and run `pg_config` in the command line. Your configuration should come up.
5. To get started on your Django application, PostgreSQL offers a [brief tutorial](https://www.enterprisedb.com/postgres-tutorials/how-use-postgresql-django
), which is actually useful later on when you're replacing the initial SQLite database in settings.

## Avoid These Potential Solutions

&emsp; Avoid using a version manager like `pyenv` or `asdf`. If you install `psycopg2` with this configuration, you will likely run into architectural issues with `arm64`.

&emsp; Avoid using `Homebrew` or anything that was installed with `Homebrew`. `openssl`, `libpq`, and other TLS/SSL packages will not be sufficient substitutes for the lack of SSL support for Python. Even adding a symlink to the specific libraries needed will not be sufficient enough to solve the problem.

## Workarounds

&emsp; As I mentioned in the [introduction](#introduction), you *can* use Docker to containerize your application and ultimately install `psycopg2`. But it's not the simplest or the fastest solution. If you must do it this way, here a few helpful links to the Github project that I was working on at the time to help you get started:

- [My Tutorial of getting started with Docker](https://github.com/tieje/yorha#Docker)
- [My `Dockerfile` used for the `Django` Backend](https://github.com/tieje/yorha/blob/main/Backend/Dockerfile)
- [My `docker-compose.yml` file used for the Frontend and Backend](https://github.com/tieje/yorha/blob/main/docker-compose.override.yml)


## Conclusion

&emsp; I hope this helps. It appears that Apple's M1 chip has received a ton of support from the developer community over the past year, which makes me less inclined to sell my Mac Air for a [Framework laptop](https://frame.work/).

&emsp; I can only speculate that Apple wants to eliminate the need for 3rd-party package managers and version managers like `Homebrew`, and `pyenv` and `asdf`, respectively. The newest update, Monterey 12.3, is hostile to 3rd party programs especially if Xcode is not installed. [It's not impossible to install and use 3rd-party applications but there will be security pop-up that needs a little more than a password to get around.](https://macresearch.org/fix-cant-be-opened-because-apple-cannot-check-it-for-malicious-software/).