12

When I attempt to deploy my application to Heroku I receive the following error:

File "/app/project/app/_ _init__.py", line 22, in <module>
File "/app/project/app/views.py", line 6, in <module>
import cv2
from .cv2 import *
File "/app/.heroku/python/lib/python3.6/site-packages/cv2/_ _init__.py", line 4, in <module>
2018-03-24T20:40:55.986945+00:00 app[web.1]: ImportError: libSM.so.6: cannot open shared object file: No such file or directory```

OpenCV is unable to find the libsm directory, however this application runs correctly locally. I have tried using a specific buildpack however those did not seem to find my site-packages folder.

How do I use openCV (python) on Heroku?

9 Answers 9

39

You can install these missing libraries taking advantage of the heroku-buildpack-apt.

At the time of this writing, I have succesfully done it for this repo, with the following steps:

  1. Add heroku-buildpack-apt to your buildpacks on Heroku platform
  2. Create a file named Aptfile and add the following libs:
libsm6
libxrender1
libfontconfig1
libice6

(one per line). Example here.

Edit: in newer versions of OpenCV, you need only to list python-opencv on the Aptfile, as seen in the docs.

Sign up to request clarification or add additional context in comments.

3 Comments

It worked for me, many thanks! But I would like to know what I've actually done... I don't have any experience with these build packs.
@mimic, buildpacks can be seen as a "set of scripts, and depending on the programming language, the scripts will retrieve dependencies, output generated assets or compiled code, and more". In my app, I wanted to use Python3, so I added the Python buildpack on Heroku. As some needed libraries were still missing from the system, the heroku-buildpack-apt let's you manage packages in a way similar to how APT works.
It worked for me too. Thanks a lot! This should be marked as the solution.
11

Use opencv-python-headless as it is out of libSM6 dependency. check this out.
Add the following line your requirements.txt and remove the old open-cv entry:

opencv-python-headless==4.2.0.32

1 Comment

perfect! I was trying many things, at last this worked for me, Thank you:)
6

New Aptfile and requirements.txt attributes works for me:

in Aptfile

libsm6
libxrender1
libfontconfig1
libice6

in requirements.txt

opencv-python-headless==4.2.0.32

Remember to have the Buildpack included in settings.

https://github.com/heroku/heroku-buildpack-apt

Comments

1

You have to install some dependencies, as Heroku will not automatically do it for you.

  1. Add an Aptfile in your project directory and add the below file
  • libsm6

  • libxrender1

  • libfontconfig1

  • libice6

    NOTE: Aptfile should not have any .txt or any other extension. Just like the Procfile

  1. Push the edited code to Github

  2. In heroku dashboard,
    goto your-app --> settings --> buildpacks --> add buildpacks --> https://github.com/heroku/heroku-buildpack-apt.git
    copy and paste this link --> add buildpack

  3. Deploy your app

enter image description here

Comments

0

For windows users, be sure to use unix style line endings in the Aptfile when following @Lelo suggestion above

1 Comment

@Bawantha please find an explanation here: cs.toronto.edu/~krueger/csc209h/tut/….
0

Referring to Lelo's answer regarding the installation of libraries, OpenCV has changed their required libraries (4.4.0 at the time of writing).

Hence, to get the latest ones, you just need python-opencv in the Aptfile instead of the other libraries.

This was referred to Install OpenCV-Python in Ubuntu.

1 Comment

For me, python3-opencv (note the added 3) worked, and without it Heroku reported that the package was not found.
0

In my case, I was having error like ImportError: libSM.so.6 , in this case one needs to add libsm1 (all in lower case) to aptfile and it worked.

Comments

0

The error you're encountering is related to a missing shared library libGL.so.1 that is required by the OpenCV library (cv2) used in your code. This issue is commonly seen when running applications in headless environments like Heroku, which doesn't have the necessary graphical libraries installed.

To resolve this issue, you can try the following steps:

  1. Update your Aptfile to include the libgl1-mesa-dev package, which provides the libGL.so.1 library:

    tesseract-ocr
    tesseract-ocr-eng
    libsm6
    libxrender1
    libfontconfig1
    libice6
    libglu1
    libgl1-mesa-dev
    
  2. Commit and push the changes to your repository.

  3. Trigger a new deployment on Heroku. The updated Aptfile will prompt Heroku to install the additional dependencies specified.

By adding libgl1-mesa-dev, you should be able to resolve the missing libGL.so.1 error and successfully execute your code on Heroku.

Comments

-1

Use this instead .it worked for me.

pip install opencv-contrib-python

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.