Forcing Homebrew and Macports to coexist?

You can tell Homebrew to use its own directory, and then add that to the path. Although not for the same purpose, this excerpt from the installation guide tells it:

Multiple installations

Create a Homebrew installation wherever you extract the tarball. Whichever brew command is called is where the packages will be installed. You can use this as you see fit, e.g. a system set of libs in /usr/local and tweaked formulae for development in ~/homebrew

Source: https://github.com/mxcl/homebrew/wiki/installation


My way to force them coexist is to make MacPorts not visible by default, but visible when invoking any MacPorts programs. That is, wrap MacPorts programs with some script like:

if [ "$#" -le 0 ]; then
  echo "Usage: $0 command [arg1, arg2, ...]" >&2
  exit 1
fi
if [[ -z $MACPORTS_PREFIX ]]; then
  MACPORTS_PREFIX='/opt/local'
fi
export PATH="$MACPORTS_PREFIX/bin:$MACPORTS_PREFIX/sbin:$PATH"
export DYLD_LIBRARY_PATH="$MACPORTS_PREFIX/lib:$DYLD_LIBRARY_PATH"
export CPATH="$MACPORTS_PREFIX/include:$CPATH"
command=$1
shift
exec $command $* 

If you name this script as macports.sh, you can do the wrapping by macports.sh macports_bin, such as macports.sh port will run port wrapped.

For convenience, you can put macports.sh something into some scripts with the same name as the commands themselves and put them in your HOME directory, such as ~/bin, ~/.local/bin, etc.

I've written a blog post about this one month ago. You can read it if you need a look into the details.