Configuring Ubuntu for deep learning with Python

When it comes to learning new technology such as deep learning, configuring your development environment tends to be half the battle. Different operating systems, hardware, dependencies, and the actual libraries themselves can lead to many headaches before you’re even able to get started studying deep learning.

These issues are further compounded by the speed of deep learning library updates and releases — new features push innovation, but oftentimes break previous versions. Your environment can quickly become obsolete, so it is imperative to become an expert in installing and configuring your deep learning environment.

Now that Deep Learning for Computer Vision with Python has officially released, I’ll be publishing three posts this week where I will demonstrate how to stand up your own deep learning environments so that you can get a head start before you dive into reading.

I’ll be demonstrating how to configure your own native development environment for the following operating systems and peripherals:

  • Configuring Ubuntu for deep learning with Python (i.e., the post you are currently reading)
  • Setting up Ubuntu (with GPU support) for deep learning with Python
  • Configuring macOS for deep learning with Python

As you start to walk the path to deep learning and computer vision mastery, I’ll be right there with you.

To get started configuring your Ubuntu machine for deep learning with Python, just keep reading.

Configuring Ubuntu for deep learning with Python

Accompanying my new deep learning book is a downloadable pre-configured Ubuntu VirtualBox virtual machine with Keras, TensorFlow, OpenCV, and other computer vision/machine learning libraries pre-installed. By far, this is the fastest way to get up and running with Deep Learning for Computer Vision with Python.

That being said, it is often desirable to install your environment on the bare metal so that you can take advantage of your physical hardware. For the GPU install tutorial part of this series it is a requirement that you be on the metal — a VM just won’t cut it since it doesn’t have access to your physical GPU.

Today, our blog post is broken down into four relatively easy steps:

  1. Step #1: Install Ubuntu system dependencies
  2. Step #2: Create your Python 3 virtual environment
  3. Step #3: Compile and Install OpenCV
  4. Step #4: Install Keras

Taking note of the steps, you will see that Deep Learning for Computer Vision with Python supports Python 3.

Python 3 will be the standard on PyImageSearch going forward as it is stable and quite frankly the future. Many organizations have been hesitant to adopt Python 3 at first (me included, as there was no Python 3 support for OpenCV until OpenCV 3), but at this point if you don’t adopt Python 3 you will be left in the dust. Expect PyImageSearch Gurus course content to be compatible with Python 3 in the near future as well.

Notice that we have chosen Keras as our deep learning library. Keras “stands out from the rest” of the available libraries for it’s ease of use and compatibility with both Tensorflow and Theano.

My deep learning book focuses on fundamentals and breaking into the field with ease rather than introducing you to a bunch of libraries — so for the Starter Bundle and Practitioner Bundle, I demonstrate various tasks and exercises with Keras (as well as implementing some basic neural network concepts by hand). The ImageNet Bundle takes advantage of mxnet as well.

While we will be primarily using Keras in my book, there are many deep learning libraries for Python, and I encourage you to become familiar with my top 9 favorite Python deep learning libraries.

To get started, you’ll want to have some time on your hands and access to an Ubuntu machine’s terminal — SSH is perfectly suitable if your box is in the cloud or elsewhere. Let’s begin!

Step #1: Install Ubuntu system dependencies

The purpose of this step is to prepare your system with the dependencies necessary for OpenCV.

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:

We’ll also need to install some developer tools as well as prerequisites required for image and video I/O, optimizations, and creating plots/visualizations:

We’ll wrap up Step #1 by installing the Python development headers and libraries for both Python 2.7 and Python 3.5 (that way you have both).

Note: If you do not install the Python development headers and static library, you’ll run into issues during Step #3 where we run  cmake  to configure our build. If these headers are not installed, then the  cmake  command will be unable to automatically determine the proper values of the Python interpreter and Python libraries. In short, the output of this section will look “empty” and you will not be able to build the Python bindings. When you get to Step #3, take the time to compare your output of the command to mine.

Let’s continue on by creating a virtual environment to house OpenCV and Keras.

