How To Set Up Python 2.7.10 and 3.3.4 on CentOS 6.4

Python Installation Procedure From Source

Setting up Python on our system will consist of 3 stages and 4 tools:

  1. Downloading the compressed source code package (wget),
  2. Extracting the files from this package (tar),
  3. Configuring and building the application (autoconf (configure) / make).

 

We will:

  • Use ./configure to configure everything before installation
  • Use make to connect libraries and the source before
  • Using make install – altinstall in our case – to build (compile) source code to create binary files and install the application on our system as configured using ./configure.

Downloading, Building (Compiling) and Installing Python

In this section, all the instructions given can be used to download any version of Python. You will just need to replace the version stated (which is “2.7.10” in the example below) with the version you require (e.g. “3.3.4”). You can install and use multiple versions at the same time. Although, you will need to specify their version during the execution (i.e. instead of python, you will need to use python2.7 or python3.3).

Downloading the Source Archive

Let’s begin with retrieving the (compressed) archive containing Python source code. We will target --version 2.7.10.

wget https://www.python.org/ftp/python/2.7.10/Python-2.7.10.tgz

Example for version 3.3.3:

wget https://www.python.org/ftp/python/3.4.3/Python-3.4.3.tgz

Extracting the Compressed Source Archive

This process consists of two steps: first decoding the XZ archive, followed by extracting the tar.

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

# For Python 3.4.3
tar -xvf Python-3.4.3.tgz

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-2.7.10

# 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

Example for version 3.4.3:

cd Python-3.4.3    
./configure --prefix=/usr/local --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"

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.4.3:

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

[Optional Step] Adding New Python Installation Location to PATH

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

Installing pip on CentOS Using a New Python Installation

Execute the following commands to install setuptools:

This will install it for version 2.7.10

# 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.9)
python2.7 setup.py install

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

This will install it for version 2.7.10

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