26

How to install OpenCV version 3.1 on Ubuntu 16.04 LTS to work with Python 3.5.1?

I tried this on a VM but obviously that is useful to do if you want to work with Python 2.

3

7 Answers 7

47

I found this.

OpenCV on wheels.

Unofficial OpenCV packages for Python.

Installation was painless for Ubuntu 16.04:

pip3 install opencv-python

Check the installation:

python3
Python 3.5.2 (default, Nov 17 2016, 17:05:23) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'3.2.0'
5
  • 1
    I like this solution but got this error message: Could not find any downloads that satisfy the requirement opencv-python
    – Jeff Tian
    Aug 25, 2017 at 3:38
  • This might be the simplest, working solution, unless you're looking for a higher version of opencv Nov 12, 2017 at 7:28
  • 1
    make sure to use pip3 and NOT pip !
    – givonz
    Jan 8, 2018 at 21:44
  • Was also missing the following in my lxc container: apt-get install -y libsm6 libxext6 libxrender-dev
    – DanJ
    Mar 16, 2018 at 17:03
  • easy but this package doesn't has video functionalities Mar 9, 2019 at 9:24
11

python3-opencv can be installed from the Universe repository in Ubuntu 17.10 and later. Open the terminal and type:

sudo apt install python3-opencv  

Upgrade your OS to 18.04 if you are using Ubuntu 14.04 or Ubuntu 16.04. If Ubuntu 16.04 is installed in WSL sudo do-release-upgrade will upgrade it to 18.04.

1
10

I have OpenCV version 3.1 on Ubuntu 16.04 LTS to work with Python 3.5.1. At the latest version it's fixed, try:

http://cyaninfinite.com/tutorials/installing-opencv-in-ubuntu-for-python-3/

But I didn't download dev python and OpenCV, I took directly from github:

1

EDIT:

Here are the instructions I tested successfully and mentioned in the link above:

  1. Update the repository: sudo apt-get update
  2. Download the required packages to compile OpenCV:

    sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
    
  3. Install the developer packages for Python3.5: sudo apt-get install python3.5-dev
  4. Copy the dev files:python3.5-config --includes The output should be similar to: -I/usr/include/python3.5m -I/usr/include/x86_64-linux-gnu/python3.5m. The first part of the output is the expected location & the second part shows the current location of the config file. To solve this problem, we’ll copy the file from the current location to the expected location: sudo cp /usr/include/x86_64-linux-gnu/python3.5m/pyconfig.h /usr/include/python3.5m/
  5. Create a temporary directory, move into it and run: git clone https://github.com/Itseez/opencv.git. This will download OpenCV 3. Then rename the folder opencv to opencv-3
  6. Create a build directory, move to it and run:

    cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ../opencv-3
    
  7. After the process is finished, run make and then sudo make install.
  8. Enjoy.
5
  • 1
    Unfortunately a lot of this still fails for me. I get errors n the way about apt_pkg and cannot import multiarray
    – CashCow
    Aug 3, 2016 at 16:37
  • 1
    In particular it keeps finding python3.4 rather than python3.5 and I have no idea how to direct it to use python3.5. When I tried import opencv in python3.4 I did get a different error about some of the missing dependencies
    – CashCow
    Aug 3, 2016 at 17:07
  • The last bit of the last command should be ../../opencv-3 I think... you are two dirs up from specifying the opencv-3 directory Dec 30, 2017 at 3:22
  • You also want to use all processors when doing make, so if you have 8 cores do make -j8 Dec 30, 2017 at 3:23
  • Execute cmake from another parallel directory else you will get this issue stackoverflow.com/questions/45518317/…
    – 100rabh
    Jul 25, 2018 at 8:54
2

The most simple and elegant way I found online is to install the library by running an installation script. Download the installation script install-opencv.sh, open your terminal and execute:

bash install-opencv.sh

Type your sudo password and you will have installed OpenCV. This operation may take a long time due to the packages to be installed and the compilation process.

The reference: https://milq.github.io/install-opencv-ubuntu-debian/

1

I am currently working with Google VM (ubuntu 14.04). Installing opencv on python3.4 version has been quite a task. I wanted opencv to be installed for python 3.4 but every time it was getting installed on 2.7 version.

I will share the steps I followed so as to help others on it.

Step 1 Follow all the steps as mentioned on openCv installation part till cmake. Link is given below:

https://docs.opencv.org/master/d7/d9f/tutorial_linux_install.html

