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

Python Opencv ImportError: libSM.so.6: cannot open shared object file: No such file or directory is an error which occurs when you have a faulty Opencv installation and/or missing dependencies.

I will explain why this error is taking place and how to fix it, while also trying to add other solutions that could help us solve the problem.

Exploring the Python Opencv ImportError: libSM.so.6: cannot open shared object file: No such file or directory

This is an error which occurs when you have a faulty Opencv installation and/or missing dependencies.

Please double check so you can avoid mixing between errors. The error message should look like the error message bellow.

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

Solution 1 : Use the headless version of opencv-python

The easiest solution is to use the headless version of opencv-python. This version does not need graphical dependencies such as libSM which causes the error we are facing here.

To install the Headless version, instead of installing opencv-python like this.

                                                                       #
pip install opencv-python
                                                                       #

You should use this command to install it

                                                                       #
pip install opencv-python-headless
                                                                       #

That is it, we are done. The Method above should solve the problem and the error should be gone.

Solution 2 : Correctly install Opencv and its dependencies

When you have a faulty Opencv installation or missing dependencies, you will get a similar error.

You are here because you are missing the libSM.so.6 Opencv dependency , for many people it is not only one dependency but many.

The solution is to run a couple of commands and install Opencv and its dependencies.

To install Opencv you can use the command bellow

                                                                       #
pip install opencv-python
                                                                       #

To install the Opencv dependencies use the command bellow

                                                                       #
sudo apt-get install libsm6 libxrender1 libfontconfig1
                                                                       #

Followed by the command bellow in order to install the other dependencies

                                                                       #
sudo apt-get install libxext6 libxrender-dev 
                                                                       #

This should be enough to get rid of the error for good. Thank you for reaching the end of this blog post.

Summing-up : 

This is the end of our article, I hope the solutions I presented worked for you, Learning Python is a fun journey, do not let the errors discourage you. Keep coding and cheers.

Thank you for reading, keep coding and cheers. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/