Installing Python 3.0 on Ubuntu

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 http://www.python.org/download/ – 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.

3 Responses to Installing Python 3.0 on Ubuntu

  1. Kris Grant says:

    Cool Thanks for this post. I am a newbie at django and this will be a big help.

  2. Denis says:

    Thanks a lot for this tutorial, it was my first install NOT using the GUI (or apt-get) and I hadn’t a clue about how to handle a “tarball install” (well, a tar ball is supposed to be sticky, right ?) and it went swimmingly.

    Now 5 minutes later, I open a console, type “python3” and it just greets me like so :

    Python 3.1.2 (r312:79147, Jun 21 2010, 22:37:35)
    [GCC 4.2.4 (Ubuntu 4.2.4-1ubuntu4)] on linux2
    Type “help”, “copyright”, “credits” or “license” for more information.

    >>>

    All I have left to do now is, well, learn python (;)).

    I wish all install tuto would be as clear as yours

    Cheers,

    Denis (Linux Newbie, obviously)

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.