Installing Python 3.0 on Ubuntu

January 14, 2009

In the course of updating a book for Python 3.0, I’ve had a lot of need to test and compare code in Python 3 and Python 2.6. The first is obvious, and the second is needed because Python 2.6’s -3 commandline switch is a  key step in the process of converting code from the 2.x world to Python 3.

Python 3 vs Python 2.6

In this post I’ll talk about installing Python 3.0 from source. Python 2.6 works much the same way except that you need to exercise some caution to make sure that 2.6 doesn’t replace 2.5 as the default Python. This is a bad thing because all the Python packages and libraries you install by way of Ubuntu packages are tied to the default Python 2.5 installed automatically. If 2.6 becomes your default Python interpreter, suddenly you lose all of those packages. Python 3.0 on the other hand will not set itself up as the default Python interpreter unless you explicitly tell it to.

The problem with Ubuntu

The problem with Ubuntu is that I’ve been running it for a few years and I’m spoiled. Using synaptic or apt-get to install software has become second nature to me, and it’s been a while since I installed any significant package from source. So when I needed the latest Python installed, my automatic reaction was to look for packages.

The bad news wasn’t unexpected – there are no Python 2.6 packages in the standard repo’s, which is really no surprise since 2.6 wasn’t released until after Intrepid. However, there were packages for Python 3. Beta and RC versions, to be sure, but there were packages. The problem was that while the base Python 3 package installs just fine, there seems to be no compatible Tkinter package. No Tkinter, no IDLE. Since the book I’m revising makes some mention of IDLE, this was a deal-breaker for me.

Back to the source

So out of necessity, I figured out how to make source installs of Python 3 and Python 2.6 (mostly) happy on Ubuntu 8.10. If you’re familiar with compiling from source on Linux/UNIX, just grab the packages listed below and get to it. OTOH, if you’re new to compiling, I’ve given more complete directions below…

General Prerequisites

In general, you need the following packages installed before you try to build Python from source:

  1. build-essential – the compiler and tools to build pretty much anything.
  2. libsqlite3-dev libreadline5-dev libncurses5-dev zlib1g-dev libbz2-dev libssl-dev libgdbm-dev and tk-dev – these packages (and their dependencies) are needed to build various Python libraries. Note that you need the “-dev” versions. Having libsqlite3 installed, for example, won’t be enough to build the Python libraries.

If you’re already at a terminal prompt the fast way to handle the above is:
doc@x60:~$ sudo apt-get install build-essential libsqlite3-dev libreadline5-dev libncurses5-dev zlib1g-dev libbz2-dev libssl-dev libgdbm-dev tk-dev

If you installed the Python 3 Ubuntu packages, you should uninstall them before going on.

Installing Python 3.0

  1. First you need to get the Python 3.0 source from – choose either of the compressed source tarballs and save to your home directory (or other convenient spot).(For the following steps you need to be in a terminal window)
  2. The next step is to unpack the tarball – switch to the folder where you saved the tarball and…

    doc@x60:~$ tar xvf Python-3.0.tgz
  3. The tarball should be unpacked into a folder called Python-3.0, so switch there…

    doc@x60:~$ cd Python-3.0
  4. There are 3 steps to compiling from source – configuring the package, compiling, and then installing. You configure the source package with the configure command;doc@x60:~/Python-3.0$ ./configure (the “./” is important here)

    This command checks your system and sets up the correct options for the build. It should end with a statement “Creating Makefile”. If there are errors instead, they must be fixed before you go any further.

  5. The compile step is pretty easy – you give the “make” command:

    doc@x60:~/Python-3.0$ make

    The make command will take some time, since it has to compile everything. Again, if the make ends with an error, you must fix the error to go on.

  6. The install step is the big one, since it actually puts  Python 3 on your system. By default Python 3.0 will not replace your existing Python, so it’s safe to go for it:

    doc@x60:~/Python-3.0$ sudo make install

    You need to use sudo because the install will try to place the files in spots where a normal user doesn’t have access – by default under /usr/local/.

After this, you should have python3.0 and idle3.0 available from the commandline. Creating menu entries,shortcuts, etc, I’ll leave as an exercise for the reader.


January 14, 2009

In the year and a half since I last posted here, I’ve posted occasionally on other sites, on other topics, but I’ve left this one alone. Now I’m thinking that it’s time to return to this site with some more Pythonic content. I’ve got a couple of reasons for this return…

A new project – The Quick Python Book, 2nd ed.

One reason to switch to more Pythonic posts is that I’ve just started working on a second edition of Manning Publication’s The Quick Python Book. Originally done in 2000 and based on Python 1.5, I’ve been  hired to revise it for Python 3.0. So far, it’s been interesting to see how stable the language has been, but it’s also made me see how the changes in 3.0 are definitely worth the break in compatibility. The more I work with the new print function, for example, the more I like it. It’s a trivial thing, I know, but I’m looking forward to a Python 3.0 future.

Python 3.0 in general

Python 3.0 in general is another reason to come back to this site. Not only is the change giving me a book gig, it’s creating a certain demand for information on how to manage that change. As I run across things that can help, I’m going to post them here.