Note: Install all the 3 packages mentioned at start. That optional one too..!! And don't forget to change the python version for which you are installing.

I did

sudo apt-get install python3-dev python3-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

Follow step 2 for cmake.

Step 2 For installing opencv in specific version of python (ubuntu), you have to set the default (PYTHON_DEFAULT_EXECUTABLE) with the path to where your python is installed. You can find that out by using command whereis python3.4 (or, your version). Mine was in /usr/bin/python3.4

Instead of cmake mentioned on the page, use this,

cmake -D CMAKE_BUILD_TYPE=Release -D BUILD_NEW_PYTHON_SUPPORT=ON -D BUILD_opencv_python3=ON -D HAVE_opencv_python3=ON -D PYTHON_DEFAULT_EXECUTABLE=/usr/bin/python3.4 ..

Note: Don't forget to change your python version and path in PYTHON_DEFAULT_EXECUTABLE.

Step 3 Follow the remaining steps as mentioned in the link till sudo make install

Hope it helps.

0

Base guide: https://docs.opencv.org/master/d7/d9f/tutorial_linux_install.html

The following, tested on Ubuntu 18.04, installs OpenCV 4.1.0-dev and does not use python virtual environment. I haven't figured out how they work yet. I put the answer here so it has wider visibility.

Requirements

[compiler] sudo apt-get install build-essential
[required] sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
[optional] sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

How to install jasper: https://github.com/opencv/opencv/issues/8622#issuecomment-353761271

sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt update
sudo apt install libjasper1 libjasper-dev

1. Clone repos

The installation will include contrib modules.

cd ~/<my_working_directory>
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git

(Git branch to 3.4 to install that version.)

1.1 Make build folder

If you mess up, just delete build folder and start over from here.

mkdir build
cd build

2. Configuring

I use the following cmake flags:

cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local -DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules ..

Optional flags: (there are a lot of flags, see this blog post for some useful ones)

-DINSTALL_C_EXAMPLES=ON 
-DINSTALL_PYTHON_EXAMPLES=ON 
-DWITH_TBB=ON 
-DWITH_V4L=ON 
-DWITH_QT=ON 
-DWITH_OPENGL=ON
-DWITH_NVCUVID=ON 
-DWITH_CUDA=ON
-DWITH_OPENMP=ON 
-DWITH_IPP=ON
-DWITH_OPENCL=ON

Ensure cmake finds your python version by checking its output for the following:

--   Python 2:
--     Interpreter:                 /usr/bin/python2.7 (ver 2.7.15)
--     Libraries:                   /usr/lib/x86_64-linux-gnu/libpython2.7.so (ver 2.7.15+)
--     numpy:                       /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.13.3)
--     install path:                lib/python2.7/dist-packages/cv2/python-2.7
-- 
--   Python 3:
--     Interpreter:                 /usr/bin/python3 (ver 3.6.8)
--     Libraries:                   /usr/lib/x86_64-linux-gnu/libpython3.6m.so (ver 3.6.8)
--     numpy:                       /home/jx/.local/lib/python3.6/site-packages/numpy/core/include (ver 1.15.4)
--     install path:                lib/python3.6/dist-packages/cv2/python-3.6

How to specify which python versions to use

3. Make and install

Use make with the desired number of threads (ex. number of processors) then install.

make -j8
sudo make install

Things should now install to /usr/local

4. Test

If all went well then you should be able to import cv2 in python.

import cv2
cv2.__version__
-1

OpenCV can be installed using pip/pip3 and homebrew. I've tried installing it before using pip3 (Python 3.6.0), but i had problems with VideoCapture, which requires the ffmpeg package. cv2.VideoCapture('videofilename'). This is why I used homebrew for installing OpenCV. Open a Terminal with Ctrl + Alt + T and type the following commands:

brew install ffmpeg
brew install opencv3 --with-ffmpeg -v (Python 2.7)
brew install opencv3 --with-python3 --with-ffmpeg -v (Python 3.6)
3
  • 3
    Why do you suggest using homebrew?
    – guntbert
    Apr 18, 2017 at 18:22
  • 1
    OpenCV can be installed using pip/pip3 and homebrew. I had installed using pip3 (Python 3.6.0), but i had problems with VideoCapture , which requires ffmpeg package. cv2.VideoCapture('videofilename') So i used homebrew for installing and it worked well.
    – Biranchi
    Apr 19, 2017 at 2:40
  • 2
    @Biranchi please update your answer with the information from your comment (and maybe add some more information). Jun 12, 2019 at 5:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .