Why cv2.so missing after opencv installed? - opencv

Today I installed opencv 2.4.4 to Ubuntu 12.10
But import cv2 not works.
root#-:~# python
Python 2.7.3 (default, Sep 26 2012, 21:53:58)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named cv2
>>>
As I understand cv2.so missed, so python don't see where opencv
root#-:~# find / -name "cv.py"
/root/opencv-2.4.4/modules/python/src2/cv.py
root#-:~# find / -name "cv2.so"
root#-:~#
My setup steps look like
wget http://downloads.sourceforge.net/project/opencvlibrary/opencv-unix/2.4.4/OpenCV-2.4.4a.tar.bz2
tar -xjf OpenCV-2.4.4a.tar.bz2
cd opencv-2.4.4
mkdir release
cd release
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_PYTHON_SUPPORT=ON ..
make && make install
echo "/usr/local/lib" >> /etc/ld.so.conf.d/opencv.conf
ldconfig
echo "PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig" >> /etc/bash.bashrc
echo "export PKG_CONFIG_PATH" >> /etc/bash.bashrc
Where is cv2.so ? And why it was missed ?

How to install opencv(cv2) with python bindings in Linux - Ubuntu/Fedora
Install gcc, g++/gcc-c++, cmake (apt-get or yum, in case of yum
use gcc-c++)
apt-get install gcc, g++, cmake
Downlaod latest opencv from openCV's website
Untar it with
tar -xvf opencv-*
Inside the untarred folder make a new folder called release
mkdir release
cd release
(or any folder name) and run this command in it
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_NEW_PYTHON_SUPPORT=ON -D BUILD_EXAMPLES=ON ..
the .. will pull files from the parents folder and will get the system ready for
installation on your platform.
in the release folder run
make
After about 2-3 mins of make processing when its finished run
sudo make install
Export python path
export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/site-packages
That's it, now go to python and try
>>> import cv2
you should not get any error message.
Tested on python 2.7, should be virtually similar to python 3.x.

I install python-opencv to solve my problem in Ubuntu 14.04
sh
sudo apt-get install python-opencv

Using raspbian on a rasberry pi I had the problem of the module not being found also. I had three versions of python (2.6, 2.7, and 3.2), be sure you are using python2.7. You can check this by running:
python --version
I found that for my case it was simply that I needed to install python-dev.
sudo apt-get install python-dev
I did Not have to remove and reinstall opencv, I tried my hardest to avoid that, knowing that it takes a few hours to complete the process.
After installing python-dev I went to the file I built the opencv into, for me it was " ~/opencv-2.4.9/release", and told it to make
sudo make
after this I was able to find the cv2.so file. searching for it with:
find / -name "cv2.so"
at this point I found a few files.
next I ran just the python to see if it could find "import" them
python
>>> import cv2
no errors should come up.
>>> import numpy
I have heard that numpy was necessary for opencv to run.
From there I believe you should be good to run your script, if no errors come about. I hope this helps.
The page that helped me is listed...
http://opencv-users.1802565.n2.nabble.com/I-can-t-find-cv-so-and-cv2-so-after-compiling-td6671937.html

I had a similar problem when I manually configured using CMAKE on OSX El Capitan. I had given this additional option:
PYTHON2_PACKAGES_PATH='lib/python2.7/site-packages'
which stopped the cv2.so in that package from getting installed. It seems to install properly in my build folder after I removed it:
PYTHON2_EXECUTABLE='/usr/bin/python2.7'
PYTHON2_INCLUDE_DIR='/usr/include/python2.7'
PYTHON2_LIBRARY='/usr/lib/libpython2.7.dylib' # TODO - Fix for linux
PYTHON2_NUMPY_INCLUDE_DIRS='/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/core/include' # Todo - Fix for linux
cd $OPENCV_DIR
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=$OPENCV_INSTALL_PATH \
-D WITH_CUDA=OFF \
-D BUILD_opencv_python2:BOOL=ON \
-D PYTHON2_EXECUTABLE=$PYTHON2_EXECUTABLE \
-D PYTHON2_INCLUDE_DIR=$PYTHON2_INCLUDE_DIR \
-D PYTHON2_LIBRARY=$PYTHON2_LIBRARY \
-D PYTHON2_NUMPY_INCLUDE_DIRS=$PYTHON2_NUMPY_INCLUDE_DIRS \
-D INSTALL_PYTHON_EXAMPLES:BOOL=ON \
..
make -j8
make install

