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/homebrew/opt/[email protected]/bin/python3.9 -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/m5/vhnyyjg16gd352x6wdhpjysh0000gn/T/pip-install-9wiqe2nr/psycopg2_147a5ae8fd5a47079480274f4225edbf/setup.py'"'"'; __file__='"'"'/private/var/folders/m5/vhnyyjg16gd352x6wdhpjysh0000gn/T/pip-install-9wiqe2nr/psycopg2_147a5ae8fd5a47079480274f4225edbf/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /private/var/folders/m5/vhnyyjg16gd352x6wdhpjysh0000gn/T/pip-pip-egg-info-06hnnl2t
         cwd: /private/var/folders/m5/vhnyyjg16gd352x6wdhpjysh0000gn/T/pip-install-9wiqe2nr/psycopg2_147a5ae8fd5a47079480274f4225edbf/
    Complete output (23 lines):
    running egg_info
    creating /private/var/folders/m5/vhnyyjg16gd352x6wdhpjysh0000gn/T/pip-pip-egg-info-06hnnl2t/psycopg2.egg-info
    writing /private/var/folders/m5/vhnyyjg16gd352x6wdhpjysh0000gn/T/pip-pip-egg-info-06hnnl2t/psycopg2.egg-info/PKG-INFO
    writing dependency_links to /private/var/folders/m5/vhnyyjg16gd352x6wdhpjysh0000gn/T/pip-pip-egg-info-06hnnl2t/psycopg2.egg-info/dependency_links.txt
    writing top-level names to /private/var/folders/m5/vhnyyjg16gd352x6wdhpjysh0000gn/T/pip-pip-egg-info-06hnnl2t/psycopg2.egg-info/top_level.txt
    writing manifest file '/private/var/folders/m5/vhnyyjg16gd352x6wdhpjysh0000gn/T/pip-pip-egg-info-06hnnl2t/psycopg2.egg-info/SOURCES.txt'
    
    Error: pg_config executable not found.
    
    pg_config is required to build psycopg2 from source.  Please add the directory
    containing pg_config to the $PATH or specify the full executable path with the
    option:
    
        python setup.py build_ext --pg-config /path/to/pg_config build ...
    
    or with the pg_config option in 'setup.cfg'.
    
    If you prefer to avoid building psycopg2 from source, please install the PyPI
    'psycopg2-binary' package instead.
    
    For further information please check the 'doc/src/install.rst' file (also at
    <https://www.psycopg.org/docs/install.html>).
    
    ----------------------------------------
WARNING: Discarding https://files.pythonhosted.org/packages/fd/ae/98cb7a0cbb1d748ee547b058b14604bd0e9bf285a8e0cc5d148f8a8a952e/psycopg2-2.8.6.tar.gz#sha256=fb23f6c71107c37fd667cb4ea363ddeb936b348bbd6449278eb92c189699f543 (from https://pypi.org/simple/psycopg2/) (requires-python:>=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*). Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
  Using cached psycopg2-2.8.5.tar.gz (380 kB)
    ERROR: Command errored out with exit status 1:
     command: /opt/homebrew/opt/[email protected]/bin/python3.9 -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/m5/vhnyyjg16gd352x6wdhpjysh0000gn/T/pip-install-9wiqe2nr/psycopg2_97efcf6747c249769acdc8430ba4238f/setup.py'"'"'; __file__='"'"'/private/var/folders/m5/vhnyyjg16gd352x6wdhpjysh0000gn/T/pip-install-9wiqe2nr/psycopg2_97efcf6747c249769acdc8430ba4238f/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /private/var/folders/m5/vhnyyjg16gd352x6wdhpjysh0000gn/T/pip-pip-egg-info-ap2am61r
         cwd: /private/var/folders/m5/vhnyyjg16gd352x6wdhpjysh0000gn/T/pip-install-9wiqe2nr/psycopg2_97efcf6747c249769acdc8430ba4238f/
    Complete output (23 lines):
    running egg_info
    creating /private/var/folders/m5/vhnyyjg16gd352x6wdhpjysh0000gn/T/pip-pip-egg-info-ap2am61r/psycopg2.egg-info
    writing /private/var/folders/m5/vhnyyjg16gd352x6wdhpjysh0000gn/T/pip-pip-egg-info-ap2am61r/psycopg2.egg-info/PKG-INFO
    writing dependency_links to /private/var/folders/m5/vhnyyjg16gd352x6wdhpjysh0000gn/T/pip-pip-egg-info-ap2am61r/psycopg2.egg-info/dependency_links.txt
    writing top-level names to /private/var/folders/m5/vhnyyjg16gd352x6wdhpjysh0000gn/T/pip-pip-egg-info-ap2am61r/psycopg2.egg-info/top_level.txt
    writing manifest file '/private/var/folders/m5/vhnyyjg16gd352x6wdhpjysh0000gn/T/pip-pip-egg-info-ap2am61r/psycopg2.egg-info/SOURCES.txt'
    
    Error: pg_config executable not found.
    
    pg_config is required to build psycopg2 from source.  Please add the directory
    containing pg_config to the $PATH or specify the full executable path with the
    option:
    
        python setup.py build_ext --pg-config /path/to/pg_config build ...
    
    or with the pg_config option in 'setup.cfg'.
    
    If you prefer to avoid building psycopg2 from source, please install the PyPI
    'psycopg2-binary' package instead.
    
    For further information please check the 'doc/src/install.rst' file (also at
    <https://www.psycopg.org/docs/install.html>).
    ==============================================================================
    Its beginning of the error, After that its tried to install each previous version of Psycopg 
    ==============================================================================

Also, I tried to install with source code with :

git clone https://github.com/psycopg/psycopg2.git
cd psycopg2
python3 setup.py build

Its return as error like that:

running build
running build_py
running build_ext

Error: pg_config executable not found.

pg_config is required to build psycopg2 from source.  Please add the directory
containing pg_config to the $PATH or specify the full executable path with the
option:

    python setup.py build_ext --pg-config /path/to/pg_config build ...

or with the pg_config option in 'setup.cfg'.

If you prefer to avoid building psycopg2 from source, please install the PyPI
'psycopg2-binary' package instead.

For further information please check the 'doc/src/install.rst' file (also at
<https://www.psycopg.org/docs/install.html>).

Also
python3 setup.py build return the same error as python3 setup.py build

How can I install it?

Upvotes: 44

Views: 43728

Answers (8)

topherPedersen
topherPedersen

Reputation: 671

I had some trouble today installing psycopg2 on my new Macbook Pro with an M3 chip. To fix this, I installed postgres from the command line using brew, then ran the recommended commands to add postgres to my PATH, closed my terminal window where I was attempting to install psycopg2, then opened a new terminal window and installed psycopg2:

$ brew install postgresql@15

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

$ export LDFLAGS="-L/opt/homebrew/opt/postgresql@15/lib"

$ export CPPFLAGS="-I/opt/homebrew/opt/postgresql@15/include"

Open a NEW terminal window to install psycopg2:

$ pip install psycopg2

Upvotes: 4

Tim Nieradzik
Tim Nieradzik

Reputation: 309

Install the PostgreSQL and OpenSSL system libraries:

brew install libpq --build-from-source
brew install openssl

Configure the linker and header search paths by setting the LDFLAGS and CPPFLAGS environment variables. Instead of hard-coding the OpenSSL version, we determine the version path using brew --prefix:

export LDFLAGS="-L$(brew --prefix openssl)/lib"
export CPPFLAGS="-I$(brew --prefix openssl)/include"

Now you should be able to install the psycopg2 dependency by running pip:

pip install psycopg2

These instructions are compatible with Apple Silicon.

Upvotes: 1

Oleksandr Venger
Oleksandr Venger

Reputation: 11

brew install openssl

Check out which version of openssl is installed!

export CPPFLAGS="-I/opt/homebrew/opt/openssl@**{YOUR_OPENSSL_VERSION}**/include"

python -V

export LDFLAGS="-L/opt/homebrew/opt/openssl@**{YOUR_OPENSSL_VERSION}**/lib -L${HOME}/.pyenv/versions/**{YOUR_PYTHON_VERSION}**/lib"

pip install psycopg2-binary

works on mac m1

Upvotes: 1

RusskiT
RusskiT

Reputation: 236

All you need to do is install the binary:

pip install psycopg2-binary

Everything has been running fine since. I referenced this link and just skipped straight to the last step. If installing the binary doesn't work on the first try, I would take a look at the other steps in this article.

Upvotes: 7

Dhia
Dhia

Reputation: 10609

Following this steps solved my problem:

❯ brew install postgresql@14
# If you open a new terminal tab you will see that pg_config is available
❯ export CPPFLAGS="-I/opt/homebrew/opt/[email protected]/include"
❯ export LDFLAGS="-L/opt/homebrew/opt/[email protected]/lib -L${HOME}/.pyenv/versions/3.10.7/lib" # use your current python version
❯ python -V
Python 3.10.7 # make sure it matches the same above configuration
❯ pip install psycopg2-binary==2.8.5

Upvotes: 4

user3661992
user3661992

Reputation: 491

Before reverting to any of the older answers, I would recommend trying the following:

Install Postgres using brew install postgres, then pip3 install psycopg2.

Worked on Mac M1, Monterey, Python 3.9.

Thanks to Kori Vernon.

Upvotes: 36

Shubhank Saxena
Shubhank Saxena

Reputation: 101

Thanks to active development, now there is a new release of psycopg2 as well as psycopg2-binary which is compatible with M1 devices. Ver. 2.9.1 works smooth. Tried and tested Reference - https://github.com/psycopg/psycopg2/issues/1200

Upvotes: 7

user2884165
user2884165

Reputation: 1100

try this:

brew install libpq --build-from-source
brew install openssl

export LDFLAGS="-L/opt/homebrew/opt/[email protected]/lib -L/opt/homebrew/opt/libpq/lib"
export CPPFLAGS="-I/opt/homebrew/opt/[email protected]/include -I/opt/homebrew/opt/libpq/include"

pip3 install psycopg2

Upvotes: 100

Related Questions