348

When I run the following command:

sudo pip install python-ldap

I get this error:

In file included from Modules/LDAPObject.c:9:

Modules/errors.h:8: fatal error: lber.h: No such file or directory

How can I fix this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
VacuumTube
  • 4,021
  • 4
  • 22
  • 19

27 Answers27

618

The python-ldap is based on OpenLDAP, so you need to have the development files (headers) in order to compile the Python module. If you're on Ubuntu, the package is called libldap2-dev.

Debian/Ubuntu:

sudo apt-get install libsasl2-dev python-dev libldap2-dev libssl-dev

RedHat/CentOS:

sudo yum install python-devel openldap-devel
Bhavesh Odedra
  • 10,990
  • 12
  • 33
  • 58
AndiDog
  • 68,631
  • 21
  • 159
  • 205
  • 5
    Thanks for that. It's got further it's now failing with sasl.h no such file or directory – VacuumTube Jan 22 '11 at 14:57
  • 75
    @VacuumTube: Missing headers are almost always a sign for missing `-dev` packages (sometimes also named `-devel`). Just do `apt-cache search sasl | grep dev` and you'll probably find the right package - I would guess it's `libsasl2-dev`. – AndiDog Jan 22 '11 at 15:02
  • 7
    Anything for windows users :( Same error, no solutions so far – Shailen Aug 13 '13 at 14:37
  • 5
    You don't have to compile from source on Windows. Just use one of the installers from https://pypi.python.org/pypi/python-ldap/. – Semmel Sep 19 '13 at 02:14
  • 3
    Also, Windows users can extract the .msi installer @Semmel mentioned to install into a virtualenv: [How to install python-ldap on a python 2.7 virtualenv on windows without compiling](http://stackoverflow.com/questions/15918188/how-to-install-python-ldap-on-a-python-2-7-virtualenv-on-windows-without-compili) (see update 2) – Dave Oct 29 '13 at 17:11
  • On Windows you can also use Chris Gohlke's excellent resource for Windows installers: http://www.lfd.uci.edu/~gohlke/pythonlibs/#python-ldap – Josh Hemann Feb 27 '14 at 17:18
  • Thanks @AndiDog. the issue was resolved as soon as I installed the `-dev` library :) – Babak K Aug 04 '15 at 20:27
  • 2
    Full list should be sudo apt-get install libsasl2-dev python-dev libldap2-dev libssl-dev – James Nov 10 '15 at 02:35
  • Notice that you need to run 'sudo apt-get update' first. – Slark Apr 10 '17 at 11:28
  • Well this is odd. I have installed `libldap2-dev` but the headers haven't been installed. Everything else in the package (libs, man pages) has. Edit: --reinstall and it's fine. – OrangeDog Mar 23 '18 at 17:39
  • @TrinhHoangNhu: Why this that the full list? Can you explain how you found those dependencies? – Lii Apr 27 '18 at 09:05
  • @Lii I found out that one since 2015. It may not correct now – James Apr 27 '18 at 16:37
  • 4
    Use `python3-dev` instead of `python-dev` if you are using Python 3. – phoenix Aug 09 '19 at 11:43
  • Sometimes also known as `python36-devel` or `python3-devel.x86_64` or `python36-devel.x86_64` (aws). When in doubt: `yum search python | grep -i dev` – Amit Naidu Aug 25 '20 at 23:56
  • 1
    Worth noting in 2022 on Debian 11, I only needed to install `libldap2-dev` and `libsasl2-dev`. (Already had `python3-virtualenv`.) – Seth Falco Jul 16 '22 at 06:57
  • for me it didn't find `python-dev` so I did this: `sudo apt-get install libsasl2-dev python-dev-is-python3 libldap2-dev libssl-dev` – Karina Klinkevičiūtė Jan 06 '23 at 20:23
157

To install python-ldap successfully with pip, following development libraries are needed (package names taken from ubuntu environment):

sudo apt-get install -y python-dev libldap2-dev libsasl2-dev libssl-dev
I159
  • 29,741
  • 31
  • 97
  • 132
jmu
  • 3,619
  • 1
  • 23
  • 12
74

On CentOS/RHEL 6, you need to install:

sudo yum install python-devel
sudo yum install openldap-devel

and yum will also install cyrus-sasl-devel as a dependency. Then you can run:

pip-2.7 install python-ldap
Mike Pennington
  • 41,899
  • 19
  • 136
  • 174
Banjer
  • 8,118
  • 5
  • 46
  • 61
48

"Don't blindly remove/install software"

In a Ubuntu or Debian based distro, you can use apt-file to find the name of the exact package that includes the missing header file.

# do this once
sudo apt-get install apt-file
sudo apt-file update

$ apt-file search lber.h
libldap2-dev: /usr/include/lber.h

As you could see from the output of apt-file search lber.h, you'd just need to install the package libldap2-dev.

sudo apt-get install libldap2-dev
wovano
  • 4,543
  • 5
  • 22
  • 49
Gayan Weerakutti
  • 11,904
  • 2
  • 71
  • 68
35

In Ubuntu it looks like this :

 $ sudo apt-get install python-dev libldap2-dev libsasl2-dev libssl-dev
 $ sudo pip install python-ldap
Abdoul Ndiaye
  • 193
  • 1
  • 7
Jeff Sheffield
  • 5,768
  • 3
  • 25
  • 32
17

Windows: I completely agree with the accepted answer, but digging through the comments took a while to get to the meat of what I needed. I ran across this specific problem with Reviewboard on Windows using the Bitnami. To give an answer for windows then, I used this link mentioned in the comments:

Then, executed the following commands

easy_install pip
pip install python_ldap-2.4.20-cp27-none_win32.whl

(because I had python 2.7 and a 32bit install at that)

easy_install python-ldap
Jonathan
  • 6,741
  • 7
  • 52
  • 69
archangel76
  • 1,544
  • 2
  • 11
  • 18
14

For those having the same issue of missing Iber.h on Alpine Linux, in a docker image that you are trying to adapt to Alpine for instance.

The package you are looking for is: openldap-dev

So run

apk add openldap-dev

Available from version 3.3 up to Edge

Available for both armhf and x86_64 Architectures.

Ilhicas
  • 1,429
  • 1
  • 19
  • 26
6

On Fedora 22, you need to do this instead:

sudo dnf install python-devel
sudo dnf install openldap-devel
chfw
  • 4,502
  • 2
  • 29
  • 32
5

I had problems with the installation on Windows, so one of the solutions is to install the ldap package manually.

A few steps:

  • Go to the page pyldap or/and python-ldap and download the latest version *whl.
  • Open a console then cd to where you've downloaded your file like some-package.whl and use:
pip install some-package.whl

The current version for pyldap is 2.4.45. On a concrete example the installation would be:

pip install .\pyldap-2.4.45-cp37-cp37m-win_amd64.whl

# or
pip install .\python_ldap‑3.3.1‑cp39‑cp39‑win_amd64.whl

Output:

Installing collected packages: pyldap
Successfully installed pyldap-2.4.45

EDIT

You can install the proper version for Python-3.X though using following command:

# if pip3 is the default pip alias for python-3
pip3 install python3-ldap

# otherwise 
pip install python3-ldap

Also here is the link of PiPy package for further information: python3-ldap 0.9.8.4

OR

ldap3 is a strictly RFC 4510 conforming LDAP V3 pure Python client library. The same codebase runs in Python 2, Python 3, PyPy and PyPy3: https://github.com/cannatag/ldap3

pip install ldap3
from ldap3 import Server, Connection, SAFE_SYNC

server = Server('my_server')
conn = Connection(server, 'my_user', 'my_password', client_strategy=SAFE_SYNC, auto_bind=True)

status, result, response, _ = conn.search('o=test', '(objectclass=*)') 
# usually you don't need the original request (4th element of the returned tuple)
Milovan Tomašević
  • 6,823
  • 1
  • 50
  • 42
4

On openSUSE you need to install the packages openldap2-devel, cyrus-sasl-devel, python-devel and libopenssl-devel.

zypper install openldap2-devel cyrus-sasl-devel python-devel libopenssl-devel

Christian Berendt
  • 3,416
  • 2
  • 13
  • 22
4

Python 3 does not support python-ldap. Rather install ldap3.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Agnel Amodia
  • 765
  • 8
  • 18
4

For alpine docker

apk add openldap-dev

if the python version is 3 and above try

pip install python3-ldap
Akhilraj N S
  • 9,049
  • 5
  • 36
  • 42
3

For most systems, the build requirements are now mentioned in python-ldap's documentation, in the "Installing" section.

If anything is missing for your system (or your system is missing entirely), please let maintainer know! (As of 2018, I am the maintainer, so a comment here should be enough. Or you can send a pull request or mail.)

Petr Viktorin
  • 65,510
  • 9
  • 81
  • 81
  • Thanks, might be worth noting that if you're running python3, you would need python3-devel, rather than python-devel. – Jamie Jun 26 '18 at 01:09
3

To correct the error due to dependencies to install the python-ldap: Windows 7 and Windows 10

Download the whl file:

http://www.lfd.uci.edu/~gohlke/pythonlibs/#python-ldap.

Python 3.6 suit with

python_ldap-3.2.0-cp36-cp36m-win_amd64.whl

Deploy the file in:

C:\python36\Scripts\

Install it with:

python -m pip install python_ldap-3.2.0-cp36-cp36m-win_amd64.whl
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tono Kuriakose
  • 718
  • 6
  • 19
3
sudo apt-get install build-essential python3-dev python2.7-dev libldap2-dev libsasl2-dev slapd ldap-utils python-tox lcov valgrind
Thomas John
  • 2,138
  • 2
  • 22
  • 38
2

On OSX, you need the xcode CLI tools. Just open a terminal and run:

xcode-select --install
Andy Fraley
  • 1,043
  • 9
  • 16
2

For Arch Linux/Manjaro, for me, it helped with the following command:

yay libldap24
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
zifter
  • 65
  • 6
2

As of December 2021 there was/is a strange problem with the ldap library (at least in Arch Linux/Manjaro).

While installing python-ldap (at 'Building wheel for python-ldap'), I got the message 'ERROR: Failed building wheel for python-ldap':

  /usr/bin/ld: cannot find -lldap_r
  collect2: error: ld returned 1 exit status
  error: command '/usr/bin/gcc' failed with exit code 1

A workaround is provided at Support for OpenLDAP 2.5+ #432.

I cite:

As a workaround create the file /usr/lib64/libldap_r.so with content INPUT ( libldap.so ). The approach works on all systems that use a GNU ld-compatible linker.

# cat > /usr/lib64/libldap_r.so << EOF
INPUT ( libldap.so )
EOF
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MacMartin
  • 2,366
  • 1
  • 24
  • 27
1

As a general solution to install Python packages with binary dependencies [1] on Debian/Ubuntu:

sudo apt-get build-dep python-ldap
# installs system dependencies (but not the package itself)
pew workon my_virtualenv # enter your virtualenv
pip install python-ldap

You'll have to check the name of your Python package on Ubuntu versus PyPI. In this case they're the same.

Obviously doesn't work if the Python package is not in the Ubuntu repos.

[1] I learnt this trick when trying to pip install matplotlib on Ubuntu.

lofidevops
  • 15,528
  • 14
  • 79
  • 119
1

In FreeBSD 11:

pkg install openldap-client # for lber.h
pkg install cyrus-sasl # if you need sasl.h
pip install python-ldap
xsc27
  • 146
  • 1
  • 3
1

Ubuntu

The package python-dev is deprecated or not longer available. You can use this instead:

sudo apt-get install libsasl2-dev python2-dev python2 python-dev-is-python3 libldap2-dev libssl-dev
pip install python-ldap

---Edit--- trying to install python-dev

fp007
  • 412
  • 1
  • 4
  • 18
  • Re *"is deprecated"*: On what version of Ubuntu? And what version of Ubuntu was it tested on? Please respond by [editing (changing) your answer](https://stackoverflow.com/posts/76695713/edit), not here in comments (but *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***[without](https://meta.stackexchange.com/a/131011)*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen Jul 20 '23 at 08:59
  • Why `python2-dev` and `python2`? Why are they necessary? – Peter Mortensen Jul 20 '23 at 09:01
  • 1
    From [another answer](https://stackoverflow.com/questions/4768446/i-cant-install-python-ldap/49760133#49760133): *"Python 3 does not support python-ldap. Rather install ldap3."* – Peter Mortensen Jul 20 '23 at 09:24
  • @PeterMortensen added screenshot – fp007 Jul 20 '23 at 21:53
  • Please review *[Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/questions/285551/)* (e.g., *"Images should only be used to illustrate problems that* ***can't be made clear in any other way,*** *such as to provide screenshots of a user interface."*) (it covers answers as well). And also [Edit: or Update: (bad practice on Stack Overflow)](https://meta.stackexchange.com/questions/131009/what-should-i-keep-out-of-my-posts-and-titles/131011#131011) (near ***"Changelogs"***) – Peter Mortensen Jul 31 '23 at 12:10
  • To repeat a previous comment: *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***[without](https://meta.stackexchange.com/a/131011)*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** `"Edit:", "Update:", or similar` - the answer should appear as if it was written today!!!!!!!!!!!!!!!!!!!!! What isn't clear? – Peter Mortensen Jul 31 '23 at 12:15
0

If you're working with Windows machines, you can find the 'python-ldap' wheel in this link and then you can install it.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
pietà
  • 760
  • 1
  • 11
  • 37
0

For those who are using Alpine Linux,

apk add openldap-dev
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vivin Veerali
  • 317
  • 3
  • 1
0

Try:

ARCHFLAGS="-arch x86_64" pip3 install python-ldap
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 4
    Can you please explain what this does? – mousetail Jun 30 '21 at 07:05
  • Answer needs supporting information Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](https://stackoverflow.com/help/how-to-answer). – moken Jul 21 '23 at 12:02
0

Adding also libzbar-dev solved for me the installation of python-ldap when building DOCKER

The full command becomes:

apt-get install -y python-dev libldap2-dev libsasl2-dev libssl-dev libzbar-dev
Marco Cerliani
  • 21,233
  • 3
  • 49
  • 54
0

A hack answer for FreeBSD 13.1 (yes, I know this is deep South of best practices, but I just needed a quick fix):

pkg install openldap24-client

cd /usr/local/include/python3.9
ln -s ../<all of the below> .

lber.h
lber_types.h
ldap.h
ldap_cdefs.h
ldap_features.h
ldap_schema.h
ldap_utf8.h
openldap.h
sasl

pip install python-ldap
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Bjørn H. Sandvik
  • 533
  • 4
  • 10
  • 22
0

You need to install libldap2-dev (sudo apt-get install libldap2-dev), but python-dev is also important. Remember to use the version you have (sudo apt-get install python3.8-dev or sudo apt-get install pythonX.X-dev)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131