Step #2: Create your Python virtual environment

In this section we will setup a Python virtual environment on your system.

Installing pip

We are now ready to start configuring our Python development environment for the build. The first step is to install pip , a Python package manager:

Installing virtualenv and virtualenvwrapper

I’ve mentioned this in every single install tutorial I’ve ever done, but I’ll say it again here today: I’m a huge fan of both virtualenv and virtualenvwrapperThese Python packages allow you to create separate, independent Python environments for eachproject that you are working on.

In short, using these packages allows you to solve the “Project X depends on version 1.x, but Project Y needs 4.x dilemma. A fantastic side effect of using Python virtual environments is that you can keep your system Python neat, tidy, and free from clutter.

While you can certainly install OpenCV with Python bindings without Python virtual environments, I highly recommend you use them as other PyImageSearch tutorials leverage Python virtual environments. I’ll also be assuming that you have both  virtualenv  and  virtualenvwrapper  installed throughout the remainder of this guide.

If you would like a full, detailed explanation on why Python virtual environments are a best practice, you should absolutely give this excellent blog post on RealPython a read. I also provide some commentary on why I personally prefer Python virtual environments in the first half of this tutorial.

Again, let me reiterate that it’s standard practice in the Python community to be leveraging virtual environments of some sort, so I suggest you do the same:

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:

The ~/.bashrc  file is simply a shell script that Bash runs whenever you launch a new terminal. You normally use this file to set various configurations. In this case, we are setting an environment variable called  WORKON_HOME   to point to the directory where our Python virtual environments live. We then load any necessary configurations from  virtualenvwrapper  .

To update your  ~/.bashrc  file simply use a standard text editor. I would recommend using  nano  ,  vim  , or emacs . You can also use graphical editors as well, but if you’re just getting started,  nano  is likely the easiest to operate.

A more simple solution is to use the  cat  command and avoid editors entirely:

After editing our ~/.bashrc  file, we need to reload the changes:

Note: Calling source on ~/.bashrc only has to be done once for our current shell session. Anytime we open up a new terminal, the contents of ~/.bashrc  will be automatically executed (including our updates).

Creating a virtual environment for deep learning and computer vision

Now that we have installed  virtualenv  and virtualenvwrapper, the next step is to actually create the Python virtual environment — we do this using the  mkvirtualenv  command.

In past install tutorials, I’ve presented the choice of Python 2.7 or Python 3. At this point in the Python 3 development cycle, I consider it stable and the right choice. You may elect to use Python 2.7 if you have specific compatibility requirements, but for the purposes of my new deep learning book we will use Python 3.

With that said, for the following command, ensure your Python ( -p ) flag is set to python3 :

Regardless of which Python version you decide to use, the end result is that we have created a Python virtual environment named  dl4cv  (short for “deep learning for computer vision”).

You can name this virtual environment whatever you like (and create as many Python virtual environments as you want), but for the time being, I would suggest sticking with the dl4cv  name as that is what I’ll be using throughout the rest of this tutorial as well as the remaining install guides in this series.

Verifying that you are in the “dl4cv” virtual environment

If you ever reboot your Ubuntu system; log out and log back in; or open up a new terminal, you’ll need to use the workon  command to re-access your dl4cv  virtual environment. An example of the workon  command follows:

To validate that you are in the dl4cv  virtual environment, simply examine your command line — if you see the text (dl4cv)  preceding your prompt, then you are in the dl4cv  virtual environment:

Figure 1: Inside the dl4cv virtual environment denoted by ‘(dl4cv)’ in the prompt.

Otherwise if you do not see the dl4cv  text, then you are not in the dl4cv  virtual environment:

Figure 2: Outside of the dl4cv virtual environment. Simply execute the ‘workon dl4cv’ command to get into the environment.

Installing NumPy

The final step before we compile OpenCV is to install NumPy, a Python package used for numerical processing. To install NumPy, ensure you are in the dl4cv  virtual environment (otherwise NumPy will be installed into the system version of Python rather than the  dl4cv  environment).

From there execute the following command:

