Install Scipy Ubuntu

Over the past two years running the PyImageSearch blog, I’ve authored two tutorials detailing the required steps to install OpenCV (with Python bindings) on Ubuntu. You can find the two tutorials here:

I am trying to install scipy on a Ubuntu 15.04 virtual box using the command pip install scipy During the installation, however, I get the following error: error: library dfftpack has Fortran so. NumPy, SciPy, Pandas, and Matplotlib are fundamental scientific computing and visualization packages with Python. Scikit-learn is a simple and efficient package for data mining and analysis in Python.

However, with support of Ubuntu 14.04 winding down and Ubuntu 16.04 set as the next LTS (with support until April 2021), I thought it would be appropriate to create a new, updated Ubuntu + OpenCV install tutorial.

Inside this tutorial, I will document, demonstrate, and provide detailed steps to install OpenCV 3 on Ubuntu 16.04 with either Python 2.7 or Python 3.5 bindings.

Furthermore, this document has been fully updated from my previous Ubuntu 14.04 tutorials to use the latest, updated packages from the apt-get repository.

To learn how to install OpenCV on your Ubuntu 16.04 system, keep reading.

Note: Don’t care about Python bindings and simply want OpenCV installed on your system (likely for C++ coding)? No worries, this tutorial will still work for you. Follow along with the instructions and perform the steps — by the end of this article you’ll have OpenCV installed on your system. From there, just ignore the Python bindings and proceed as usual.

Ubuntu 16.04: How to install OpenCV

Before we get into this tutorial, I want to mention that Ubuntu 16.04 actually ships out-of-the-box with both Python 2.7 and Python 3.5 installed. The actual versions (as of 24 October 2016) are:

  • Python 2.7.12 (used by default when you type python in your terminal).
  • Python 3.5.2 (can be accessed via the python3 command).

Again, it’s worth repeating that Python 2.7 is still the default Python version used by Ubuntu. There are plans to migrate to Python 3 and use Python 3 by default; however, as far as I can tell, we are still a long way from that actually becoming a reality.

In either case, this tutorial will support both Python 2.7 and Python 3. I’ve highlighted the steps that require you to make a decision regarding which version of Python you would like to use. Make sure you are consistent with your decision, otherwise you will inevitably run into compile, linking, and import errors.

Regarding which Python version you should use…I’m not getting into that argument. I’ll simply say that you should use whichever version of Python you are comfortable with and use on a daily basis. Keep in mind that Python 3 is the future — but also keep in mind that porting Python 2.7 code to Python 3 isn’t terribly challenging either once you understand the differences between the Python versions. And as far as OpenCV goes, OpenCV 3 doesn’t care which version of Python you’re using: the bindings will work just the same.

All that said, let’s get started installing OpenCV with Python bindings on Ubuntu 16.04.

Step #1: Install OpenCV dependencies on Ubuntu 16.04

Scipy

Most (in fact, all) steps in this tutorial will be accomplished by using your terminal. To start, open up your command line and update the apt-get package manager to refresh and upgrade and pre-installed packages/libraries:

Ubuntu 16.04: How to install OpenCV
2
$sudo apt-getupgrade

Next, let’s install some developer tools:

Ubuntu 16.04: How to install OpenCV
2
$sudo rm-rf~/get-pip.py~/.cache/pip

Once we have virtualenv and virtualenvwrapper installed, we need to update our ~/.bashrc file to include the following lines at the bottom of the file:

Ubuntu 16.04: How to install OpenCV
2
4
6
8
10
$mkdirbuild
$cmake-DCMAKE_BUILD_TYPE=RELEASE
-DINSTALL_PYTHON_EXAMPLES=ON
-DOPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.1.0/modules
-DBUILD_EXAMPLES=ON..

The above commands change directory to ~/opencv-3.1.0 , which if you have been following this tutorial is where you downloaded and unarchived the .zip files.

Note: If you are getting an error related to stdlib.h:No such file ordirectory during either the cmake or make phase of this tutorial you’ll also need to include the following option to CMake: -DENABLE_PRECOMPILED_HEADERS=OFF . In this case I would suggest deleting your build directory, re-creating it, and then re-running CMake with the above option included. This will resolve the stdlib.h error. Thank you to Carter Cherry and Marcin for pointing this out in the comments section!

