elastalert: ImportError: failed to find libmagic. Check your installation

I believe this may be an issue with the magic package used by thehive4py/api.py

After installing elastalert v 0.1.3.6 I get the following stacktrace when I try to run an elastalert test with a folder of rules

Traceback (most recent call last):
  File "/home/elastalert/.local/bin/elastalert", line 11, in <module>
    load_entry_point('elastalert==0.1.36', 'console_scripts', 'elastalert')()
  File "/usr/lib/python2.7/site-packages/pkg_resources/__init__.py", line 480, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/usr/lib/python2.7/site-packages/pkg_resources/__init__.py", line 2691, in load_entry_point
    return ep.load()
  File "/usr/lib/python2.7/site-packages/pkg_resources/__init__.py", line 2322, in load
    return self.resolve()
  File "/usr/lib/python2.7/site-packages/pkg_resources/__init__.py", line 2328, in resolve
    module = __import__(self.module_name, fromlist=['__name__'], level=0)
  File "/home/elastalert/.local/lib/python2.7/site-packages/elastalert/elastalert.py", line 21, in <module>
    from alerts import DebugAlerter
  File "/home/elastalert/.local/lib/python2.7/site-packages/elastalert/alerts.py", line 31, in <module>
    from thehive4py.api import TheHiveApi
  File "/home/elastalert/.local/lib/python2.7/site-packages/thehive4py/api.py", line 7, in <module>
    import magic
  File "/home/elastalert/.local/lib/python2.7/site-packages/magic.py", line 181, in <module>
    raise ImportError('failed to find libmagic.  Check your installation')
ImportError: failed to find libmagic.  Check your installation
RMTMACSIERRA01:infrastructure-cm lucas.rudd$ docker run --rm -it -e ES_HOST=search-pss-pf-prod-es-51-gcm4nbuhxjuo6telgbszkp3i7u.us-east-1.es.amazonaws.com -e AWS_PROFILE=asurion-soluto-nonprod.dev -v ~/.aws:/home/elastalert/.aws  -v ~/Desktop/rules/:/home/elastalert/rules --entrypoint elastalert asurionpss/elastalert:test --verbose --es_debug
Traceback (most recent call last):
  File "/home/elastalert/.local/bin/elastalert", line 11, in <module>
    load_entry_point('elastalert==0.1.36', 'console_scripts', 'elastalert')()
  File "/usr/lib/python2.7/site-packages/pkg_resources/__init__.py", line 480, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/usr/lib/python2.7/site-packages/pkg_resources/__init__.py", line 2691, in load_entry_point
    return ep.load()
  File "/usr/lib/python2.7/site-packages/pkg_resources/__init__.py", line 2322, in load
    return self.resolve()
  File "/usr/lib/python2.7/site-packages/pkg_resources/__init__.py", line 2328, in resolve
    module = __import__(self.module_name, fromlist=['__name__'], level=0)
  File "/home/elastalert/.local/lib/python2.7/site-packages/elastalert/elastalert.py", line 21, in <module>
    from alerts import DebugAlerter
  File "/home/elastalert/.local/lib/python2.7/site-packages/elastalert/alerts.py", line 31, in <module>
    from thehive4py.api import TheHiveApi
  File "/home/elastalert/.local/lib/python2.7/site-packages/thehive4py/api.py", line 7, in <module>
    import magic
  File "/home/elastalert/.local/lib/python2.7/site-packages/magic.py", line 181, in <module>
    raise ImportError('failed to find libmagic.  Check your installation')
ImportError: failed to find libmagic.  Check your installation

After manually installing libmagic the issue persists.

Has anyone else experienced this issue?

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 30
  • Comments: 57 (1 by maintainers)

Commits related to this issue

Most upvoted comments

This worked for me:

pip uninstall python-magic pip install python-magic-bin==0.4.14

Solved it by running

brew install libmagic

This worked for me:

pip uninstall python-magic pip install python-magic-bin==0.4.14

@primeroz , Can you tell me that how do you solve this problem, just install libmagic when build docker image?

We’ve just added apk add libmagic.

Use the method by @synhershko i.e, 1. pip uninstall python-magic 2. pip install python-magic-bin Except, don’t include the python-magic-bin version!

My laptop is MacBook Pro M1, and I succeed with the following process:

  1. brew install libmagic
  2. pip install python-magic (since pip install python-magic-bin not working)

In my case (M1 MacBook), it was all about telling the system where to find the library files. So after obvious brew install libmagic (on M1s, homebrew installs libmagic to /opt/homebrew/Cellar/libmagic), it was necessary to create a symbolic link from the library files to the place where the system expects libmagic to be located, which is /usr/local/lib.

Therefore, this worked for me:

sudo ln -s /opt/homebrew/Cellar/libmagic/5.41/lib/libmagic.dylib /usr/local/lib/libmagic.dylib

