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?

bad_coder
  • 11,289
  • 20
  • 44
  • 72
Brian Hamill
  • 2,466
  • 4
  • 12
  • 20

10 Answers10

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.

Lelo
  • 854
  • 11
  • 25
  • 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 Jul 28 '18 at 01:14
  • 1
    @mimic, [buildpacks](https://devcenter.heroku.com/articles/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](https://en.wikipedia.org/wiki/APT_(Debian)) works. – Lelo Jul 29 '18 at 22:18
  • It worked for me too. Thanks a lot! This should be marked as the solution. – Novus Dec 17 '19 at 02:03
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
geertjanvdk
  • 3,440
  • 24
  • 26
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
Zyncho
  • 407
  • 6
  • 12
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

סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
0

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

Paul Phillips
  • 21
  • 1
  • 6
  • @Bawantha please find an explanation here: http://www.cs.toronto.edu/~krueger/csc209h/tut/line-endings.html#:~:text=Text%20files%20created%20on%20DOS,(%22%5Cn%22). – Lelo Nov 05 '20 at 20:34
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.

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.

Hardik Kamboj
  • 81
  • 1
  • 7
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.

-1

Use this instead .it worked for me.

pip install opencv-contrib-python
Rupa
  • 9
  • 3
-4

you should install libsm6 and libxext6, run

$ sudo apt-get install -y libsm6 libxext6

but, since heroku doesn't provide root access, sort answer, you can't do that. you can choose another provider like Google Cloud Platform or AWS

cmiiw

Dery Rahman A
  • 35
  • 2
  • 7