# Ansible pip installing psycopg2 from source when binary requested

**URL:** <https://forum.ansible.com/t/ansible-pip-installing-psycopg2-from-source-when-binary-requested/10363>\
**Category:** Get Help\
**Tags:** pip, ansible-core\
**Created:** [October 17, 2024, 2:44pm UTC](https://forum.ansible.com/t/ansible-pip-installing-psycopg2-from-source-when-binary-requested/10363 "2024-10-17T14:44:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bgoulet00](https://avatars.discourse-cdn.com/v4/letter/b/6bbea6/32.png) [@bgoulet00](https://forum.ansible.com/u/bgoulet00)\
**Post date:** [October 17, 2024, 2:44pm UTC](https://forum.ansible.com/t/ansible-pip-installing-psycopg2-from-source-when-binary-requested/10363/1 "2024-10-17T14:44:19Z")

</div>

i’m trying to install psycopg2-binary but when i do it via ansible it attempts to install from source, even though i’m outright requesting the binary package

from the terminal, the following works just fine and installs the binary package  
`sudo pip3 install psycopg2-binary`

when using the pip module it attempts to install from source and then cries about dependencies it can’t find.

```auto
    - name: install psycopg2-binary
      pip:
        name: psycopg2-binary
      become: yes

```

i’ve also tried calling it from command just like at the terminal and this also results in it getting installed from source

```auto
    - name: install psycopg2-binary
      command: "pip3 install psycopg2-binary"
      become: yes

```

ansible core 2.15.12  
target system RHEL8, python3.8

---

<div class="post-metadata">

**Author:** ![gundalow](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/gundalow/32/99_2.png) [@gundalow](https://forum.ansible.com/u/gundalow)\
**Post date:** [October 17, 2024, 3:03pm UTC](https://forum.ansible.com/t/ansible-pip-installing-psycopg2-from-source-when-binary-requested/10363/2 "2024-10-17T15:03:16Z")

</div>

If you are on RHEL, would using the [dnf module](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/dnf_module.html) be better?

I suggest this, after [reading stackoverflow](https://stackoverflow.com/questions/5420789/how-to-install-psycopg2-with-pip-on-python#comment24347208_13158616)

> This isn’t just CentOS, I think. psycopg has native dependencies, so to build from source (which is what pip does), the PostgreSQL development libraries need to be there. (The C headers, maybe? I don’t know much about compiling native code.)

---

<div class="post-metadata">

**Author:** ![bgoulet00](https://avatars.discourse-cdn.com/v4/letter/b/6bbea6/32.png) [@bgoulet00](https://forum.ansible.com/u/bgoulet00)\
**Post date:** [October 17, 2024, 4:19pm UTC](https://forum.ansible.com/t/ansible-pip-installing-psycopg2-from-source-when-binary-requested/10363/3 "2024-10-17T16:19:14Z")

</div>

thanks gundalow! being sort of a noob i just followed all the instructions i saw online which directed install using pip. installing with dnf never occurred to me.  
that did resolved the issue.

i’m still rather confused why manual pip at the terminal installs the binary version fine while calling pip via ansible installs from source if anyone has an answer for that

---

<div class="post-metadata">

**Author:** ![gundalow](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/gundalow/32/99_2.png) [@gundalow](https://forum.ansible.com/u/gundalow)\
**Post date:** [October 18, 2024, 5:20am UTC](https://forum.ansible.com/t/ansible-pip-installing-psycopg2-from-source-when-binary-requested/10363/4 "2024-10-18T05:20:40Z")

</div>

Glad you got it working.

> [@bgoulet00](#):
>
> i’m still rather confused why manual pip at the terminal installs the binary version fine while calling pip via ansible installs from source if anyone has an answer for that

If you run `pip` directly on the system it will likely fail as well. From that stackoverflow article is seems like the package is always built from source, so your system is likely missing some other dependencies.

---

<div class="post-metadata">

**Author:** ![bgoulet00](https://avatars.discourse-cdn.com/v4/letter/b/6bbea6/32.png) [@bgoulet00](https://forum.ansible.com/u/bgoulet00)\
**Post date:** [October 18, 2024, 4:11pm UTC](https://forum.ansible.com/t/ansible-pip-installing-psycopg2-from-source-when-binary-requested/10363/5 "2024-10-18T16:11:46Z")

</div>

if i run pip directly on the system it works just fine, that’s where my confusion comes in. i did look at the article and noted they were having the issue running pip directly but that is not the case in my situation. guess it will remain an unsolved mystery. thanks again for the workaround.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [November 17, 2024, 4:11pm UTC](https://forum.ansible.com/t/ansible-pip-installing-psycopg2-from-source-when-binary-requested/10363/6 "2024-11-17T16:11:52Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
