How to install Python 2.7 and Python 3.3 on CentOS 6

This guide shows you how to install Python 2.7 and/or Python 3.3 on any version of CentOS 6. It also includes instructions for setuptools, pip, virtualenv and pyvenv.

In this guide I will show you how to install Python 2.7 and 3.3 on CentOS 6. The examples below are for Python 2.7.6 and Python 3.3.5, but the procedure is the same for any modern version of Python including the upcoming Python 3.4.0.

I make regular updates to this guide to track new versions. Please see the end of the document for a changelog.

CentOS 6 ships with Python 2.6.6 and several critical system utilities, for example yum, will break if the default Python interpreter is upgraded. The trick is to install new versions of Python in /usr/local (or some other non-standard location) so that they can live side-by-side with the system version.

This guide should work for all versions of CentOS 6, but I have only verified it on CentOS 6.5 64 bit. It will probably work for some versions of CentOS 5 also.

Execute all the commands below as root either by logging in as root or by using sudo.

Preparations – install prerequisites

In order to compile Python you must first install the development tools and a few extra libs. The extra libs are not strictly needed to compile Python but without them your new Python interpreter will be quite useless.

Things to consider

Before you compile and install Python there are a few things you should know and/or consider:

Unicode

Python has a long and complicated history when it comes to Unicode support. Unless you have very specific reasons you should configure Python 3.2 and earlier to enable UTF-32 support. This increases memory usage but improves compatibility. In Python 3.3 the Unicode support has been completely rewritten and strings are automatically stored using the most efficient encoding possible.

You enable UTF-32 in Python 2.7 by adding --enable-unicode=ucs4 to the configure command. In Python 3.2 the flag is called --with-wide-unicode.

Shared library

You should probably compile Python as a shared library. All modern Linux distros ship with Python compiled as a shared library, and there are third-party tools such as mod_wsgi and Blender that won’t work without it. If you compile Python as a shared library you must also tell it how to find the library. You have two options:

  • Compile the path into the executable by adding this to the end of the configure command: LDFLAGS="-Wl,-rpath /usr/local/lib"
  • Open the file /etc/ld.so.conf in a text editor and add the path /usr/local/lib to the end of it. After you have added the line you must run /sbin/ldconfig to make the dynamic linker aware of the change. This is how the file will look after adding the line on a clean install of CentOS 6.5:

Use “make altinstall” to prevent problems

It is critical that you use make altinstall when you install your custom version of Python. If you use the normal make install you will end up with two different versions of Python in the filesystem both named python. This can lead to problems that are very hard to diagnose.

Download, compile and install Python

Here are the commands to download, compile and install Python. If you modify /etc/ld.so.conf as discussed above you can remove the LDFLAGS parameter below.

After running the commands above your newly installed Python interpreter will be available as /usr/local/bin/python2.7 or /usr/local/bin/python3.3. The system version of Python 2.6.6 will continue to be available as /usr/bin/python, /usr/bin/python2 and /usr/bin/python2.6.

Download and install Setuptools + pip

Setuptools has replaced Distribute as the official package manager used for installing packages from the Python Package Index. Each Python interpreter on your system needs its own install of Setuptools. I also suggest you install pip. It builds on top of Setuptools and provides a few extra functions that are useful when you manage your packages.

The instructions below will install the latest version of Setuptools and pip for you.

The packages will end up in /usr/local/lib/pythonX.Y/site-packages/ (where X.Y is the Python version).

What’s next?

If you are using Python 2.7 I strongly recommend that you install virtualenv and learn how to use it. Virtualenv makes it possible to create isolated Python environments. If you are using Python 3.3 then you don’t need virtualenv because that functionality is already built in.

Each isolated Python environment (also called sandbox) can have its own Python version and packages. This is very useful when you work on multiple projects or on different versions of the same project.

Create your first isolated Python environment

When you use virtualenv to create a sandbox it will automatically install setuptools and pip for you inside the sandbox. If you use pyvenv then you must do it yourself. You can reuse the ez_setup.py file you downloaded earlier and just run it after you activate your new sandbox.

Changelog

2014-03-15

  • Examples updated with Python 3.3.5.

2014-02-16

  • The Python versions used in the examples have been updated to 2.7.6 and 3.3.4.
  • The list of library prerequisites has been extended so that more features are compiled into Python.
  • New parameters for compiling Python with a shared library and for enabling Unicode UTF-32 support in Python 2.7 and Python 3.2 have been added.
  • Instructions for installing and using setuptools, pip, virtualenv and pyvenv have been added/updated.

Long tail traffic

A few months ago I posted a step-by-step guide on how to install Python 2.7.3 on CentOS 6.2. The guide started out as a personal cheat-sheet, but it didn’t take much work to massage it into something I could post here.

A few months ago I posted a step-by-step guide on how to install Python 2.7.3 on CentOS 6.2. The guide started out as a personal cheat-sheet, but it didn’t take much work to massage it into something I could post here.

The only promotion of the guide I’ve done is to post a link to it from an answer on Stack Overflow. With only one useful article and almost no promotion this website is still getting 60+ unique visitors a day on weekdays.

Traffic since June
Traffic since June

 

I don’t understand why I get so many visitors, but much of the traffic comes from long tail (see below) organic search results on Google:

Traffic sources
Traffic sources

I like the idea of helping people and with numbers like these I feel compelled to put some more effort into this website. Expect to see more helpful articles in the future.

Ian Lurie defines long tail as:

Specific, niche search phrases, usually more than 2 words in length, that offer a low competition, low search volume and high searcher intent.

 

SOPA and domain transfer

To show how unhappy I am with GoDaddy supporting SOPA I have decided to vote with my wallet. I have initiated the transfer of this domain to Loopia, a Swedish registrar where I have several other domains.

When I bought this domain several years ago I bought it from GoDaddy. They have never been the best domain registrar, but they are a large and well-established company with really good prices.

As many of you already know they support SOPA. SOPA is short for Stop Online Piracy Act and is a proposed law that is supposed to help fight piracy. There are many problems with the bill, but the biggest problem is that it is vaguely written and can be used for censorship.

Stop SOPA

To show how unhappy I am with GoDaddy I have decided to vote with my wallet. I have just initiated the transfer of this domain to Loopia, a Swedish domain registrar where I have several other domains. The transfer will take a few weeks to complete.

First Post

Welcome to the First Post™ of this relaunched website! The plan is to host my public projects here and write about web and software development.

Welcome to the First Post™ of this website! I have owned this domain since 2006 but I haven’t used it for a couple of years. The plan is to host my public projects here and write about web and software development. I have another website where I blog in Swedish about life in general and sports in particular.

The first project I will host here is a WordPress theme that I call Size Matters. It is a slightly modified version of Twenty Eleven with less whitespace and a few other minor changes.

I have also developed a WordPress plugin called Social Metadata. It inserts metadata in the header of posts and pages to make your site appear exactly like YOU want it to when shared/liked on social networks such as Facebook and Google+.

Another possible project is a lightweight WordPress plugin for social bookmarking & sharing. I’m using AddToAny on a few websites I manage but it has a lot of overhead and the layout of the buttons looks bad, especially when viewed on a small screen. If I cannot find a better plugin I might write one myself.