Can't install mongoengine inside virtualenv
Question: When I try to install mongoengine in virtualenv, I've got problem:

    
    
    Requirement already satisfied (use --upgrade to upgrade): flask-mongoengine in ./lib/python2.7/site-packages
    Requirement already satisfied (use --upgrade to upgrade): Flask>=0.8 in ./lib/python2.7/site-packages (from flask-mongoengine)
    Downloading/unpacking mongoengine>=0.7.10 (from flask-mongoengine)
      Running setup.py (path:/var/www/msgapp/backend/build/mongoengine/setup.py) egg_info for package mongoengine
        0.8.7
    
        no previously-included directories found matching 'docs/_build'
    Downloading/unpacking flask-wtf (from flask-mongoengine)
      Running setup.py (path:/var/www/msgapp/backend/build/flask-wtf/setup.py) egg_info for package flask-wtf
    
        warning: no previously-included files matching '*.pyc' found under directory 'tests'
        warning: no previously-included files matching '*.pyc' found under directory 'tests'
        warning: no previously-included files matching '*.pyc' found under directory 'docs'
        warning: no previously-included files matching '*.pyo' found under directory 'docs'
        no previously-included directories found matching 'docs/_build'
        no previously-included directories found matching 'docs/_themes/.git'
    Requirement already satisfied (use --upgrade to upgrade): Werkzeug>=0.7 in ./lib/python2.7/site-packages (from Flask>=0.8->flask-mongoengine)
    Requirement already satisfied (use --upgrade to upgrade): Jinja2>=2.4 in ./lib/python2.7/site-packages (from Flask>=0.8->flask-mongoengine)
    Requirement already satisfied (use --upgrade to upgrade): itsdangerous>=0.21 in ./lib/python2.7/site-packages (from Flask>=0.8->flask-mongoengine)
    Requirement already satisfied (use --upgrade to upgrade): pymongo>=2.5 in ./lib/python2.7/site-packages (from mongoengine>=0.7.10->flask-mongoengine)
    Requirement already satisfied (use --upgrade to upgrade): WTForms>=1.0.5,<2.0 in ./lib/python2.7/site-packages (from flask-wtf->flask-mongoengine)
    Requirement already satisfied (use --upgrade to upgrade): markupsafe in ./lib/python2.7/site-packages (from Jinja2>=2.4->Flask>=0.8->flask-mongoengine)
    Installing collected packages: mongoengine, flask-wtf
      Running setup.py install for mongoengine
        0.8.7
    
        no previously-included directories found matching 'docs/_build'
        error: could not delete '/var/www/msgapp/backend/lib/python2.7/site-packages/bson/json_util.py': Permission denied
        Complete output from command /var/www/msgapp/backend/bin/python2.7 -c "import setuptools, tokenize;__file__='/var/www/msgapp/backend/build/mongoengine/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-811vit-record/install-record.txt --single-version-externally-managed --compile --install-headers /var/www/msgapp/backend/include/site/python2.7:
        0.8.7
    
    running install
    running build
    running build_py
    creating build
    creating build/lib.linux-x86_64-2.7
    creating build/lib.linux-x86_64-2.7/mongoengine
    copying mongoengine/common.py -> build/lib.linux-x86_64-2.7/mongoengine
    copying mongoengine/document.py -> build/lib.linux-x86_64-2.7/mongoengine
    copying mongoengine/context_managers.py -> build/lib.linux-x86_64-2.7/mongoengine
    copying mongoengine/__init__.py -> build/lib.linux-x86_64-2.7/mongoengine
    copying mongoengine/connection.py -> build/lib.linux-x86_64-2.7/mongoengine
    copying mongoengine/errors.py -> build/lib.linux-x86_64-2.7/mongoengine
    copying mongoengine/dereference.py -> build/lib.linux-x86_64-2.7/mongoengine
    copying mongoengine/python_support.py -> build/lib.linux-x86_64-2.7/mongoengine
    copying mongoengine/signals.py -> build/lib.linux-x86_64-2.7/mongoengine
    copying mongoengine/fields.py -> build/lib.linux-x86_64-2.7/mongoengine
    creating build/lib.linux-x86_64-2.7/bson
    copying bson/json_util.py -> build/lib.linux-x86_64-2.7/bson
    copying bson/objectid.py -> build/lib.linux-x86_64-2.7/bson
    copying bson/py3compat.py -> build/lib.linux-x86_64-2.7/bson
    copying bson/tz_util.py -> build/lib.linux-x86_64-2.7/bson
    copying bson/max_key.py -> build/lib.linux-x86_64-2.7/bson
    copying bson/binary.py -> build/lib.linux-x86_64-2.7/bson
    copying bson/__init__.py -> build/lib.linux-x86_64-2.7/bson
    copying bson/code.py -> build/lib.linux-x86_64-2.7/bson
    copying bson/son.py -> build/lib.linux-x86_64-2.7/bson
    copying bson/errors.py -> build/lib.linux-x86_64-2.7/bson
    copying bson/timestamp.py -> build/lib.linux-x86_64-2.7/bson
    copying bson/regex.py -> build/lib.linux-x86_64-2.7/bson
    copying bson/dbref.py -> build/lib.linux-x86_64-2.7/bson
    copying bson/min_key.py -> build/lib.linux-x86_64-2.7/bson
    creating build/lib.linux-x86_64-2.7/mongoengine/base
    copying mongoengine/base/metaclasses.py -> build/lib.linux-x86_64-2.7/mongoengine/base
    copying mongoengine/base/common.py -> build/lib.linux-x86_64-2.7/mongoengine/base
    copying mongoengine/base/document.py -> build/lib.linux-x86_64-2.7/mongoengine/base
    copying mongoengine/base/__init__.py -> build/lib.linux-x86_64-2.7/mongoengine/base
    copying mongoengine/base/datastructures.py -> build/lib.linux-x86_64-2.7/mongoengine/base
    copying mongoengine/base/fields.py -> build/lib.linux-x86_64-2.7/mongoengine/base
    creating build/lib.linux-x86_64-2.7/mongoengine/django
    copying mongoengine/django/shortcuts.py -> build/lib.linux-x86_64-2.7/mongoengine/django
    copying mongoengine/django/sessions.py -> build/lib.linux-x86_64-2.7/mongoengine/django
    copying mongoengine/django/tests.py -> build/lib.linux-x86_64-2.7/mongoengine/django
    copying mongoengine/django/storage.py -> build/lib.linux-x86_64-2.7/mongoengine/django
    copying mongoengine/django/__init__.py -> build/lib.linux-x86_64-2.7/mongoengine/django
    copying mongoengine/django/auth.py -> build/lib.linux-x86_64-2.7/mongoengine/django
    copying mongoengine/django/utils.py -> build/lib.linux-x86_64-2.7/mongoengine/django
    creating build/lib.linux-x86_64-2.7/mongoengine/queryset
    copying mongoengine/queryset/queryset.py -> build/lib.linux-x86_64-2.7/mongoengine/queryset
    copying mongoengine/queryset/base.py -> build/lib.linux-x86_64-2.7/mongoengine/queryset
    copying mongoengine/queryset/visitor.py -> build/lib.linux-x86_64-2.7/mongoengine/querset
    copying mongoengine/queryset/__init__.py -> build/lib.linux-x86_64-2.7/mongoengine/queryset
    copying mongoengine/queryset/field_list.py -> build/lib.linux-x86_64-2.7/mongoengine/queryset
    copying mongoengine/queryset/transform.py -> build/lib.linux-x86_64-2.7/mongoengine/queryset
    copying mongoengine/queryset/manager.py -> build/lib.linux-x86_64-2.7/mongoengine/queryset
    creating build/lib.linux-x86_64-2.7/mongoengine/django/mongo_auth
    copying mongoengine/django/mongo_auth/__init__.py -> build/lib.linux-x86_64-2.7/mongoengine/django/mongo_auth
    copying mongoengine/django/mongo_auth/models.py -> build/lib.linux-x86_64-2.7/mongoengine/django/mongo_auth
    
    running egg_info
    
    writing requirements to mongoengine.egg-info/requires.txt
    
    writing mongoengine.egg-info/PKG-INFO
    
    writing top-level names to mongoengine.egg-info/top_level.txt
    
    writing dependency_links to mongoengine.egg-info/dependency_links.txt
    
    warning: manifest_maker: standard file '-c' not found
    
    
    
    reading manifest file 'mongoengine.egg-info/SOURCES.txt'
    
    reading manifest template 'MANIFEST.in'
    
    no previously-included directories found matching 'docs/_build'
    
    writing manifest file 'mongoengine.egg-info/SOURCES.txt'
    
    running install_lib
    
    copying build/lib.linux-x86_64-2.7/mongoengine/base/metaclasses.py -> /var/www/msgapp/backend/lib/python2.7/site-packages/mongoengine/base
    
    copying build/lib.linux-x86_64-2.7/mongoengine/base/common.py -> /var/www/msgapp/backend/lib/python2.7/site-packages/mongoengine/base
    
    copying build/lib.linux-x86_64-2.7/mongoengine/base/document.py -> /var/www/msgapp/backend/lib/python2.7/site-packages/mongoengine/base
    
    copying build/lib.linux-x86_64-2.7/mongoengine/base/__init__.py -> /var/www/msgapp/backend/lib/python2.7/site-packages/mongoengine/base
    
    copying build/lib.linux-x86_64-2.7/mongoengine/base/datastructures.py -> /var/www/msgapp/backend/lib/python2.7/site-packages/mongoengine/base
    
    copying build/lib.linux-x86_64-2.7/mongoengine/base/fields.py -> /var/www/msgapp/backend/lib/python2.7/site-packages/mongoengine/base
    
    copying build/lib.linux-x86_64-2.7/mongoengine/django/mongo_auth/__init__.py -> /var/www/msgapp/backend/lib/python2.7/site-packages/mongoengine/django/mongo_auth
    
    copying build/lib.linux-x86_64-2.7/mongoengine/django/mongo_auth/models.py -> /var/www/msgapp/backend/lib/python2.7/site-packages/mongoengine/django/mongo_auth
    
    copying build/lib.linux-x86_64-2.7/mongoengine/django/shortcuts.py -> /var/www/msgapp/backend/lib/python2.7/site-packages/mongoengine/django
    
    copying build/lib.linux-x86_64-2.7/mongoengine/django/sessions.py -> /var/www/msgapp/backend/lib/python2.7/site-packages/mongoengine/django
    
    copying build/lib.linux-x86_64-2.7/mongoengine/django/tests.py -> /var/www/msgapp/backend/lib/python2.7/site-packages/mongoengine/django
    
    copying build/lib.linux-x86_64-2.7/mongoengine/django/storage.py -> /var/www/msgapp/backend/lib/python2.7/site-packages/mongoengine/django
    
    copying build/lib.linux-x86_64-2.7/mongoengine/django/__init__.py -> /var/www/msgapp/backend/lib/python2.7/site-packages/mongoengine/django
    
    copying build/lib.linux-x86_64-2.7/mongoengine/django/auth.py -> /var/www/msgapp/backend/lib/python2.7/site-packages/mongoengine/django
    
    copying build/lib.linux-x86_64-2.7/mongoengine/django/utils.py -> /var/www/msgapp/backend/lib/python2.7/site-packages/mongoengine/django
    
    copying build/lib.linux-x86_64-2.7/mongoengine/common.py -> /var/www/msgapp/backend/lib/python2.7/site-packages/mongoengine
    
    copying build/lib.linux-x86_64-2.7/mongoengine/document.py -> /var/www/msgapp/backend/lib/python2.7/site-packages/mongoengine
    
    copying build/lib.linux-x86_64-2.7/mongoengine/queryset/queryset.py -> /var/www/msgapp/backend/lib/python2.7/site-packages/mongoengine/queryset
    
    copying build/lib.linux-x86_64-2.7/mongoengine/queryset/base.py -> /var/www/msgapp/backend/lib/python2.7/site-packages/mongoengine/queryset
    
    copying build/lib.linux-x86_64-2.7/mongoengine/queryset/visitor.py -> /var/www/msgapp/backend/lib/python2.7/site-packages/mongoengine/queryset
    
    copying build/lib.linux-x86_64-2.7/mongoengine/queryset/__init__.py -> /var/www/msgapp/backend/lib/python2.7/site-packages/mongoengine/queryset
    
    copying build/lib.linux-x86_64-2.7/mongoengine/queryset/field_list.py -> /var/www/msgapp/backend/lib/python2.7/site-packages/mongoengine/queryset
    
    copying build/lib.linux-x86_64-2.7/mongoengine/queryset/transform.py -> /var/www/msgapp/backend/lib/python2.7/site-packages/mongoengine/queryset
    
    copying build/lib.linux-x86_64-2.7/mongoengine/queryset/manager.py -> /var/www/msgapp/backend/lib/python2.7/site-packages/mongoengine/queryset
    
    copying build/lib.linux-x86_64-2.7/mongoengine/context_managers.py -> /var/www/msgapp/backend/lib/python2.7/site-packages/mongoengine
    
    copying build/lib.linux-x86_64-2.7/mongoengine/__init__.py -> /var/www/msgapp/backend/lib/python2.7/site-packages/mongoengine
    
    copying build/lib.linux-x86_64-2.7/mongoengine/connection.py -> /var/www/msgapp/backend/lib/python2.7/site-packages/mongoengine
    
    copying build/lib.linux-x86_64-2.7/mongoengine/errors.py -> /var/www/msgapp/backend/lib/python2.7/site-packages/mongoengine
    
    copying build/lib.linux-x86_64-2.7/mongoengine/dereference.py -> /var/www/msgapp/backend/lib/python2.7/site-packages/mongoengine
    
    copying build/lib.linux-x86_64-2.7/mongoengine/python_support.py -> /var/www/msgapp/backend/lib/python2.7/site-packages/mongoengine
    
    copying build/lib.linux-x86_64-2.7/mongoengine/signals.py -> /var/www/msgapp/backend/lib/python2.7/site-packages/mongoengine
    
    copying build/lib.linux-x86_64-2.7/mongoengine/fields.py -> /var/www/msgapp/backend/lib/python2.7/site-packages/mongoengine
    
    copying build/lib.linux-x86_64-2.7/bson/json_util.py -> /var/www/msgapp/backend/lib/python2.7/site-packages/bson
    
    error: could not delete '/var/www/msgapp/backend/lib/python2.7/site-packages/bson/json_util.py': Permission denied
    
    ----------------------------------------
    Cleaning up...
    Command /var/www/msgapp/backend/bin/python2.7 -c "import setuptools, tokenize;__file__='/var/www/msgapp/backend/build/mongoengine/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-811vit-record/install-record.txt --single-version-externally-managed --compile --install-headers /var/www/msgapp/backend/include/site/python2.7 failed with error code 1 in /var/www/msgapp/backend/build/mongoengine
    Storing debug log for failure in /home/www/.pip/pip.log
    

