148

When trying to import OpenCV, using import cv2 I get the following error:

/usr/local/lib/python2.7/dist-packages/cv2/__init__.py in <module>()
      7 
      8 # make IDE's (PyCharm) autocompletion happy
----> 9 from .cv2 import *
     10 
     11 # wildcard import above does not import "private" variables like __version__

ImportError: libSM.so.6: cannot open shared object file: No such file or directory

Not sure how to fix this - trying to play around with Google's new Colaboratory tool. Notebook is here: https://drive.google.com/file/d/0B7-sJqBiyjCcRmFkMzl6cy1iN0k/view?usp=sharing

Dmitry Rastorguev
  • 3,473
  • 4
  • 13
  • 14

9 Answers9

181

This fixed the problem by having it as the first two lines of the script:

!pip install opencv-python
!apt update && apt install -y libsm6 libxext6
!apt-get install -y libxrender-dev
Faizan Amin
  • 398
  • 5
  • 16
Dmitry Rastorguev
  • 3,473
  • 4
  • 13
  • 14
72

You need to add sudo . I did the following to get it installed :

sudo apt-get install libsm6 libxrender1 libfontconfig1

and then did that (optional! maybe you won't need it)

sudo python3 -m pip install opencv-contrib-python

FINALLY got it done !

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
smerllo
  • 3,117
  • 1
  • 22
  • 37
66

For CentOS, run this: sudo yum install libXext libSM libXrender

Caesar
  • 1,092
  • 12
  • 19
48

There is now a headless version of opencv-python which removes the graphical dependencies (like libSM). You can see the normal / headless version on the releases page (and the GitHub issue leading to this); just add -headless when installing, e.g.,

pip install opencv-python-headless
# also contrib, if needed
pip install opencv-contrib-python-headless
Nathan
  • 9,651
  • 4
  • 45
  • 65
  • 1
    I was having error `ImportError: libSM.so.6: cannot open shared object file: No such file or directory` once added headless it's gone – Vadim Aug 01 '19 at 14:28
  • also if anyone looking for a solution to run OpenCV on Google App Engine Flexible environment, this is the solution. – Oğulcan Çelik Mar 27 '20 at 11:02
21

May be the problem is with your python-opencv version. It's better to downgrade your version to 3.3.0.9 which does not include any GUI dependencies. Same question was found on GitHub here the link to the answer.

Raptor
  • 53,206
  • 45
  • 230
  • 366
billa-code
  • 549
  • 5
  • 18
  • 2
    pip install 'opencv-contrib-python==3.3.0.9' (after removing the current one, of course: pip uninstall opencv-contrib-python) -https://github.com/skvark/opencv-python/issues/44 – Alex Punnen Feb 28 '19 at 10:11
10

I had the same problem in docker and these steps worked for me:

apt update

then:

apt install libsm6 libxext6 libxrender-dev
hossein hayati
  • 1,088
  • 2
  • 15
  • 34
2

I was facing similar issue with openCV on the python:3.7-slim docker box. Following did the trick for me :

apt-get install build-essential libglib2.0-0 libsm6 libxext6 libxrender-dev

Please see if this helps !

Utopia
  • 591
  • 1
  • 8
  • 14
0

I was not able to install cv2 on Anaconda-Jupyter notebook running on Ubuntu on Google Cloud Platform. But I found a way to do it as follows:

Run the following command from the ssh terminal and follow the instruction:

 sudo apt-get install libsm6 libxrender1 libfontconfig1

Once its installed Open the Jupyter notebook and run following command:

!pip install opencv-contrib-python

Note: I tried to run this command: "sudo python3 -m pip install opencv-contrib-python"but it was showing an error. But above command worked for me.

Now refresh the notebook page and check whether it's installed or not by running import cv2 in the notebook.

Yogesh Awdhut Gadade
  • 2,498
  • 24
  • 19
0

I got the same error using heroku, follow these steps:

add a file named Aptfile in your repo and include a list of apt package names to be installed in that Aptfile

something like this in (my case)

libsm6
libxrender1
libfontconfig1
libice6

now try pushing the code to heroku. This will do the job.

Or you can still refer this blog

  • 2
    Can you try to include the contents of the Aptfile with the case of the question instead or your case? – 12944qwerty May 04 '21 at 00:57
  • I was getting the same error, the reason of this error is the following packages aren't included with opencv package. So these are to be installed explicitly. I guess the same contents will do the job to resolve the question also. – Puneeth Regonda May 05 '21 at 01:22
  • If the packages are to be installed explicitly every time after deployment in new server its best way we add Aptfile and place the contents in it. Rather using cli and apt - get install. – Puneeth Regonda May 05 '21 at 01:25