FYI: my version of python-magic is 0.4.18 (and I wasn’t willing to downgrade as suggested above)

Solved it by running

brew install libmagic

In my case this is what helped (MacOS obviously…)

Installation

The current stable version of python-magic is available on PyPI and can be installed by running pip install python-magic.

Other sources:

PyPI: http://pypi.python.org/pypi/python-magic/ GitHub: https://github.com/ahupp/python-magic This module is a simple wrapper around the libmagic C library, and that must be installed as well:

Debian/Ubuntu

$ sudo apt-get install libmagic1

Windows

You’ll need DLLs for libmagic. @julian-r has uploaded a version of this project that includes binaries to PyPI: https://pypi.python.org/pypi/python-magic-bin/0.4.14

Other sources of the libraries in the past have been File for Windows . You will need to copy the file magic out of [binary-zip]\share\misc, and pass its location to Magic(magic_file=...).

If you are using a 64-bit build of python, you’ll need 64-bit libmagic binaries which can be found here: https://github.com/pidydx/libmagicwin64. Newer version can be found here: https://github.com/nscaife/file-windows.

OSX

When using Homebrew: brew install libmagic When using macports: port install file

Ref:- https://pypi.org/project/python-magic/

On my Macbook with M1 chip. This worked for me.

  • brew install libmagic
  • pip install python-magic-bin==0.4.14

I’m on MacOS m1 and I fix this issue by installing libmagic using Homebrew:

brew install libmagic

This worked for me: pip uninstall python-magic pip install python-magic-bin==0.4.14

it works thank you

pip install python-magic-bin==0.4.14
ERROR: Could not find a version that satisfies the requirement python-magic-bin==0.4.14 (from versions: none)
ERROR: No matching distribution found for python-magic-bin==0.4.14

Is not for Linux, the package is only for Widnows and MacOS https://pypi.org/project/python-magic-bin/0.4.14/#files

For me it works (22.04 Ubuntu):

pip uninstall python-magic
apt install libmagic1
pip install python-magic

This worked for me:

pip uninstall python-magic pip install python-magic-bin==0.4.14

What is this for a crap? I get this error on Windows and Mac OS.

This worked for me:

pip uninstall python-magic pip install python-magic-bin==0.4.14

I love you.

On my Macbook with M1 chip. This worked for me.

  • brew install libmagic
  • pip install python-magic-bin==0.4.14

I tried this by only doing brew install libmagic and having the python-magic already works

Thanks!!

This worked for me: pip uninstall python-magic pip install python-magic-bin==0.4.14

This works for my windows 11.

This worked for me:

pip uninstall python-magic pip install python-magic-bin==0.4.14

it really works!

When I run pip uninstall python-magic and then pip install python-magic-bin==0.4.14 I get the error message: ERROR: Could not find a version that satisfies the requirement python-magic-bin==0.4.14 (from versions: none) ERROR: No matching distribution found for python-magic-bin==0.4.14. Has anyone else seen this error?

This worked for me:

pip uninstall python-magic pip install python-magic-bin==0.4.14

This worked for me too

In my case (M1 MacBook), it was all about telling the system where to find the library files. So after obvious brew install libmagic (on M1s, homebrew installs libmagic to /opt/homebrew/Cellar/libmagic), it was necessary to create a symbolic link from the library files to the place where the system expects libmagic to be located, which is /usr/local/lib.

Therefore, this worked for me:

sudo ln -s /opt/homebrew/Cellar/libmagic/5.41/lib/libmagic.dylib /usr/local/lib/libmagic.dylib

FYI: my version of python-magic is 0.4.18 (and I wasn’t willing to downgrade as suggested above)

This worked for me (M2 pro). Just make sure you use the correct version inside the /opt/homebrew/Cellar/libmagic/.

pip uninstall python-magic
apt install libmagic1
pip install python-magic

Worked for me too.

When I run pip uninstall python-magic and then pip install python-magic-bin==0.4.14 I get the error message: ERROR: Could not find a version that satisfies the requirement python-magic-bin==0.4.14 (from versions: none) ERROR: No matching distribution found for python-magic-bin==0.4.14. Has anyone else seen this error?

Happened to me, solved using python-magic-debian-bin==0.4.25

We had this error on a popular Jupyter Notebook image, scipy-notebook. The fix ended up being to install libmagic with the Anaconda package manager:

conda install libmagic

Tracking down this error was a pain in the neck, so I hope this helps someone.

neither solution worked for me

I just installed libmagic and it fixed that error. I am running an alpine container

@synhershko Your solution doesn’t work for me because python-magic requirement is >=0.4.15. Here is the error message:

pkg_resources.DistributionNotFound: The ‘python-magic>=0.4.15’ distribution was not found and is required by elastalert