Computer Vision Drone

Install Opencv On Aws With Cuda

4 minute read

Install OpenCV 3.2 Ubuntu on AWS GPU instances with CUDA Support

To test some state of the art Convolutional Neural Networks (CNNs) we need CUDA support.

WARNING: running your GPU instance on AWS will create costs. Make sure you stop it, when you don't need it anymore.

Setup AWS instance

  • Login to your AWS console. Select EU-Ireland as your location.
  • Goto Services / EC2
  • Click on Launch Instance
  • Select “Ubuntu Server 16.04 LTS (HVM), SSD Volume Type”

Select Instance type

  • Select g2.2xlarge ; This instance comes with
    • 8 vCPUs
    • 15 GB RAM and
    • 60 GB SSD storage
    • 1536 CUDA-Cores (nvidia)
    • 4 GB Video RAM
  • click configure details

Network settings

  • click “Request Spot Instances” to save money (instance price will come cheaper, but instance maybe terminated at any time)
  • select a network/subnet that allows Public IPs


  • Increase storage of first drive to: 30 GB general purpose SSD
  • Next: add tags


  • Name it “opencv-gpu-instance”
  • Next: Configure security group

Security group

  • Create a security group that allows SSH access to the machine
  • Hit review and launch
  • Launch
  • Create and download keypair

Move your keypair

mv ~/Downloads/gpu-instance.pem ~/.ssh/
chmod 600 ~/.ssh/gpu-instance.pem
ssh-add ~/.ssh/gpu-intance.pem

Test ssh connection (get public ip of your instance from AWS GUI)

ssh ubuntu@54.171.225.XXX
Welcome to Ubuntu 16.04.1 LTS (GNU/Linux 4.4.0-59-generic x86_64)

 * Documentation:
 * Management:
 * Support:

  Get cloud support with Ubuntu Advantage Cloud Guest:

0 packages can be updated.
0 updates are security updates.

Update system

sudo -i
apt-get update
apt-get dist-upgrade

Setup OS dependencies

apt-get install -y gcc g++ gfortran build-essential \
        git wget linux-image-generic libopenblas-dev \
        python-dev python-pip python-nose python-numpy \
        python-scipy liblapack-dev libblas-dev cmake unzip \
        pkg-config libopenblas-dev linux-source linux-headers-generic \
        python3 python3-pip

setup cuda 8.0

mkdir /mnt/tmp
cd /mnt/tmp
sh cuda_8.0.44_linux-run


Install NVIDIA Accelerated Graphics Driver for Linux-x86_64 367.48?
(y)es/(n)o/(q)uit: y

Do you want to install the OpenGL libraries?
(y)es/(n)o/(q)uit [ default is yes ]: y

Install the CUDA 8.0 Toolkit?
(y)es/(n)o/(q)uit: y

Enter Toolkit Location
 [ default is /usr/local/cuda-8.0 ]:

Do you want to install a symbolic link at /usr/local/cuda?
(y)es/(n)o/(q)uit: y

Install the CUDA 8.0 Samples?
(y)es/(n)o/(q)uit: y

Enter CUDA Samples Location
 [ default is /home/ubuntu ]: 


= Summary =

Driver:   Installed
Toolkit:  Installed in /usr/local/cuda-8.0
Samples:  Installed in /home/ubuntu, but missing recommended libraries

Please make sure that
 -   PATH includes /usr/local/cuda-8.0/bin
 -   LD_LIBRARY_PATH includes /usr/local/cuda-8.0/lib64, or, add /usr/local/cuda-8.0/lib64 to /etc/ and run ldconfig as root

Setup paths

echo "/usr/local/cuda-8.0/lib64" >> /etc/
echo 'PATH=$PATH:/usr/local/cuda-8.0/bin' >> /etc/profile

# logout, login

setup cuDNN

To install cuDNN you need an nvidia developer account.

scp Downloads/cudnn-8.0-linux-x64-v5.1.tgz ubuntu@YOUR_PUB_IP:~
ssh ubuntu@YOUR_PUB_IP
tar -xvf cudnn-8.0-linux-x64-v5.1.tgz 

sudo cp cuda/lib64/* /usr/local/cuda/lib64
sudo cp cuda/include/* /usr/local/cuda/include/

setup virtualenvs

pip3 install virtualenv virtualenvwrapper
pip3 install --upgrade pip

echo "export WORKON_HOME=$HOME/.virtualenvs" >> ~/.bashrc
echo "source /usr/local/bin/" >> ~/.bashrc
source ~/.bashrc

mkvirtualenv computer-vision -p python3
pip install numpy scipy scikit-image imutils matplotlib

install opencv 3.2

wget -O
wget -O

mkdir ~/opencv-3.2.0/build
cd ~/opencv-3.2.0/build

workon computer-vision

    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D WITH_CUBLAS=1 \
    -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.2.0/modules \

Result should be like this:

--     Use CUFFT:                   YES
--     Use CUBLAS:                  YES
--     USE NVCUVID:                 NO
--     NVIDIA GPU arch:             20 30 35 37 50 52 60 61
--     NVIDIA PTX archs:
--     Use fast math:               YES
--   Python 3:
--     Interpreter:                 /home/ubuntu/.virtualenvs/computer-vision/bin/python3 (ver 3.5.2)
--     Libraries:                   /usr/lib/x86_64-linux-gnu/ (ver 3.5.2)
--     numpy:                       /home/ubuntu/.virtualenvs/computer-vision/lib/python3.5/site-packages/numpy/core/include (ver 1.12.0)
--     packages path:               lib/python3.5/site-packages

compile and install opencv

make -j8
sudo make install
sudo ldconfig

If the build crashes on:

[ 27%] Building CXX object modules/core/CMakeFiles/opencv_core.dir/src/hal_internal.cpp.o
In file included from /home/ubuntu/opencv-3.2.0/modules/core/src/hal_internal.cpp:49:0:
/home/ubuntu/opencv-3.2.0/build/opencv_lapack.h:2:45: fatal error: LAPACKE_H_PATH-NOTFOUND/lapacke.h: No such file or directory
compilation terminated.
modules/core/CMakeFiles/opencv_core.dir/build.make:361: recipe for target 'modules/core/CMakeFiles/opencv_core.dir/src/hal_internal.cpp.o' failed
make[2]: *** [modules/core/CMakeFiles/opencv_core.dir/src/hal_internal.cpp.o] Error 1
make[2]: *** Waiting for unfinished jobs....


sudo apt-get install liblapacke-dev checkinstall

and try again.

cd ~/.virtualenvs/computer-vision/lib/python3.5/site-packages
ln -s /usr/local/lib/python3.5/site-packages/

test installation


import cv2


(computer-vision) ubuntu@ip-XXXXXXXXXX:~/.virtualenvs/computer-vision/lib/python3.5/site-packages$ python
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__