Inside this directory we create a sub-directory named build and change into it. The build directory is where the actual compile is going to take place.

Finally, we execute cmake to configure our build.

Before we move on to the actual compilation of OpenCV, make sure you examine the output of CMake!

To do this, scroll down the section titled Python2 and Python3 .

If you are compiling OpenCV on Ubuntu 16.04 with Python 2.7 support, make sure the Python2 section includes valid paths to the Interpreter , Libraries , numpy , and packages path . Your output should be similar to mine below:

Figure 3: Ensuring that Python 2.7 will be used when compiling OpenCV 3 for Ubuntu 16.04.

Examining this output, you can see that:

  1. The Interpreter points to the Python 2.7 binary in the cv virtual environment.
  2. Libraries points to the Python 2.7 library (which we installed during the final step of Step #1).
  3. The numpy value points to our NumPy installation in the cv virtual environment.
  4. And finally, the packages path points to lib/python2.7/site-packages . When combined with the CMAKE_INSTALL_PREFIX , this means that after compiling OpenCV, we’ll find our cv2.so bindings in /usr/local/lib/python2.7/site-packages/ .

Similarly, if you’re compiling OpenCV 16.04 with Python 3 support, you’ll want to ensure your Python3 section looks similar to mine below:

Figure 4: Validating that Python 3 will be used when compiling OpenCV 3 for Ubuntu 16.04.

Install Scipy Ubuntu Python3

Again, notice how my Interpreter , Libraries , numpy and packages path have all been correctly set.

If you do not see the cv virtual environments in these variable paths, it’s almost certainly because you are NOT in the cv virtual environment prior to running CMake!

If that is indeed the case, simply access the cv virtual environment by calling workon cv and re-run the CMake command mentioned above.

Assuming your CMake command exited without any errors, you can now compile OpenCV:

Ubuntu 16.04: How to install OpenCV

The -j switch controls the number of processes to be used when compiling OpenCV — you’ll want to set this value to the number of processors/cores on your machine. In my case, I have a quad-core processor, so I set -j4 .

Using multiple processes allows OpenCV to compile faster; however, there are times where race conditions are hit and the compile bombs out. While you can’t really tell if this is the case without a lot of previous experience compiling OpenCV, if you do run into an error, my first suggestion would be to run make clean to flush the build, followed by compiling using only a single core:

Ubuntu 16.04: How to install OpenCV
2
$make

Below you can find a screenshot of a successful OpenCV + Python compile on Ubuntu 16.04:

Figure 5: Successfully compiling OpenCV 3 for Ubuntu 16.04.

The last step is to actually install OpenCV 3 on Ubuntu 16.04:

Ubuntu 16.04: How to install OpenCV
2
$sudo ldconfig

Step #5: Finish your OpenCV install

You’re coming down the home stretch, just a few more steps to go and your Ubuntu 16.04 system will be all setup with OpenCV 3.

For Python 2.7:

After running sudo make install , your Python 2.7 bindings for OpenCV 3 should now be located in /usr/local/lib/python-2.7/site-packages/ . You can verify this using the ls command:

Ubuntu 16.04: How to install OpenCV
2
$rm-rf opencv-3.1.0opencv_contrib-3.1.0opencv.zipopencv_contrib.zip

But again, be careful when running this command! You’ll want to make sure you have properly installed OpenCV on your system prior to blowing along these directories. Otherwise, you’ll need to restart the entire compile process!

Troubleshooting and FAQ

In this section, I address some of the common questions, problems, and issues that arise when installing OpenCV on Ubuntu 16.04.

Q. When I execute mkvirtualenv or workon , I get a “command not found error”.

A. There are three primary reasons why you would be getting this error message, all of which are related to Step #3:

  1. First, make sure you have installed virtualenv and virtualenvwrapper using the pip package manager. You can verify this by running pip freeze , examining the output, and ensuring that you see both virtualenv and virtualenvwrapper in the list of installed packages.
  2. Your ~/.bashrc file may not be updated correctly. To diagnose this, use a text editor such as nano and view the contents of your ~/.bashrc file. At the bottom of the file, you should see the proper export and source commands are present (again, check Step #3 for the commands that should be appended to ~/.bashrc ).
  3. After editing your ~/.bashrc file, you may have forgotten to source it and reload its contents. Make sure you run source ~/.bashrc after editing it to ensure the contents are reloaded — this will give you access to the mkvirtualenv and workon commands.

Q. Whenever I open a new terminal, logout, or reboot my Ubuntu system, I cannot execute the mkvirtualenv or workon commands.

A. See reason #2 from the previous question.

Q.When I (1) open up a Python shell that imports OpenCV or (2) execute a Python script that calls OpenCV, I get an error: Import Error:No module named cv2 .

A. Unfortunately, the exact cause of this error message is extremely hard to diagnose as there are multiple reasons this could be happening. In general, I recommend the following suggestions to help diagnose and resolve the error:

  1. Make sure you are in the cv virtual environment by using the workon cv command. If this command gives you an error, then see the first question in this FAQ.
  2. If after you’ve ensured your ~/.bashrc file has been updated properly and source ‘d, then try investigating the contents of the site-packages directory in your cv virtual environment. You can find the site-packages directory in ~/.virtualenvs/cv/lib/python2.7/site-packages/ or ~/.virtualenvs/cv/lib/python3.5/site-packages/ depending on your Python version. Make sure that (1) there is a cv2.so file in this site-packages directory and (2) that it’s properly sym-linked to a valid, existing file.
  3. Be sure to check the site-packages (and even dist-packages ) directory for the system install of Python located in /usr/local/lib/python2.7/site-packages/ and /usr/local/lib/python3.5/site-packages/ , respectively. Ideally, you should have a cv2.so file there.
  4. If all else fails, check in your build/lib directory of your OpenCV build. There should be a cv2.so file there (provided that both cmake and make executed without error). If the cv2.so file is present, manually copy it into both the system site-packages directory as well as the site-packages directory for the cv virtual environment.

So, what’s next?

Congrats! You now have a brand new, fresh install of OpenCV on your Ubuntu 16.04 system — and I’m sure you’re just itching to leverage your install to build some awesome computer vision apps…

…but I’m also willing to bet that you’re just getting started learning computer vision and OpenCV, and probably feeling a bit confused and overwhelmed on exactly where to start.

Personally, I’m a big fan of learning by example, so a good first step would be to have some fun and read this blog post on detecting cats in images/videos. This tutorial is meant to be very hands-on and demonstrate how you can (quickly) build a Python + OpenCV application to detect the presence of cats in images.

And if you’re really interested in leveling-up your computer vision skills, you should definitely check out my book, Practical Python and OpenCV + Case Studies. My book not only covers the basics of computer vision and image processing, but also teaches you how to solve real-world computer vision problems including face detection in images and video streams, object tracking in video, and handwriting recognition.

So, let’s put that fresh install of OpenCV 3 on your Ubuntu 16.04 system to good use — just click here to learn more about the real-world projects you can solve using Practical Python and OpenCV.

Summary

In today’s blog post, I demonstrated how to install OpenCV 3 with either Python 2.7 or Python 3 bindings on your Ubuntu 16.04 system.

For more OpenCV install tutorials on other operating systems (such as OSX, Raspbian, etc.), please refer to this page where I provide additional links and resources.

But before you go…

If you’re interested in learning more about OpenCV, computer vision, and image processing, be sure to enter your email address in the form below to be notified when new blog posts are published!

Active3 years, 11 months ago

I am trying to install scipy on a Ubuntu 15.04 virtual box using the command

During the installation, however, I get the following error:

How can this be fixed?

Python Install Scipy Ubuntu

AlexAlex
1,92416 gold badges46 silver badges75 bronze badges

1 Answer

Install a fortran compiler?

should do the trick.

Install Numpy Python Ubuntu

Occasionally, scipy is packaged in ubuntu, precompiled. You can just do

instead.

Install Scipy Ubuntu Pip

Wouter VerhelstWouter Verhelst

Not the answer you're looking for? Browse other questions tagged ubuntu or ask your own question.