Want to know more about us? Visit 2general.com »

How to install Node.js packages and Ruby gems in virtualenv

Virtualenv is a superb tool for creating isolated environments for development with Python. However, if your project requires libraries in other languages, you may want to install them in your virtual environment as well. Here’s how to do it with Node.js packages and Ruby gems.

If you’re using virtualenvwrapper (and you should), you can put this in your postactivate script. Otherwise make sure some other way that these variables are set:

# node.js packages
export npm_config_prefix=$VIRTUAL_ENV

# ruby gems
export GEM_PATH=""

Installing Node.js packages

As an example, to install the Less CSS compiler in the virtual environment, use:

$ npm install -g less

(Note the -g option!)

The lessc script will be conveniently symlinked into $VIRTUAL_ENV/bin:

$ echo "div { a { color: #f00; } }" > my.less
$ lessc my.less
div a {
  color: #f00;

The virtualenv trick requires a fairly recent version of Node.js. If you’re on an Ubuntu box, the easiest way to get one is the following (tested on 11.10 and 12.04):

$ sudo add-apt-repository ppa:chris-lea/node.js
$ sudo apt-get update
$ sudo apt-get install -y npm

Installing Ruby gems

Ruby gems are installed simply like this:

$ gem install sass

Just like with Node.js packages, scripts get installed into $VIRTUAL_ENV/bin:

$ echo "div { a { color: #f00; } }" > my.scss
$ sass my.scss
div a {
  color: #f00; }