Step #3: Compile and Install OpenCV

In this section we will install and compile OpenCV. We’ll start by downloading and unarchiving OpenCV 3.3. Then we will build and compile OpenCV from source. Finally we will test that OpenCV has been installed.

Downloading OpenCV

First let’s download opencv and opencv_contrib into your home directory:

You may need to expand the commands above to copy and past the full path to the opencv_contrib  file.

Then, let’s unzip both files:

Running CMake

Let’s create a build  directory and run CMake:

For CMake, it is important that your flags match mine for compatibility. Also, make sure that your opencv_contrib  version is the exact same as the OpenCV version you downloaded (in this case version 3.3.0 ).

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

Start by scrolling to the section titled Python 3 .

Make sure that your Python 3 section looks like the figure below:

Figure 3: Checking that Python 3 will be used when compiling OpenCV 3 for Ubuntu.

Pay attention that the Interpreter points to our python3.5  binary located in the dl4cv  virtual environment while numpy  points to our NumPy install.

In either case if you do not see the dl4cv  virtual environment in these variables’ paths, then it’s almost certainly because you are NOT in the dl4cv  virtual environment prior to running CMake!

If this is the case, access the dl4cv  virtual environment using workon dl4cv  and re-run the command outlined above (I would also suggest deleting the build  directory and re-creating it).

Compiling OpenCV

Now we are now ready to compile OpenCV with 4 cores:

Note: You can try a version of the -j4  flag corresponding to the number of cores of your CPU to achieve compile time speedups. In this case I used -j4  since my machine has four cores. If you run into compilation errors, you may run the command make clean  and then just compile without the parallel flag: make .

From there, all you need to do is to install OpenCV 3.3 and then free up some disk space if you so desire:

When your compilation is complete you should see output that looks similar to the following:

Figure 4: OpenCV compilation is complete.

Symbolic linking OpenCV to your virtual environment

To sym-link our OpenCV bindings into the dl4cv  virtual environment, issue the following commands:

Notice that I am using Python 3.5 in this example. If you are using Python 3.6 (or newer) you’ll want to update the paths to use your version of Python.

Secondly, your .so  file (i.e., the actual OpenCV bindings) may be some variant of what is shown above, so be sure to use the appropriate file by double-checking the path.

Testing your OpenCV 3.3 install

Now that we’ve got OpenCV 3.3 installed and linked, let’s do a quick sanity test to see if things work:

Make sure you are in the dl4cv  virtual environment before firing up Python ( workon dl4cv ). When you print out the version, it should match the version of OpenCV that you installed (in our case, OpenCV 3.3.0 ).

That’s it — assuming you didn’t encounter an import error, you’re ready to go on to Step #4 where we will install Keras.

Step #4: Install Keras

For this step, be sure that you are in the dl4cv  environment by issuing the workon dl4cv  command. Then install our various Python computer vision, image processing, and machine learning libraries:

Next, install Tensorflow:

Notice how we are using the CPU version of TensorFlow. I will be covering the GPU version in a separate tutorial.

Installing Keras is extremely simple, thanks to pip :

Again, do this in the dl4cv  virtual environment.

You can test our Keras install from a Python shell:

You should see that Keras has been imported with no errors and the TensorFlow backend is being used.

Before you wrap up the install tutorial take a second to familiarize yourself with the ~/.keras/keras.json  file:

Ensure that image_data_format  is set to channels_last  and backend  is tensorflow .

Congratulations! You are now ready to begin your Deep learning for Computer Vision with Python journey.


In today’s blog post, I demonstrated how to set up your deep learning environment on an Ubuntu machine using only the CPU. Configuring your development environment is half the battle when it comes to learning new techniques, algorithms, and libraries.

If you’re interested in studying deep learning in more detail, be sure to take a look at my new book, Deep Learning for Computer Vision with Python.

The next few blog posts in this series will cover alternative environments including macOS and Ubuntu (with GPU support).

Of course, if you’re interested in pre-configured deep learning development environments, take a look my Ubuntu virtual machine and Amazon EC2 instance.

