Install Python3 and Postgres (OS X El Capitan) via Homebrew

Another Post on Installing Python3 and Postgres…?

I’ve already written a tutorial on how to set up a new Mac for Python-Django development with PostgreSQL as the database engine; however, that tutorial used the Python3 installer downloaded from the Python website. I recently got a new Mac, and I wanted to take this opportunity to learn how to set up my environment using other tools. Specifically, I wanted to download Python3 through Homebrew, a popular package manager for macOS.

Installing Python3 through Homebrew has its advantages, as many articles have stated. See the Further Reading section for a couple of those articles.

I thought about just updating my previous blog post, but a coworker suggested that I leave the old one, in case someone wants a reference for how to get a Mac up and running with the Python installer. So, here’s a whole new post for using homebrew to install several of the packages necessary for Python/Django development.

Prerequisites

Before we get started, make sure you have disabled SIP, macOS El Capitan’s Rootless security feature. If you don’t know how to do this, see my other blog entry on how to do just that.

You’ll also want to install Xcode and all of the command line tools that Xcode can offer.

Install HomeBrew

First off, we need to install HomeBrew. To do this, run the following:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Install Python3 via HomeBrew

To install the most recent version of Python3 via Homebrew, run the following in the terminal:

brew install python3

If you want to install a specific version of Python3 (i.e., Python3.4), Homebrew makes it a bit more difficult. Here’s an article on how to go about doing something like that.

Things to Note After Install

There are a couple of things to note after your install of Python3. Mainly, if you’re used to the Python installer, Homebrew installs all of the Python files in different directories. It would be of benefit to become familiar with where your Python files have been installed.

Also, if ever you need to uninstall anything that you used Homebrew to install, you can run the following command:

brew uninstall package_name

If you’re comparing this tutorial with my other tutorial using the Python Installer, you’ll also note that I overwrite the symlinks for python, pythonw, pydoc, and python-config, as well as pip. This was strictly for the ability to say python in the terminal and invoke python3 instead. After writing that tutorial, I found that I never really needed that step, and therefore found it to be useless. So, we won’t be adding any symlinks here.

Get Your Virtual Environment Set Up (Optional but Recommended)

This section of the setup, though highly recommended, is optional. Virtual environments are a really great way to keep your machine clean and organized (concerning libraries/modules downloaded), and the most effective way to be able to run multiple python projects with different package version needs on one computer.

This section of the setup, though highly recommended, is optional. Virtual environments are a really great way to keep your machine clean and organized (concerning libraries/modules downloaded), and the most effective way to be able to run multiple python projects with different package version needs on one computer.

Note: If you’re interested in using virtual environments, you will need to have a project directory already planned for whatever project you’ll be working on.

Since brew-installing Python3 includes the pip3 package for virtualenv, we don’t need to worry about installing the package. Instead, we’ll hop right into creating our virtual environment, and move on from there.

Navigate from the terminal into the directory directly above the project directory where you’ll be working, and create your virtual environment. An important step here is to include the path of the Python interpreter that you would like to use.

Note: I say to create your virtual environment in the directory above your project directory only to avoid adding your virtualenv to your version tracking for the project (if you’re setting up your project with git, or another version tracking software).

cd PATH_TO_DIRECTORY_ABOVE_PROJECT
virtualenv -p /usr/local/bin/python3 NAME_OF_VIRTUAL_ENVIRONMENT

At this point, you can activate your virtual environment and continue on.

source NAME_OF_VIRTUAL_ENVIRONMENT/bin/activate

Note: In your virtual environment, the “pip” command refers to “pip3”. Because of this, you’ll see throughout the rest of this (and the previous) tutorial that whenever I install a package via pip3, I only use “pip”. Be aware of this in case you’re using this tutorial without using virutal environments.

Setting Up PostgreSQL

Now that python and pip are installed, it’s time to set up your database. I’m a big fan on Postgres as my database for django(-mako-plus) development, so here are the steps to getting your machine rolling with PostgreSQL.

As with Python3, I’ll be using Homebrew to install Postgres, only I’ll be installing Postgres9.4. To do this, run the following:

brew install homebrew/versions/postgresql94

This should install and correctly symlink all necessary binaries for Postgres9.4. Next, start Postgres with the following:

postgres -D /usr/local/var/postgres

IN A NEW TERMINAL TAB, create a new database, and attempt to enter the psql prompt:

createdb testing
psql -d testing

If you’re able to get into the db, then you’re all set! Exit the psql prompt by entering the following into the terminal:

\q

Install AdminPack (Optional but Recommended)

If you’re interested in ever using PgAdmin (a great GUI for interacting with Postgres), it’s recommended that you install the PSQL extension “AdminPack”. Do that with the following:

psql postgres -c 'CREATE EXTENSION "adminpack";'

Configure Postgres to Start Automatically

Finally, we’ll want Postgres to be running automatically each time we login or restart our computer. Do so with the following:

mkdir -p ~/Library/LaunchAgents
ln -sfv /usr/local/opt/postgresql94/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql94.plist

You can test that this worked by restarting your machine, then attempting to enter your “testing” database when your machine has restarted. If it didn’t work, then turn to Google.

Set Up Psycopg2

Here is where the real beauty of installing with Homebrew comes to fruition. If you consult my other tutorial which uses the Python and Postgres installers, importing Psycopg2 can be a real pain. Usually you’ll need to go edit or create some symlinks to get it all to work. However, at the time of this writing, I was able pip-install Psycopg2 and successfully import it in a Python prompt without a hitch.

To do this, re-enter your virtual environment, and run the following:

pip install psycopg2
python

Now from the python prompt:

import psycopg2

If you don’t get any errors, then you’re all set!

If for whatever reason you do run into errors, then check my other tutorial in the Setting Up Psycopg2 section to troubleshoot.

Other Tidbits

Here are some other little tidbits of advice that might come in handy as you’re dealing with PostgreSQL on macOS.

Move Database (Data Directory) to a Different Location

In case you’re like me, and realize that you need more space on your machine, you might come across a time where you need to move an existing DB from one location/drive to another. Here’s how you go about doing that.

First, start by getting your drive mounted and formatted correctly. Then locate your data directory. To do this, log into the psql terminal as a superuser (this should be your normal macOS user) and show the data directory:

psql
SHOW data_directory;

This should print out something like this:

/usr/local/var/postgres

That’s the location of the database files the Postgres binaries read and store data to.

Next, stop Postgres and move that directory to the desired location.

launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql94.plist
mv /usr/local/var/postgres /Volumes/NiftyDrive/data

Now create a symbolic link from the original location (inside of /usr/local/var/ to the new location, and restart Postgres

ln -s /Volumes/NiftyDrive/data /usr/local/var/postgres
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql94.plist

Wrapping Up

At this point, you’re all set to continue installing any other pip packages or modules that you need, and move forward with your project! Congratulations! You just installed everything with Homebrew!

Further Reading

2018-03-12T02:23:45+00:00 June 25th, 2016|
css.php