Running Node, Python, or Ruby on OS X (like a boss)
Why are we doing this?
Node, Python, and Ruby are handy programming languages / development environments but getting them set up to run in the correct environment can be a little tricky. Certain versions of these languages / environments work with some projects and not with others and releases can break backwards compatibility. To address this, there are tools that let you switch between different versions of the language and install different sets of packages and libraries. The three tools I’ll talk about today are Node Version Manager (NVM), Ruby Version Manager (RVM) for Ruby and Virtual Environment (virtualenv) for Python. After setting up either you will have support for package/library management and can easily configure your machine to target versions appropriate to your code. I’ll also be focusing on OSX as the target operating system because I had a whole lot less trouble getting things configured on Linux and Windows machines. Let’s get started with setting up Ruby.
Setting up Node on OSX with NVM
OSX does not ship with Node so NVM is a great way to get started. As described on the Creationix/nvm project, you can use curl or wget to automatically install the manager:
To install you could use the install script using cURL:curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bash
or wget:wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bash
The script clones the nvm repository to
~/.nvmand adds the source line to your profile (
Once installed, you can install a few environments:
nvm install 0.10 nvm install 0.11
… and can then switch between Node versions:
nvm use 0.11 nvm use 0.10
Setting up Ruby on OSX with RVM
OSX does not ship with Ruby, so even if you weren’t installing RVM, you would need to download and setup something. For starters, you can grab and install the latest version of RVM by running the following command:
\curl -L https://get.rvm.io | bash -s stable –ruby
After running this command, RVM will download and install. You will next need to add the rvm scripts to your shell prompt:
echo "source $HOME/.rvm/scripts/rvm" >> ~/.bash_profile
After you re-source your bash configuration (source ~/.bash_profile), you should have RVM going. Just having RVM is not quite enough, you will need to acquire a version of Ruby now.
This is typically where you will run into a number of issues with OSX. The compiler that ships with OSX does not use standard flags and so you need to give RVM a hint when it performs builds. The following command, which I took from this blog post elsewhere, seems to do the trick:
rvm install <version> --reconfigure --debug -C --enable-pthread --with-gcc=clang
For example, if I wanted to install Ruby version 1.8.7, a legacy version as of today, the following command would do it:
rvm install 1.8.7 --reconfigure --debug -C --enable-pthread --with-gcc=clang
Now that you have setup your RVM client, it’s a good time to test it. You can switch between installed versions of Ruby with the rvm command:
rvm 1.9.3 ruby -v rvm 1.8.7 ruby -v
You can now install gems because RVM ships with bundler:
gem install bundler gem install haml
If everything is working, you are all set and can run Ruby like a boss.
Setting up Python on OSX with virtualenv
As explained on the Python web site, “Python comes pre-installed on Mac OS X, but due to Apple’s release cycle, it’s often one or even two years old.” This is good news and bad news, the good news is that if you have a Mac, you have Python already; the bad news is that the version of Python you’re running is probably incompatible with a lot of code. Additionally, installing libraries will do so globally and will require administrative permissions. To address this, let’s install virtualenv which will let us use multiple versions of Python as well as install packages and dependencies locally.
The documentation on the virtualenv site suggests installing using pip but unfortunately OSX does not ship with this tool. Instead, let’s build from sources!
- Download the latest stable version of the virtualenv sources, for example:
curl -O https://pypi.python.org/packages/source/v/virtualenv/virtualenv-1.8.4.tar.gz
- Unpack the sources:
tar -xzvf virtualenv-1.8.4.tar.gz
sudo python setup.py install
Now you have virtualenv, (that was easy!), let’s setup an isolated environment with a few specifics configured…
This creates a copy of python and pip, both locally scoped to the folder classy. Change to the folder classy and switch to this environment:
cd classy source bin/activate
This will make some small changes to your shell prompt and will configure your Python settings so that they are scoped to that folder. If you have multiple versions of Python setup on your computer, you can switch between them within environments using the following command:
virtualenv -p /usr/bin/python2.6
You can also install locally scoped libraries using pip. For example, the following command will install Flask, a common minimalist web server:
pip install flask
If you’re working in Node, Ruby, or Python or are just starting, it is really handy to be aware of the available tools for setting up a flexible environment. Happy hacking!