If you are interested in learning more about computer vision and deep learning, be sure to enter your email address in the form below to be notified when new blog posts + tutorials are published!

, , , , , ,

66 Responses to Configuring Ubuntu for deep learning with Python

  1. Brian Robbins September 25, 2017 at 11:32 am #

    libjasper-dev libpng12-dev unavailable on Ubuntu 17.04
    So are we forced to use Ubuntu 16?

    • Adrian Rosebrock September 26, 2017 at 8:23 am #

      I created this tutorial with the intention of Ubuntu 16 being used. I would suggest using Ubuntu 16.

      • Luke July 14, 2018 at 1:55 pm #

        I wouldn’t recommend using older operating system just for one package! In fact, you can see versions newer than 16.x already have OpenCV >3.0 so you probably don’t even need to manually compile it on newer OS and newer packages.

        By the way you know you can combine sudo apt-get packagea packageb,
        sudo apt-get packagec packaged, into one command

        sudo apt-get packagea packageb packagec packaged

        That should same some time and clean up above where you have 8 different commands for users to paste into the terminal.

        Thanks for the writeup 🙂

        • Adrian Rosebrock July 17, 2018 at 7:34 am #

          I do not recommend installing OpenCV via apt-get. You will install an older version and likely without the “contrib” module which is important for other applications of computer vision.

          • Igor Nikitin August 1, 2018 at 6:36 am #

            You can use pip install opencv-python or pip install opencv-contrib-python, but it will be without CUDA, i think it’s easier than comile OpenCV. See on

    • Rafael Campos January 29, 2019 at 9:44 am #

      I solved this issue adding this line to /etc/apt/sources.list:

      deb xenial-security main
      deb-src xenial-security main

      After, i update the system and installed the packages:

      sudo apt-get update
      sudo apt-get install libjasper1 libjasper-dev

  2. Michael Schwab September 25, 2017 at 12:38 pm #

    It’s also possible to use a pre-created dockerfile to get setup with a full Cuda/CuDnn/Keras/Theano/TensorFlow/jupyter/OpenCv environment in ubuntu that has access to the cpu and gpu. This is using something called nvidia-docker and is probably the easiest way to have a full running gpu stack. I’ve used it successfully all weekend to work through the first 12 chapters of the deeplearning book. Here’s more info.. . For me, having a full docker stack that doesn’t require any changes to the host system makes things extremely portable and easy to configure.

    • Adrian Rosebrock September 26, 2017 at 8:21 am #

      I’ve used Docker + GPU before, the biggest problem is that it’s a huge support burden for me personally. Some PyImageSearch readers are just getting started and Docker is overkill so a simple VM with a nice GUI is more appropriate. Some readers are more advanced and want to work with the “bare metal”. Docker is excellent and makes life easier and I do hope I can cover it in the future. But for the time being I simply need to create a baseline of install instructions that “work”.

  3. Jose Antonio Sanchez Valero September 25, 2017 at 1:38 pm #

    How much free space we need for this kind of task? Btw I’m thinking about a new partition in my disc for deep learning with ubuntu so it would be nice if you could estimate how much we need


    • Adrian Rosebrock September 26, 2017 at 8:18 am #

      The more the better. I would recommend at least 16GB to start (including OS files), especially if you intend on following next week’s blog post on GPU + CUDA. Keep in mind that you’ll not only need space for the drivers but for any code + datasets you want to work with as well.

  4. ofey September 25, 2017 at 1:48 pm #

    Hey Adrian! Thank you for the tutorial 🙂

    Can you also post a tutorial for installing CUDA support for OpenCV with Python on Ubuntu. Like I’m able to set it up but have to go through a bunch of tutorials. So if you can enumerate the steps (in your style) it would be awesome!

    • Adrian Rosebrock September 26, 2017 at 8:17 am #

      Yes, this coming Wednesday (September 27th, 2017) I’ll be doing a GPU + CUDA install tutorial for Ubuntu.

  5. Jason September 25, 2017 at 7:38 pm #

    Great little tutorial…. I’m just wondering you prefer to compile opencv from source instead of plain install? Do you get more stuff this way ??

    Also what about anaconda for a virtual environment. I’ve been using it for quite some time without issues? Again… with anaconda I can just install opencv?!?

    • Adrian Rosebrock September 26, 2017 at 8:11 am #

      I’m not sure what you mean by “plain install”. Do you mean using apt-get? The reason is because the apt-get will install an older, out of date version of OpenCV. Compiling from source will allow you to pull in the latest OpenCV version, including the “contrib” modules. Anaconda does a better job of keeping their scientific libraries up to date, but again, compiling from source gives you the most control.

  6. Michael September 25, 2017 at 9:32 pm #

    Ugh, I got all the way to the end of Step 3, and couldn’t import cv2: Import error: No module named ‘cv2’

    Any suggestions?

    • Adrian Rosebrock September 26, 2017 at 8:10 am #

      There are a number of things that could be causing the problem. First, make sure that CMake correctly configured the Python 3 build and “python3” is in the list of OpenCV modules to be compiled. Secondly, make sure you sym-linked the resulting .so file (which are your actual Python bindings) into the Python virtual environment. Finally, double-check that you are in the dl4cv Python virtual environment before running CMake.

    • Daniel Watanabe September 26, 2017 at 1:53 pm #

      To fix the import cv2 problem i refered to this link:

      • Adrian Rosebrock September 27, 2017 at 6:50 am #

        Are you using Python virtual environments? If so, I do not recommend this solution. Let your Python virtual environment set the PYTHONPATH accordingly.

        • Daniel Watanabe September 27, 2017 at 8:09 am #

          Yes, I am using Python virtual environments. However, the Python wasn’t setting the PythonPath.

          • Adrian Rosebrock September 27, 2017 at 8:40 am #

            I’d be curious to learn more about your setup. Explicitly setting the PYTHONPATH shouldn’t be necessary. Definitely ensure that the .so file (i.e., your actual OpenCV bindings) are correctly sym-linked into your Python virtual environment.

    • Regina March 6, 2019 at 1:31 pm #

      I also had this problem, it just needed to run “source ~/.bashrc” and then it worked fine.

  7. jl September 26, 2017 at 6:59 am #

    i sent a message yesterday that I was having issues configuring VM , Cancel it , its all working now , I made a new virtual machine and now its all working


    • Adrian Rosebrock September 26, 2017 at 8:05 am #

      Congrats on resolving the issue!

  8. mario September 26, 2017 at 1:47 pm #


    This comes at a fortunate time as I was just looking for something simliar, but I’m still looking into different frameworks: what do you think about pytorch? I have no experience but the autograd feature looks like a sweet component to have especially when playing around with the architecture, do you think keras (with any backend) is flexible enough for research?

    Thank you!

    • Adrian Rosebrock September 27, 2017 at 6:48 am #

      I plan on covering PyTorch vs. Keras in a future blog post, but my opinion is that Keras is more mature, more used, and has a larger community. PyTorch has a lot of nice features, but Keras is fantastic for both research and commercial applications.

  9. Daniel Watanabe September 26, 2017 at 1:57 pm #

    For this error: /opencv_contrib-3.1.0/modules/hdf/include/opencv2/hdf/hdf5.hpp:40:18: fatal error: hdf5.h: No such file or directory
    compilation terminated. I refered to the links below for help:

    • Adrian Rosebrock September 27, 2017 at 6:51 am #

      Another solution is to update the CMake command to configure the build without HDF5:

      -D WITH_HDF5=OFF

  10. Abhijit September 26, 2017 at 3:31 pm #

    Thanks much for the great tutorial Adrian !!
    The install process was a smooth sail. It worked exactly as described.
    (I am using LinuxMint 18.1)
    I have purchased the copy of DL4CV and anxious now to get my hands on the code !!


    • Adrian Rosebrock September 27, 2017 at 6:47 am #

      Nice job on getting your system configured, Abhijit! And thank you for picking up a copy DL4CV 🙂 Were you able to download the code associated wit the book okay?

  11. Eason Wang September 29, 2017 at 2:43 am #

    Hi Adrian, I run into a problem for my ubuntu 16.04. In the cmake step, I got the results you mentioned. However, for the ‘make step’, it was terminated becaused of “/opencv-3.3.0/modules/stitching/include/opencv2/stitching/detail/matchers.hpp:52:42: fatal error: opencv2/xfeatures2d/cuda.hpp: No such file or directory
    compilation terminated.” Don’t know why. I checked the output for cmake, and found this ”
    …output removed for formatting…
    Do you have any suggestion on this issue? Thanks!

    • Adrian Rosebrock October 2, 2017 at 10:21 am #

      It sounds like CUDA is interfering with your compile. Please ensure that the -D WITH_CUDA=OFF flag is supplied to CMake.

  12. Carlos October 18, 2017 at 5:07 am #

    Hi Adrian , Do you think it’s possible to follow all the step to install keras in raspbian in my raspberry pi 3 ?

    • Carlos October 18, 2017 at 5:08 am #

      I train a model in keras on my GPU and i ask my self if it’s possible to run the model i train in my raspberry pi

      • Adrian Rosebrock October 19, 2017 at 4:54 pm #

        Yes. Train the model on your GPU system. Serialize it to disk. Transfer it to your Raspberry Pi and then load it there. I detail a similar process inside my book, Deep Learning for Computer Vision with Python.

    • Adrian Rosebrock October 19, 2017 at 4:53 pm #

      Yes. Since Raspbian is Ubuntu-based you can use these instructions to install Keras on your Raspberry Pi.

  13. mr.lee October 27, 2017 at 8:33 am #

    when I “import cv2”,there was an error:
    ImportError: /opt/ros/kinetic/lib/python2.7/dist-packages/ undefined symbol: PyCObject_Type
    what is that? what should I do?

    • Adrian Rosebrock October 27, 2017 at 11:49 am #

      Hi Mr. Lee — It seems you have a linking problem. Assuming you’re using virtual environments you should try this:

      cd ~/.virtualenvs/dl4cv/lib/python3.5/site-packages/
      ln -sf /usr/local/lib/python3.5/site-packages/

      Try that and let me know if it resolves your issue.

      I also offer a pre-configured Virtual Machine which is ready to go with the Deep Learning for Computer Vision with Python book. I know this isn’t the ideal solution, but it would let you get to learning rather than fighting configuration issues.

      • mr.lee October 27, 2017 at 10:19 pm #

        I have tried that,but it was the same problem.I not sure what’s wrong.Do you have any suggestion?

      • mr.lee October 30, 2017 at 12:39 am #

        It comes to the same problem.I was confused by it several days.Do you have more suggestion?

        • Adrian Rosebrock October 30, 2017 at 1:42 pm #

          Since you’re using ROS, see this SOF thread.

          • Gushu October 9, 2018 at 8:49 am #

            I found the solution in the Stack Overflow link you shared. thanks!

  14. October 28, 2017 at 11:21 am #

    I have a problem,after I installed the opencv3.3.0,when I tried:
    pkg-config –modversion opencv
    It showed:
    But when I do this :
    $ python
    >>> import cv2
    >>> cv2.__version__
    then it turned to:
    What is that mean?Was I success to install the opencv3.3.0?

    • Adrian Rosebrock October 30, 2017 at 2:40 pm #

      Hi Mr. Wang — Thanks for your comment. Did you use virtual environments as I instructed? You might not be in your new virtual environment. Did you compile from source or use a package manager to install OpenCV?

  15. Reed October 28, 2017 at 12:36 pm #

    Hi Adrian
    I have installed the optimized opencv following by the post on 9th Oct, then followed this post to install keras. when I installed tensorflow, it showed:
    (cv) pi@raspberrypi:~ $ pip install tensorflow
    Collecting tensorflow
    Could not find a version that satisfies the requirement tensorflow (from versions:)
    No matching distribution found for tensorflow

    what is that?and what should I do?

  16. Halbstein November 26, 2017 at 4:29 am #

    Is it possible to make a backup of the vitrualenv so i do not have to reinstall everything when something goes wrong?

    • Adrian Rosebrock November 27, 2017 at 1:10 pm #

      You can run “pip freeze” to save all dependencies, but keep in mind that OpenCV also relies on libraries installed globally as well. I would suggest backing up your system if at all possible.

  17. Jose Luis December 21, 2017 at 1:36 pm #

    Hi Adrian, first of all thanks, I got your book and I’ve been reading it even since. In step 3 I got stocked an error popped out

    /home/jose/opencv_contrib-3.3.0/modules/xfeatures2d/src/vgg.cpp:474:41: fatal error: vgg_generated_120.i: No such file or directory

    I followed all the steps and I actually don’t know how to proceed. Thanks a lot.

    • Adrian Rosebrock December 22, 2017 at 8:37 am #

      Hi Jose, did you make sure that the OpenCV-contrib package was included in your CMake command?

      • Jose Luis December 26, 2017 at 10:19 pm #

        Yes, I did, still the same error appeared. I started all over again and the error stopped. Thanks a lot!

        • Adrian Rosebrock December 27, 2017 at 9:52 am #

          Congrats on resolving the issue 🙂

  18. Alzbeta Vlachynska April 10, 2018 at 9:54 am #

    Hi Adrian,

    I’ve just finished the installation on the new Ubuntu 17.10 and I faced the problem with the fact that the Python3 is the default interpreter there, in the CMake configuration output there is no Python3 section and the library was never built.
    It could be fixed by adding this option to Cmake:

    -D PYTHON_DEFAULT_EXECUTABLE=/usr/bin/python3

    For more info please reffer to and

    Hope this information will save someone some time and nerves. 😉

    By the way, I am now making my way through your deep learning book and I must say, it is very well written, I can read it almost like belles-lettres. Thank you for that! 🙂

    All the best.

    • Adrian Rosebrock April 10, 2018 at 11:54 am #

      Thanks so much for sharing! Not only do I really appreciate it but I’m sure other PyImageSearch readers will as well.

      Belles-lettres is probably the highest compliment I have ever received on my writing. Thank you so much and I’m so happy to hear you are enjoying the book 🙂

  19. Arklegru April 13, 2018 at 7:19 pm #

    Hey Adrian,

    I ran into several problems over the course of setting up my ubuntu. I’ve solved most of them myself, but I’ve gotten stuck at getting my OpenCV working. All the compilation seems to work and I’ve followed the instructions for the symbolic linking. However, when testing to see if it works, when I try importing cv2 in python I get the error “ImportError: cannot enable executable stack as shared object requires: Invalid argument”
    I’ve looked online and they ( recommended running execstack on my files, which I did and it still doesn’t work. I’ve already tried redoing everything from scratch. Do you have any insights? My system is a 64-bit Windows 10 OS running Ubuntu. Thanks!

    • Adrian Rosebrock April 16, 2018 at 2:35 pm #

      Hi Arklegru — I’m sorry to hear about the issues installing OpenCV on your Ubuntu system. To be honest I have not encountered that error before but my guess is that there was a problem with your OpenCV + Python bindings. Go back to your “cmake” step and make sure your Python 2 or Python 3 section matches mind (depending on which version of Python you are using, of course).

      My guess is that you compiled OpenCV bindings for one version of Python (ex. Python 2.7) but are trying to import the bindings into a different OpenCV version (ex. Python 3).

  20. qin June 9, 2018 at 1:29 am #

    Hi, I have a problem here:
    when building openCV, I couldn’t get the And I discover in the log that I have no numpy information, ONLY interpreter information is there as is shown. I DO have installed numpy. Please help !!!

    Python 3:
    — Interpreter: /home/qin/.virtualenvs/dl4cv/bin/python3 (ver 3.5.2)

    — Python (for build): /usr/bin/python2.7

    • qin June 9, 2018 at 3:10 am #

      Turned out I have to apt install python3-numpy first. Then it works as the tutorial shows. Thanks for the information@Arklegru. But I don’t know the logic behind this since I already installed numpy for my virualen

      • Adrian Rosebrock June 13, 2018 at 6:09 am #

        That is very strange. You should not have needed to apt-get install NumPy. Also, you can ignore the “Python (for build)” section — that configuration is buggy. As long as your “Python 3” section is filled out correctly the compile will complete.

  21. Philipp August 10, 2018 at 10:15 am #


    I did this tutorial on a standard DigitalOcean Droplet and it worked great!

    Thanks for sharing!

    • Adrian Rosebrock August 15, 2018 at 9:24 am #

      Congrats on getting your Ubuntu system configured, Philipp!

  22. Aniket September 1, 2018 at 2:49 am #

    I got a error after last step saying :
    >>> import tensorflow
    Illegal instruction (core dumped)
    >>> import keras
    Illegal instruction (core dumped)

    I am using Ubuntu 16, CPU.
    all previous steps before last one that is was >>>import tensorflow….executed without any error

    so please suggest a solution.

    • Aniket September 1, 2018 at 3:10 am #

      hello sir,

      i solved this issue by downgrading tensorflow==1.5. it’s a great tutorial. installation done successfully.

      earlier i installed tensorflow 1.10.1, so giving error.

      Thanks Dr. Adrian

  23. Ainamazima Yoas September 11, 2018 at 4:46 am #

    Thanks Adrian, finally i manged to install all the environments.
    However, i had to downgrade tensorflow version from 1.10.1 to 1.5 due to an error (core dumped)

    I would request you to also make us guides to installation through Anaconda 5.2. (this was the biggest issue i was getting due to the fact that Anaconda was pointing to python 3.6) and thus could stop on symlinking #step3

    Thank you very much Adrian.

  24. BruceDai November 20, 2018 at 2:48 am #

    Thanks very much. Love your book and posts., helped me a lot.
    But I got a question, in all the commands involve ‘pip install’, do you actually mean ‘pip3 install’ instead? Since you have installed both python 2.7, python 3, pip 2, pip 3, the default of ‘pip install’ will be referring to python 2.7. Am I right? And how do I make a alias for pip3? I mean if I type ‘pip install’, actually I will be calling ‘pip3 install’, how do I change the names of it? Like change pip to pip2, pip3 to pip.

    • Adrian Rosebrock November 20, 2018 at 9:06 am #

      No, once you are inside your Python virtual environment the “pip” command automatically detects your Python version inside the virtual environment and installs any packages into the “site-packages” directory of the virtual environment.

Before you leave a comment...

Hey, Adrian here, author of the PyImageSearch blog. I'd love to hear from you, but before you submit a comment, please follow these guidelines:

  1. If you have a question, read the comments first. You should also search this page (i.e., ctrl + f) for keywords related to your question. It's likely that I have already addressed your question in the comments.
  2. If you are copying and pasting code/terminal output, please don't. Reviewing another programmers’ code is a very time consuming and tedious task, and due to the volume of emails and contact requests I receive, I simply cannot do it.
  3. Be respectful of the space. I put a lot of my own personal time into creating these free weekly tutorials. On average, each tutorial takes me 15-20 hours to put together. I love offering these guides to you and I take pride in the content I create. Therefore, I will not approve comments that include large code blocks/terminal output as it destroys the formatting of the page. Kindly be respectful of this space.
  4. Be patient. I receive 200+ comments and emails per day. Due to spam, and my desire to personally answer as many questions as I can, I hand moderate all new comments (typically once per week). I try to answer as many questions as I can, but I'm only one person. Please don't be offended if I cannot get to your question
  5. Do you need priority support? Consider purchasing one of my books and courses. I place customer questions and emails in a separate, special priority queue and answer them first. If you are a customer of mine you will receive a guaranteed response from me. If there's any time left over, I focus on the community at large and attempt to answer as many of those questions as I possibly can.

Thank you for keeping these guidelines in mind before submitting your comment.

Leave a Reply