Installing Python 2.7 on Ubuntu

October 10th, 2011 at 8:48 pm

This week I upgraded the main installation of Python on my Ubuntu 10.04 machines to version 2.7. Here’s a short documentation of this process.

Step 1: Prerequisites

The first step in Python’s installation is running a configure script which snoops around your system, looking for packages that it needs to build various capabilities and extensions with. Having these packages installed before running configure makes sure it finds them.

Here are some packages that have to be installed to have various aspects of Python functioning:

sudo apt-get install libreadline-dev
sudo apt-get install libsqlite3-dev
sudo apt-get install libbz2-dev
sudo apt-get install libssl-dev

Step 2: Download and build Python

Go to http://www.python.org/. In the "Quick links" section on the left-hand side of the page, "Source distribution" is a direct link to the tarball. Download it. Unzip the tarball, and from the root of the created directory (which will be called Python-2.7.2 or something similar, depending on the version):

./configure
make -j

I found that the default configure settings work fine for Ubuntu 10.04 and there’s no real need to specify extra --with flags.

You can now check that Python was correctly built by executing ./python and falling into its interactive terminal. If you want, you can also execute the Python test-suite with make test, though it may take a long time to run (~10 minutes on a relatively fast machine).

Step 3: Install

In the same directory, run:

sudo make install

This installs Python into /usr/local/bin. Depending on the configuration of your system, you may want to add symlinks to the newly created /usr/local/bin/python2.7 in /usr/bin/ as well.

That’s it, you now have Python 2.7 installed.

Step 4: Install some essential first modules

Python has a powerful packaging & installation machinery for its modules, but it doesn’t come pre-installed with Python itself.

So it’s a good idea to install setuptools (or distribute), followed by pip.

From now, pip can be used to install other Python modules very conveniently. For example, all you need to have the IPython shell installed is:

sudo pip install ipython

Related posts:

  1. Installing Python 2.5 on Bluehost
  2. Some problems installing Ubuntu 10.04 on VirtualBox
  3. Some insights on Linux on the EEE
  4. success in battle against activeperl/minicpan
  5. rant about weather and perl

11 Responses to “Installing Python 2.7 on Ubuntu”

  1. Ram RachumNo Gravatar Says:

    Why are you recommending setuptools instead of distribute?

  2. elibenNo Gravatar Says:

    Ram,

    No particular reason – since I just wanted to get to pip which is then used to install other modules. pip requires either setuptools or distribute. I will update the post to reflect that I don’t have a preference.

  3. Petri LehtinenNo Gravatar Says:

    There’s also a PPA that has a .deb for 2.7 (among other versions) for Lucid:

    https://launchpad.net/~fkrull/+archive/deadsnakes?field.series_filter=lucid

  4. elibenNo Gravatar Says:

    Petri,

    Ah, thanks! I was actually looking for a .deb and couldn’t find any. This may be an easier way to install, although I’m not sure if it installs all the per-requisites.

  5. JohnNo Gravatar Says:

    Aside from distribute and pip, I also like to install virtualenv right off the bat.

  6. Ram RachumNo Gravatar Says:

    If distribute doesn’t cause you any problems, why even mention setuptools? It’s gonna take years to move all the systems in the world from setuptools to distribute, and blog posts suggesting to install setuptools just stall that process.

    If there was some reason, however tiny, to prefer setuptools over distribute (like there are some small reasons to prefer Python 2.6 over 2.7) then I’d say “okay, fine, if setuptools is needed for your systems, then use that, just for the love of god fix your system sometime soon so it works with distribute” but as far as I know, there is no such case, so I see no reason in the world to suggest installing setuptools to anyone.

  7. elibenNo Gravatar Says:

    John,

    I like having virtualenv handy as well, but didn’t want to turn this post into “my favorite Python modules”. Just to cover the bare essentials.

  8. Helen NeelyNo Gravatar Says:

    Rather than having to download Python from the website, isn’t there a way to run a command on Linux that downloads and upgrade it for you? I’m worried because I need to upgrade mine.

  9. elibenNo Gravatar Says:

    Helen,

    There’s sudo apt-get install python for Ubuntu, but it may not have 2.7 for your Ubuntu versions. You can look for repositories online, but the steps outlined above should work for pretty much every version of Ubuntu.

  10. dunithdNo Gravatar Says:

    This worked for me! Thanks.

  11. elibenNo Gravatar Says:

    Re setuptools vs. distribute – the two projects are going to be merged (announced during PyCon 2013) into setuptools, so I believe my mentioning setuptools here won’t harm any kittens in the long run.

Leave a Reply

To post code with preserved formatting, enclose it in `backticks` (even multiple lines)