I have this problem in my OS X El Capitan.
I followed the instructions mentioned in this tutorial. Didn't get a successful working install and had the above error of missing cv2.so file in the required folders mentioned and at the python prompt.
Finally figured that using the virtual python setup was causing trouble. So uninstalled with
pip install virtualenv virtualenvwrapper
Then ran
brew link opencv
which threw errors.
And then followed below steps to resolve the issue.
First run
brew link opencv
If it gives an error, try for an automated diagnosis
brew doctor
brew doctor gives a list of problems that could be leading to errors in installation process.
To fix problems in case of conflicting files,
run to get a list of all actions which will be performed by overwrite without actually performing them.
To list all files that would be deleted:
brew link --overwrite --dry-run opencv
followed by this run which will execute the overwrite, assuming you feel that the actions performed by overwrite will take your system to a more stable state.
To force the link and overwrite all conflicting files:
brew link --overwrite opencv
This tutorial is a simpler alternative.

None of the above worked for me; am in Ubuntu 16.04 on an ec2 instance & had anaconda installed so I just used
conda install opencv for both my conda2 and 3 installs

All above answers did not work for me, however, after a whole day struggling, I finally solved this problem.
To have cv2.so, we need:
At least python 2 or 3 installed. that why people say: sudo apt-get install python-dev. But that's not necessary, in my case, I use anaconda python. (there are many ways to install python)
numpy is also a must. so, whatever python you use, just make sure you have it downloaded. In my case, I use anaconda numpy. (anaconda have it installed already, for normal python, use pip install numpy)
To tell camke where the path is, just take my command as an example:
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules \
-D PYTHON2_EXECUTABLE='/home/parallels/anaconda2/bin/python' \
-D PYTHON2_LIBRARY='/home/parallels/anaconda2/lib/python2.7' \
-D PYTHON2_NUMPY_INCLUDE_DIRS='/home/parallels/anaconda2/lib/python2.7/site-packages/numpy/core/include' \
-D BUILD_EXAMPLES=ON ..
for python3,you should (I'm using anaconda python, so I linked everything to anaconda):
cmake -D CMAKE_BUILD_TYPE=Release \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib-3.3.1/modules \
-D PYTHON3_EXECUTABLE='/home/test/SoftWare/anaconda3/bin/python3.6m' \
-D PYTHON_INCLUDE_DIR='/home/test/SoftWare/anaconda3/include/python3.6m' \
-D PYTHON3_LIBRARY='/home/test/SoftWare/anaconda3/lib/libpython3.6m.so' \
-D PYTHON3_NUMPY_INCLUDE_DIRS='/home/test/SoftWare/anaconda3/lib/python3.6/site-packages/numpy/core/include' \
-D PYTHON3_PACKAGES_PATH='/home/test/SoftWare/anaconda3/lib/python3.6/site-packages' ..
One thing to remember!!! before you enter cmake ... 1. clean your build folder, 2. Only camke once! otherwise you can not change ** PYTHON3_LIBRARY: NO**...(this is a bug I think)
I know there might be some useless arguments, but I am tired to try to clean them. Here's a screenshot of my cmake print info.
screenshot of my cmake info
You can clearly see that, only python2 can generate cv2.so. python3 can not! (Python3 wrappers can not be generated).

I came across similar problem. After digging into this a little more I came across a post where it was mentioned that python-numpy package was required. So, I uninstalled the opencv by running the following command in build folder(in your case release folder):
dpkg -r build
Then I removed all the opencv files. I installed python-numpy and python-dev with this command:
sudo apt-get install python-dev python-numpy
Then, after re-running the installation script, import cv2 command in python console doesn't give me any error and is properly imported.

In my case it was a problem with cmake:
sudo apt install software-properties-common
sudo add-apt-repository ppa:george-edison55/cmake-3.x
sudo apt update
If cmake is not yet installed:
sudo apt install cmake
If cmake is already installed:
sudo apt upgrade
For more information, see this link.

Related

Singularity containers: adding custom packages to $PATH and pass it to singularity exec

I'm in the process of making a singularity container with PLINK v 1.9 (a genetics software, not the PuTTY interface, https://www.cog-genomics.org/plink/1.9/dev) inside it. I'm able to successfully compile the software within the container and I can see the PLINK files and functions when I shell into the container. To make things easier for my end users, I am trying to add the software's main folder to the singularity $PATH so that they can just call plink from the command line, but I'm not able to achieve this.
So far I have tried:
Assigning SINGULARITYENV_APPEND_PATH=path/to/plink in the %runscript section of my singularity recipe file.
Moving the source code folder for Plink into /usr/local/bin of the Singularity
Setting a variable PLINKDIR=/path/to/plink and the export it in the %environment section of my recipe file similar to what they have done here.
None of these methods work. The only way I can access this package is by spelling out the full path in my singularity exec command.
singularity exec test.simg /path/to/plink/plink --version
I would like to just run
singularity exec test.simg plink --version
Contents of my singularity recipe file:
BootStrap: docker
From: ubuntu:16.04
%labels
Plink Version 1.9
%runscript
# Allows us to run the versions of the packages installed in the post
# section
exec plink19 "$#"
%environment
export SINGULARITYENV_PLINKDIR="/plink-ng-b15c19f/1.9/"
%post
# Make some directories for storage
mkdir /new_folder_1 /new_folder_2
apt-get update
apt-get -y upgrade
apt-get install -y --no-install-recommends \
software-properties-common \
build-essential \
apt-transport-https \
curl \
git \
wget \
libfreetype6-dev \
pkg-config \
python \
python-dev \
dh-autoreconf \
libarchive-dev \
libatlas-dev \
libatlas-base-dev \
zlib1g-dev
# Install PLINK
wget https://github.com/chrchang/plink-ng/archive/b15c19f.tar.gz
tar xvf b15c19f.tar.gz
cd /plink-ng-b15c19f/1.9/
./plink_first_compile
ln -s plink plink19
%test
echo $PLINKDIR
The echo statement outputs nothing. How do I add custom packages like this (meaning not installed using apt-get) to the singularity environment so that they can be called from the command line with singularity exec? Any help would be appreciated!
The first part of your message mentions "so that they can just call plink from the command line, but I'm not able to achieve this." In that case why not just:
apt-get install -y plink1.9
In your definition file?
And In my case, If I try singularity exec imgfile.sif plink --version it obviously doesn't work since you have to call plink1.9 so: singularity exec imagfile.sif plink1.9 --version.
You should be able to just add it to your path:
%environment
PATH=$PATH:/path/to/plink/
Also, edit your runscript:
%runscript
exec "$#"
Then you can run:
singularity run test.simg plink --version

Trying to execute easy_install or pip on docker build says command not found

I am trying to build a docker container based on amazonlinux which is sort of centos.
One of the packages I need is supervisor and it is not available on the official repos so I have to do it with easy_install or pip.
The problem is that, although I tried installing python-setuptools and python-pip, then when I try to do:
RUN easy_install supervisor
or
RUN pip install supervisor
It says the command doesn't exists
/bin/sh: easy_install: command not found
The command '/bin/sh -c easy_install supervisor' returned a non-zero code: 127
I tried with full path, but same result, and I see other dockerfiles people doing it like that on centos images.
After a while, I found the reason.
By default, yum was installing python26 and the easy_install script runs with python27, so I had to be calling easy_install-2.6 or install the python27 package
Not familiar with AWS's specific image, but for a general centos image, you'll need to install pip or easy_install with a yum command first, which requires the epel repository:
RUN yum -y install epel-release \
&& yum -y install python-pip python-setuptools \
&& yum clean all
Python documented the process in detail on their page here: https://packaging.python.org/install_requirements_linux/
There's also some documentation on this over at superuser: https://superuser.com/q/877759/587488

Failed to run Apache Singa after installation

Followed the instruction and installed Apache Singa v1.0.0 from the wheel successfully, but failed to run it below,
(singa) $ pip list | grep singa
singa (1.0.0)
(singa) $ python
> import singa
> ImportError: No module named '_singa_wrap'
(singa) $ find -name "*singa_wrap*"
singa_wrap.py
singa_wrap.pyc
_singa_wrap.so
Seemed something suspicious with Swig module extension. Any suggestions?
#EDIT
Verified that protobuf 2.6.1 already installed globally below,
(singa) $ python -c "from singa import _singa_wrap"
undefined symbol: _ZNK6google8protobuf7Message11GetTypeNameEv
$ ldd _singa_wrap.so
libprotobuf.so.9 => /usr/lib/x86_64-linux-gnu/libprotobuf.so.9
$ dpkg -S libprotobuf.so.9
libprotobuf9v5
$ apt-cache policy libprotobuf9v5
Installed: 2.6.1-1.3
#Solution
Singa starts dancing now after protobuf 2.6.1 being successfully installed locally on Ubuntu 16.04 below,
$ sudo apt-get install python-pip # gcc-5 required thus installed
$ sudo apt-get install gcc-4.8
$ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 50 --slave /usr/bin/g++ g++ /usr/bin/g++-4.8
$ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 60 --slave /usr/bin/g++ g++ /usr/bin/g++-5
$ sudo update-alternatives --config gcc # to choose gcc-4.8
The rest then follows the answer #Wei below. Thanks again.
I think you can try the approaches in the first FAQ http://singa.apache.org/en/docs/installation.html#faq
In addition, you can check the dependent libs of _singa_wrap.so (go to /Python2.7/site-packages/singa/, and ldd _singa_wrap.so. If any library (e.g. cudnn or protobuf) is not found, then try to install it or export the library path.
There is a ticket for your issue https://issues.apache.org/jira/browse/SINGA-255.
Will update you once it is done.
Thanks.
UPDATE:
It seems your error is related to protobuf (could be caused by multiple versions of protobuf on your computer). Here is the solution,
Download protobuf (protobuf-2.6.1.tar.gz on github) and decompress the tar file
Install protobuf into /home//local/ by
./configure --prefix=/home/<yourname>/local
make && make install
echo "export LD_LIBRARY_PATH=/home/<yourname>/local/lib:$LD_LIBRARY_PATH" >> ~/.bashrc
source ~/.bashrc
pip uninstall singa
pip install <path to the wheel file>

Sudo make install permissions issue on Ubuntu 14.04

I have amended a script from https://github.com/jayrambhia/Install-OpenCV/blob/master/Ubuntu/2.4/opencv2_4_10.sh to try to install OpenCV 2.4.13 onto a vm running Ubuntu 14.04 where I have sudo permission. I'm new to openCV, cmake & make so any help getting this script to work would be appreciated as I have to install it on 20 vm's.
After a few minutes of running the script returns with an error saying can't
/bin/sh: 1: cd: can't cd to /home/myaccount/setups/OpenCV/opencv-2.4.13/build
and
make[2]: *** [3rdparty/libtiff/CMakeFiles/libtiff.dir/depend] Error 2
make[1]: *** [3rdparty/libtiff/CMakeFiles/libtiff.dir/all] Error 2
The last few lines of the output is included below and the script is before that.
Any ideas why the script wont complete successfully or how to correct the permissions issue?
Extra Details
I'm not sure if these are relevant but as I dig into the problem I'll update this section
cmake version 2.8
Makerfile mentions # The shell in which to execute make rules. SHELL
= /bin/sh but my terminal reports echo $0 as bash
when I run make install V=1 it builds 100% but then reports
-- Install configuration: "RELEASE" CMake Error at cmake_install.cmake:36 (FILE): file cannot create directory: /usr/local/include/opencv2. Maybe need administrative privileges
But when I run sudo make install V=1 I get can't cd error above
Adding Shebang #!/bin/bash to the start of my script didn't solve it
Running the script on a fresh ubuntu local machine alows the script to run but I need to get it running on the hosted vm
umask of setups folder (where script runs and creates a directory is 0022 . Permissions for setups folder is 755, group = Domain Users, Owner = myaccount Thats where the source files folder gets created as well as the build folder which is also 755 after it is created
Script
arch=$(uname -m)
if [ "$arch" == "i686" -o "$arch" == "i386" -o "$arch" == "i486" -o "$arch" == "i586" ]; then
flag=1
else
flag=0
fi
echo "Installing OpenCV 2.4.13"
mkdir OpenCV
cd OpenCV
echo "Removing any pre-installed ffmpeg and x264"
sudo apt-get -y remove ffmpeg x264 libx264-dev
echo "Installing unzip"
sudo apt-get -y install unzip
echo "Installing Dependenices"
sudo apt-get -y install libopencv-dev
sudo apt-get -y install build-essential checkinstall cmake pkg-config yasm
sudo apt-get -y install libtiff4-dev libjpeg-dev libjasper-dev
sudo apt-get -y install libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev libxine-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev
sudo apt-get -y install python-dev python-numpy
sudo apt-get -y install libtbb-dev libeigen3-dev
sudo apt-get -y install libqt4-dev libgtk2.0-dev
sudo apt-get -y install libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-core-dev
sudo apt-get -y install x264 v4l-utils ffmpeg
sudo apt-get -y install libgtk2.0-dev
echo "Downloading OpenCV 2.4.13"
if ! [ -f "OpenCV-2.4.13.zip" ]; then
wget -O OpenCV-2.4.13.zip http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.4.13/opencv-2.4.13.zip/download
fi
echo "Installing OpenCV 2.4.13"
if ! [ -d "opencv-2.4.13" ]; then
unzip OpenCV-2.4.13.zip
fi
rm OpenCV-2.4.13.zip
cd opencv-2.4.13
rm -rf build
mkdir build
cd build
cmake -D CUDA_ARCH_BIN=3.2 -D CUDA_ARCH_PTX=3.2 -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D BUILD_TIFF=ON -D WITH_QT=ON -D WITH_OPENGL=ON ..
make -j$(nproc)
sudo make install
sudo sh -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig
echo "OpenCV 2.4.13 ready to be used"
Command Output
[100%] Build Java tests
Buildfile: /home/myaccount/setups/OpenCV/opencv-2.4.13/build/modules/java/test/.build/build.xml
build:
compile:
[mkdir] Created dir: /home/myaccount/setups/OpenCV/opencv-2.4.13/build/modules/java/test/.build/build/classes
[javac] Compiling 104 source files to /home/myaccount/setups/OpenCV/opencv-2.4.13/build/modules/java/test/.build/build/classes
jar:
[mkdir] Created dir: /home/myaccount/setups/OpenCV/opencv-2.4.13/build/modules/java/test/.build/build/jar
[jar] Building jar: /home/myaccount/setups/OpenCV/opencv-2.4.13/build/modules/java/test/.build/build/jar/opencv-test.jar
BUILD SUCCESSFUL
Total time: 6 seconds
[100%] Built target opencv_test_java
[sudo] password for myaccount:
Sorry, try again.
[sudo] password for myaccount:
/bin/sh: 1: cd: can't cd to /home/myaccount/setups/OpenCV/opencv-2.4.13/build
make[2]: *** [3rdparty/libtiff/CMakeFiles/libtiff.dir/depend] Error 2
make[1]: *** [3rdparty/libtiff/CMakeFiles/libtiff.dir/all] Error 2
make: *** [all] Error 2
OpenCV 2.4.13 ready to be used
myaccount#vm-20161023-002:~/setups$
What popped up into my head to explain the lack of permission problem: if your /home is on NFS, root may have nobody's permission on files there. There are quite a few related questions on StackExchange.
My solution would be not to try installing third-party software as root in the first place. Executing as root, your make install can do anything, including adding or overwriting files that are already managed by your software package manager. This may confuse the package manager or even render your system unusable. It's better to install the software somewhere else; /usr/local and /opt were created for this purpose. In order to make sure your installation procedure doesn't deviate from this, you can create a different user (which I call local), chown -R these directories to that user, and install using sudo -u local instead of sudoing to root. This will be fine for most installations, and should you run into one that tries to do something requiring root permissions (such as writing files to /etc or /usr/bin, restarting system services, doing tricky things with permissions, etc.), it will fail with an error message, allowing you to decide whether you want this before it has already happened. I have no idea whether OpenCV requires any such steps.

wicked_pdf does not run on Ubuntu server : wkhtmltopdf: cannot connect to X server

I'm trying to use wicked_pdf on my prod server but it keeps failling :
RuntimeError (Failed to execute:
"/usr/bin/wkhtmltopdf" -q "file:////tmp/wicked_pdf20130709-23109-1adqx5g.html" "/tmp/wicked_pdf_generated_file20130709-23109-1ic5dbe.pdf"
Error: PDF could not be generated!
Command Error: wkhtmltopdf: cannot connect to X server
):
app/controllers/contrats_controller.rb:15:in `block (2 levels) in show'
app/controllers/contrats_controller.rb:11:in `show'
I tried to follow this answer : wkhtmltopdf: cannot connect to X server but it still does not work.
This post helped me to resolve my problem :
http://www.stormconsultancy.co.uk/blog/development/generating-pdfs-in-rails-with-pdfkit-and-deploying-to-a-server/
I'm reproducing here the step from this post that helped me to install it :
# first, installing dependencies
sudo aptitude install openssl build-essential xorg libssl-dev
# for 64bits OS
wget http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.9.9-static-amd64.tar.bz2
tar xvjf wkhtmltopdf-0.9.9-static-amd64.tar.bz2
mv wkhtmltopdf-amd64 /usr/local/bin/wkhtmltopdf
chmod +x /usr/local/bin/wkhtmltopdf
He also create an initializer to tell to pdfKit where it is, so this method is for wicked PDF and PDF Kit.
Resolved this problem in this tread https://stackoverflow.com/a/34947479/5320149
I found method to resolve this problem without fake X server.
In newest version of wkhtmltopdf dont need X server for work, but it no into official linux repositories.
Solution for Ubuntu 14.04.4 LTS (trusty) i386
$ sudo apt-get install xfonts-75dpi
$ wget https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/0.12.2.1/wkhtmltox-0.12.2.1_linux-trusty-i386.deb
$ sudo dpkg -i wkhtmltox-0.12.2.1_linux-trusty-i386.deb
$ wkhtmltopdf http://www.google.com test.pdf
Solution for Ubuntu 14.04.4 LTS (trusty) amd64
$ sudo apt-get install xfonts-75dpi
$ wget https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/0.12.2.1/wkhtmltox-0.12.2.1_linux-trusty-amd64.deb
$ sudo dpkg -i wkhtmltox-0.12.2.1_linux-trusty-amd64.deb
$ wkhtmltopdf http://www.google.com test.pdf
Verify you have xvfb installed, or install it using apt-get
install xvfb.
Create a file called wkhtmltopdf.sh and add the following:
xvfb-run -a -s "-screen 0 640x480x16" wkhtmltopdf $*
Change the dimensions (640x480x16) to match whatever virtual screen parameters you want it to emulate.
Move the shell script to /usr/bin, and set permissions:
sudo chmod a+x /usr/bin/wkhtmltopdf.sh
Optionally, you can add a symbolic link in your project directory:
ln -s /usr/bin/wkhtmltopdf.sh wkhtmltopdf
See this and this for reference.

Resources