Installing gevent inside a virtual environment on OSX

Installing Python’s gevent package can be a bit challanging. This blog post explains how to install it on OSX v10.8 or later without using something like ‘MacPorts’.

You will need to get XCode from Apple’s App Store and make sure to install the Command Line Tools. You do this under Preferences>Downloads (Using XCode 5 you don’t have to do that anymore, apparently).

Installing virtualenv can be done using easy_install (or pip if available):

$ easy_install virtualenv

Here’s how you can use virtualenv:

$ mkdir MyExample
$ cd MyExample
$ virtualenv ENV
$ source ENV/bin/activate
(ENV)$

The virtualenv package comes with pip, so we’ll use this instead of easy_install.

We need to install the C-library libevent. Download the latest version and do the following while in the virtual environment:

(ENV)$ curl -L -O https://github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz
(ENV)$ tar xzf libevent-2.0.21-stable.tar.gz
(ENV)$ cd libevent-2.0.21-stable
(ENV)$ ./configure --prefix="$VIRTUAL_ENV"
(ENV)$ make && make install
(ENV)$ cd $VIRTUAL_ENV/..

If all went well, install the Pyton packages greenlet and gevent. Note that the order is important.

(ENV)$ pip install greenlet
(ENV)$ pip install gevent --global-option "-L$VIRTUAL_ENV/lib" \
    --global-option "-I$VIRTUAL_ENV/include"

The order is important because gevent depends on greenlet. If you install gevent first, the extra global options will not work and you’ll get an error.

Try to import gevent and if all went well, no error should raise:

$ python
>>> import gevent

Using OSX ‘Mavericks’ v10.9-beta you need XCode v5 Developer Preview to be able to compile libevent.

Comments

Damjan

gevent 1.0 is in a almost-released state and is highly preferred over the 0.3.x version that you get with pip. And it also requires libev which it has bundled, so no need for libevent.

I just do:

pip install --user git+https://github.com/surfly/gevent.git

and it installs it in ~/.local/lib/…/…

allen.bang
thanks, it works. your article help me a lot!