Error

M1 Mac에 psycopg2 설치하는 방법 (pip install psycopg2)

ssun-g 2023. 8. 16. 15:07
PostgreSQL을 Python과 함께 사용하기 위한 psycopg2 패키지를 M1 Mac에 성공적으로 설치하기 위한 문서

 

시스템 환경

  • Chip : Apple M1 Pro
  • OS : MacOS Ventura 13.4.1(22F82)
  • Memory : 16GB

1. Homebrew 설치

필자의 경우, Intel Mac → M1 Mac으로 교체하면서 Homebrew PATH가 달라져서 재설치하였음.

 

Github

 

Install Native Homebrew on Apple Silicon M1

Install Native Homebrew on Apple Silicon M1. GitHub Gist: instantly share code, notes, and snippets.

gist.github.com

 

다음 명령어로 Homebrew를 설치한다. (공식 Github과 같은 설치 방법) 

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

 

설치 완료 후 다음 명령어를 입력하면 아래 그림과 같은 결과 확인 가능

/opt/homebrew/bin/brew doctor

 

위 결과에서 echo~ 로 시작하는 부분을 복사하여 터미널에 붙여 넣어 실행한다.

echo 'export PATH="/opt/homebrew/bin:$PATH"' >> ~/.zshrc

 

재부팅 후 다음 명령어를 입력하면 PREFIX/opt/homebrew로 표시되어야 한다.

brew config

 

2.  libpq, openssl 설치 및 환경변수 설정

libpq 설치

brew install libpq --build-from-source

 

--build-from-source에서 오류가 발생하는 경우 다음 명령어 실행 (xcode 설치)

xcode-select --install

 

openssl 설치

brew install openssl

 

환경변수 설정

export LDFLAGS="-L/opt/homebrew/opt/openssl@1.1/lib -L/opt/homebrew/opt/libpq/lib"
export CPPFLAGS="-I/opt/homebrew/opt/openssl@1.1/include -I/opt/homebrew/opt/libpq/include"

 

3. psycopg2 설치

pip install psycopg2
pip install psycopg2-binary

Reference

 

Cannot install psycopg2 with pip3 on M1 Mac

I cannot install psycopg2 on my M1 Mac and I used pip3. When I tried to install with pip3 install psycopg2 The output like that: ERROR: Command errored out with exit status 1: command: /opt/

stackoverflow.com