Installing Python 2.7.2 in Ubuntu 11.04

Preface: In this example, $ is the prompt. So you don’t type the dollar sign. It’s just a sign that precedes “this is what I typed into my computer terminal”. These instructions are for beginners. Experts are welcome to chime in.

What’s nice about this guide is that because I specify instructions for a local installation, it can be applied to many types of machines (like compute clusters where you do not have root access).

Beginning the Installation

I just installed the Python 2.7.2 software package locally on my desktop. I’m running Ubuntu Natty Narwhal 11.04. I say “locally” because I’m installing it in my home directory, not system-wide. This is useful when you’re not root on your machine or do not have root authority.

Because Python is already installed on my machine, when I issue the command:

$ which python

to find the directory where Python is executed from, I get something like

/usr/bin/python

But I want to install Python in a new, local directory. So I created a directory.

$ mkdir ~/opt

Then I extracted my Python tar.gz file into this directory.

$ tar -xzvf Python-2.7.2.tgz

I went into this directory to poke around.

$ cd ~/opt/Python-2.7.2

Customizing Your Installation

I read the INSTALL.txt file for information about how to customize my installation. I found the options I want. In order to install Python locally, I specify my “prefix” during the configure step. Prefix is basically the location of the installation directory. Right now you’re in the source directory because that’s where the source code is located. I also want to specify my custom C compiler. I use the Intel compiler because it is pretty awesome. Its location on my machine is:
/opt/intel/composerxe-2011.4.191/bin/ia32/icc
Let’s not confuse the two different locations on my machine. Ubuntu has already set up an /opt directory. But I later created a /home/ben/opt directory because that’s what I wanted. You’ll notice I like to use the ‘~’ symbol to represent $HOME. You can find your value of $HOME by typing:

$ echo $HOME

Configure Python

Anyway, it’s time to configure Python. I had two goals: 1) Install it locally and 2) Specify my C compiler (so it doesn’t default to gcc). Experts know there are several ways of doing this without what I’m about to show you. This tutorial is not for experts. In your terminal you’ll need to be in the Python installation directory where you unpackaged your files to issue this command.

$ ./configure –prefix=~/opt/Python-2.7.2 CC=/opt/intel/composerxe-2011.4.191/bin/ia32/icc

Now your machine should configure Python. This may take up to a few minutes. The next step is exciting. Your version of Python will be compiled when you issue the next steps.

Python Make

$ make && make install

Now be aware that ‘make’ and ‘make install’ are two separate commands. I just used ‘&&’ to issue them on one line for convenience, so they execute back to back without my further instruction (because ‘make’ and ‘make install’ can sometimes take a while).

*NOTE* If you previously attempted this step and screwed up, or wrote something you didn’t want to in the configure step, you’ll have to reissue your ‘configure’ with the appropriate options and then:

$ make clean

before you continue with ‘make’ and ‘make install’.

Assigning Your PATH to Find Python

After you finish ‘make install’, Python should finish its compilation. Now I want to do a little organizing. I’d like to be able to execute Python by typing:

$ python

from a fresh terminal window. Right now, if I try to execute Python that way, it launches from /usr/local/bin as I mentioned earlier. This is not what I desire! We can change this by adjusting our path. We’ll go into the .bashrc file and edit with vim. Of course, you’re free to edit with whatever editor you like.

$ cd ~
$ vim .bashrc

Now once we’re viewing the file, get in insert mode and scroll down to assign your path. My path line looks like this after my edits:

PATH=”/home/ben/opt/Python-2.7.2/bin:$PATH”

I put my installation directory that I specified during prefix into my path. It’s represented by /home/ben/opt/Python-2.7.2/. But what’s ‘bin’ have to do with this (very few of you ask…but some may)?

Two important things to note. If I put $PATH at the beginning of my quoted statement, it wouldn’t have found my desired Python. That’s because your machine searches for the first result in your path. That would have been in /usr/local/bin (or whatever else you previously specified). So I had to drag $PATH to the rear of my line. Also, Python is not executed from the directory

~/opt/Python-2.7.2

but from

~/opt/Python-2.7.2/bin

The $INSTALL_DIR/bin is where the Python binary is located. That’s the typical location for binaries.

Final Steps

Now save your .bashrc file. Close your terminal window and open a new one. Type:

$ which python

It should be the location of the Python you just installed.

$ python -V

This will tell you the version of Python you’re running. Now just type ‘python’ to get started!

$ python

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s