Installing psycopg2 on OS X

I have recently been doing some python development that uses the psycopg2 driver for PostgreSQL. On Linux, this worked wonderfully, and everything was installed using the regular easy_install method with no troubles. However on OS X it wasn’t quite so easy.

First I tried using easy_install but it failed with a long list of gcc warnings and errors. Looking near the beginning of the errors I saw:
Warning: /bin/sh: pg_config: command not found followed by
./psycopg/connection.h:27:22: error: libpq-fe.h: No such file or directory Well this makes sense as I’m using PostgreSQL Plus for OS X, and it doesn’t put it’s header files or libraries in a standard place. There is an easy workaround though.

Thanks to Luke Tucker who provided this easy solution: PATH=$PATH:/Library/PostgresPlus/8.3/bin/ sudo easy_install psycopg2

Original Method

This is left here for historical purposes. I recommend using the above command instead.
We’ll need to download the psycopg2 package and install it manually. Don’t worry, this is very easy and doesn’t require much work at all.

  1. Open you favorite terminal application.
  2. Download the package. Extract the package, enter the directory.
    curl -o psycopg2.tar.gz http://www.initd.org/pub/software/psycopg/psycopg2-latest.tar.gz
    tar xzf psycopg2.tar.gz
    cd psycopg2-${version}
  3. Edit the setup configuration file. Use whatever editor you’re comfortable with.
    vim setup.cfg
  4. Uncomment the include_dirs line and change it to read:
    include_dirs=/Library/PostgresPlus/8.3/include
  5. Uncomment the library_dirs line and change it to read:
    library_dirs=/Library/PostgresPlus/8.3/lib
  6. Save and quit your editor.
  7. Finally, build and install the package.
    sudo python setup.py install

And now you should have a working PostgreSQL driver for Python. This was very useful for me since I like to use SQLAlchemy for my database work in Python and it uses psycopg2.

Alternative

As a alternative method, you can replace steps 4 and 5 above with:

  • Uncomment pg_config line and change it to read:
    pg_config=/Library/PostgresPlus/8.3/bin/pg_config

Thanks, pacopablo!

Running rTorrent on OS X

This guide is old and probably outdated. I used these steps to compile and install rTorrent 0.7.4/libTorrent 0.11.4

So, you’ve come to the realization that most of the OS X BitTorrent clients aren’t up to par with those offered on other platforms. Until today, I had been using the standard BitTorrent client but it left me wanting more, especially a “watch” folder. A watch folder is a folder where I can throw my torrents and know that my client will automatically pick them up and download them. Anyways, I was pointed to rTorrent today, and thought I’d write a little guide on getting it up and running on OS X, since I was unable to find something quickly with a Google search. Here is step by step what I did to get rTorrent working for me.

Install XCode

XCode is a set of development tools provided by Apple. They can be obtained by anyone at the Apple Developer Connection. You will have to sign up for an account, but you can do so for free. Download the latest version of XCode (2.4.1 at the time of writing this), mount it and run the installer. This should be a familiar process for most Mac users. As a warning the XCode dmg is almost 1GB.

Install/Update DarwinPorts

OS X doesn’t really have a package manager, and resolving and installing dependencies manually is no fun. This is what DarwinPorts is for. It is based on the BSD package manager and is pretty slick. If you don’t like DarwinPorts, you may want to consider Fink, although they didn’t seem to have rTorrent marked as stable when I looked.

Assuming you choose DarwinPorts, download the Universal Binary from their main page, mount it, and run the installer that it contains. Then you’re going to need to add one line to your ~/.bashrc file so that port will be in your execution search path. You can do this with one command in a terminal. Open up Terminal.app (In /Applications/Utilities or just use QuickSilver) and copy and paste this line into it and press enter. echo "export PATH=\$PATH:/opt/local/bin" >> ~/.bashrc If you’re using zShell then you can use the same thing in your ~/.zshrc, but if you’re using zShell you probably already know what to do.

If you already had DarwinPorts installed, you should probably run port selfupdate to upgrade it to the latest version (you’ll need to sudo to do this).

After you’ve installed or updated your copy of DarwinPorts, you need to update the package list so that you can get the latest stable version of rTorrent. You can achieve this with the port sync command.

Congratulations, you should now have a working copy of DarwinPorts.

Install rTorrent

rTorrent and all of it’s dependencies can be installed with one simple command. sudo port install rtorrent When sudo prompts you for a password, just enter your local account password. That’s all there is to installing rTorrent. It may take 15-20 minutes depending on your computer.

Configure rTorrent

Like many *nix applications, rTorrent uses a configuration file located in your home directory to decide how to behave. I’m not going to go in-depth and explain every option to you, as I have left comments in the file to do that. I have changed very little from the official documentation’s example config, so you may wish to just start from theirs instead. Anyways, here’s the link to my config file, just save it to your home directory and make any changes that you feel are necessary.

If you’re going to use my config, make sure you have these directories created. ~/torrents/files
~/torrents/session
~/torrents/watch

Start rTorrent

Open up Terminal.app and run the command rtorrent and you should be presented with a mostly blank screen that says rTorrent and the version at the top.

Add Your Torrents

Now, all you should have to do is drop your torrents in the watch directory you specified in the config file, and it should automatically pick them up and start downloading. If you chose not to use a watch directory, you can add a torrent by hitting the return key and typing in the name of the file. It has tab completion and I recommend taking advantage of it.

Learn the Commands

I don’t know them all yet, but I’m picking them up quickly. I’m not going to list them all here because the rTorrent devs already took care of it over here. A notable one though, is Ctrl-q for quit.

Encryption

More and more ISPs and campuses have started to throttle or completely disable bittorrent. Encryption can sometimes help you workaround their restrictions and let you utilize all of that precious bandwidth. Look in the config file you downloaded from earlier in this guide. On line 83, you’ll see an option “encryption” that has been commented out. If you keep this line commented out, you will never use an encrypted connection. But, there are other users out there that will only accept an encrypted connection. So, we want to change this. If you uncomment the line as it is, it will allow you to accept an encrypted connection but will prefer unencrypted if that’s possible. This will enable you to connect to any body, whether they use encryption or not. But, we probably want encrypted to be the default, so let’s change it a bit. encryption = allow_incoming,try_outgoing,enable_retry This line will initiate and accept encrypted connections but will fallback to plaintext if the other user is not using encryption. This is my preferred method, as it will allow me to connect to as many people as possible, but will use encryption where available. encryption = enable_retry,require
or
encryption = enable_retry,require_rc4
These two options (as I understand it) will require an encrypted connection, meaning no unencrypted connections will ever occur. The difference between require and require_rc4 is how much of the data it encrypts. Using require will only encrypt the header information, which will sometimes be enough to get around the your ISP’s throttling/blocking. The require_rc4 option will encrypt the entire stream, all the data that ever gets sent between the two clients. This is much more difficult, although not impossible, for a traffic shaper to detect. Note that require_rc4 will be much more processor intensive, so you may notice some performance differences.

Other Things to Consider

  • Run rTorrent in a screen session. You can learn more about GNU screen here.
  • Use Visor for you rTorrent session. Visor provides a quake-like console for OS X using Terminal.app.
  • I don’t know everything. If I have a typo, mistake or just plain say something that’s incorrect, please leave a comment so I can fix it.