How To Set Up Python 3.3.3 on CentOS 6.4

Introduction

When you get started with CentOS, however, you will be surprised to see that Python still is at version 2.6 and it should not be used by deployed applications regardless as it is reserved for system’s use.

We are going to talk about downloading and setting up Python 3.3.3 without breaking the system’s default. It is rather important to not to get involved with that as critical system tools such as YUM depend on it. Furthermore, we will install two popular must-have Python companions pip and virtualenv.

After we are done, you will be able to simultaneously use either versions of Python on your CentOS 6.4 VPS, create and use virtual environments and finally, download and manage Python packages for each version.

Preparing The System and Installing Python

The installation of Python on CentOS consists of a few (simple) stages, starting with updating the system and followed by actually getting Python and proceeding with the set up process.

Remember: You can see all available releases of Python by checking out the Releases page.

Updating the Default CentOS Applications

Before we begin with the installation, let’s make sure to update the default system applications to have the latest versions available.

yum update

Preparing The System for Development Installations

CentOS distributions are lean – perhaps, a little too lean – meaning they do not come with many of the popular applications and tools that you are likely to need.

This is an intentional design choice. For our installations, however, we are going to need some libraries and tools (i.e. development [related] tools) not shipped by default. Therefore, we need to get them downloaded and installed before we continue.

The recommended of doing this is simply downloading a bunch of tools by a single command with yum software groups.

YUM Software Groups consist of bunch of commonly used tools (applications) bundled together, ready for download all at the same time via execution of a single command and stating a group name. Using YUM, you can even download multiple groups together.

How to Install Development Tools using YUM on CentOS

yum groupinstall -y 'development tools'
yum install -y zlib-dev openssl-devel sqlite-devel bzip2-devel

Downloading, Building and Installing Python

Let’s begin with retrieving the (compressed) archive containing Python source code.

wget http://www.python.org/ftp/python/3.3.3/Python-3.3.3.tar.xz

Extracting the Compressed Source Archive

# Let's decode (-d) the XZ encoded tar archive:
xz -d Python-3.3.3.tar.xz

# Now we can perform the extraction:
tar -xvf Python-3.3.3.tar

Configuring and Installation

Before building the source, we need to make sure that all the dependencies are there and prepare the environment. This is achieved automatically by using ./configure to handle the task for us.

# Enter the file directory:
cd Python-3.3.3

# Start the configuration (setting the installation directory)
# By default files are installed in /usr/local.
# You can modify the --prefix to modify it (e.g. for $HOME).
./configure --prefix=/usr/local    

This procedure should execute without any hiccups – as we have downloaded all the necessary tools and applications. When it is complete, we will be ready to move on to the next step: building and installing.

Building and Installing

After configuring everything for the system we are working on, we can continue with building (compiling) the source and installing the application. Normally, one would use “make install”; however, in order not to override system defaults – replacing the Python already used by the system – we will use make altinstall.

# Let's build (compile) the source
# This procedure can take awhile (~a few minutes)
make

# After building everything:
make altinstall

Example for version 3.3.3:

make && make altinstall   # <--- Two commands joint together

[Optional Step] Adding New Python Installation Location to PATH

Note: If you have followed the instructions using the default settings, you should not have the need to go through this section. However, if you have chosen a different path than /usr/local to install Python, you will need to perform the following to be able to run it without explicitly stating its full [installation] path each time.

Once the installation is complete, we can access the generated binaries (i.e. the Python interpreter for the version we have chosen) only by specifying its full location (path) (e.g. /usr/local/bin/python2.7) – unless of course the path exists already in the PATH variable (i.e. the variable which tells contains information on where to look for files stated).

If you would like to be able to access the newly installed Python interpreter without explicitly telling each and every time where to look for it, its path needs to be appended to PATH variable:

# Example: export PATH="[/path/to/installation]:$PATH"
export PATH="/usr/local/bin:$PATH"

Setting Up Common Python Tools pip and virtualenv

Having installed Python, we can now finalize completing the basics for application production and deployment. For this, we will set up two of the most commonly used tools: pip package manager and virtualenv environment manager.

If you are interested in learning more about these two tools or just quickly refreshing your knowledge, consider reading Common Python Tools: Using virtualenv, Installing with Pip, and Managing Packages.

Installing pip on CentOS Using a New Python Installation

Before installing pip, we need to get its only external dependency – setuptools.

From the article on virtualenv and pip:

It [setuptools] builds on the (standard) functionality of Python’s distribution utilities toolset called distutils. Given that distils is provided by default, all we need left is setuptools.

Execute the following commands to install setuptools:

This will install it for version 2.7.6

# Let's download the installation file using wget:
wget --no-check-certificate https://pypi.python.org/packages/source/s/setuptools/setuptools-1.4.2.tar.gz

# Extract the files from the archive:
tar -xvf setuptools-1.4.2.tar.gz

# Enter the extracted directory:
cd setuptools-1.4.2

# Install setuptools using the Python we've installed (2.7.6)
python2.7 setup.py install

Installing pip itself is a very simple process afterwards. We will make use of the instructions from the article mentioned above to have it downloaded and installed automatically and securely using cURL library.

Note: To learn more about cURL, you can refer to the section explaining it here.

Let’s download the setup files for pip and have Python (2.7) install it:

This will install it for version 2.7.6

curl https://raw.github.com/pypa/pip/master/contrib/get-pip.py | python2.7 -

To learn about how to use pip, please refer to the article on Common Python Tools: Using virtualenv, Installing with Pip, and Managing Packages.

Installing virtualenv on CentOS Using a New Python Installation

Now that we have pip the package manager ready, getting virtualenv on the system is a breeze.

Run the following command to download and install virtualenv:

pip install virtualenv

To learn about how to use virtualenv, please refer to the article on Common Python Tools: Using virtualenv, Installing with Pip, and Managing Packages.

Submitted by: O.S. Tezer
The following two tabs change content below.

allenpg

Latest posts by allenpg (see all)

Leave a Reply