Would be nice to find out how to install it inside virtualenv.


Answer: The error message says that installer tries to delete file
**/var/www/msgapp/backend/lib/python2.7/site-packages/bson/json_util.py** but
it fails because you don't have permissions.  
There are two possible reasons:

  1. You are trying to install mongoengine as a different user from the one who own the virtualenv.  
Let's assume that you are logged in as user `holms` so your bash prompt looks
like: _[holms@localhost ~]$_  
Now check ownership of virtualenv with:  
`ls -la /var/www/msgapp/ | grep backend`  
If the output should look like:  
_drwxr-xr-x. 9 holms holms 4096 05-06 15:49 backend_  
If instead of _holms_ you get for example _bruce_ then virtualenv is owned by
this user and you should perform installation as _bruce_ :  
`sudo su - bruce`  
`source /var/www/msgapp/backend/bin/activate`  
`pip install mongoengine`

  2. You are logged in as the correct user but **/var/www/msgapp/backend/lib/python2.7/site-packages/bson/json_util.py** is owned by someone else. Again I'm assuming that your username is _holms_.  
Check permissions:  
`ls -la /var/www/msgapp/backend/lib/python2.7/site-packages/bson/ | grep json_util.py`  
If you see that someone else e.g. _bruce_ owns this specific file then change
the ownerschip:  
`sudo chown holms:holms /var/www/msgapp/backend/lib/python2.7/site-
packages/bson/`  
Now you should be able to install mongoengine.

