20 July 2013

Install Python, Virtualenv, Virtualenvwrapper on OS X using Homebrew: The world's easiest guide

The world's simplest instructions in the world for installing Python, virtualenv, virtualenvwrapper on OS X using Homebrew for Apache and MySQL.

1) Apache: Use OS X's Apache installation. To run and test apache :
sudo apachectl start
sudo apachectl stop

sudo apachectl restart

2) Bashrc/Bash_Profile: If you don't have ~/.bashrc or ~/.bash_profile create them using.
touch ~/.bashrc
touch ~/.bash_profile

(unlike other terminal apps in the Linux/Unix world OS X terminal.app runs .bash_profile every time you open a new terminal window. See this excellent piece on the topic. We will come back to these files in a minute)

3) Installing Homebrew:
ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"

Then run:
brew doctor

4) Install Python. OS X comes with Python, but you want to install the latest 2.x version using Homebrew.

brew install python

5) Upgrade the distribution:

pip install --upgrade distribute

6) Upgrade install pip:

pip install --upgrade pip

7) Install virtualenv:
pip install virtualenv

8) Install virtualenvwrapper:

pip install virtualenvwrapper

9) Install MySQL:

brew install mysql

(to start/stop or restart MySQL type: mysql.server start/stop/restart)


10) Install MySQL-python, needed for python to talk to MySQL:

pip install MySQL-python

11) Your Path:

sudo nano /etc/paths

make sure /usr/local/bin comes before /usr/bin. Mine looks like this:

/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin

12) Environment Variables: Set your .bashrc and .bash_profile to include these entries respectively:

export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python
export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh

(this is for .bashrc)

source ~/.bashrc

(This is for .bash_profile. Basically, .bash_profile runs every time you create a terminal window and it calls .bashrc which in os x only runs when you log in to a terminal window)

also run:

source ~/.bash_profile
source ~/.bashrc

(to execute these changes)

13) Get started. Some basic commands:
Create a new project:
mkvirtualenv MyProject (the project files are in ~/.virtualenvs)
Work on the project:
workon MyProject
Install stuff:
pip install django
Get out of there:
deactivate
Remove it:
rmvirtualenv MyProject

It's that simple! Enjoy. (Thanx Josh)

2 comments:

Unknown said...

Thank you very much. This one worked! On Mavericks. mZoo.org

Unknown said...

Actually - I would love to read a more clear explanation of the relationship between .bashrc and .bash_profile - if you have one.