Wednesday, November 20, 2013

Installing Python 3 on CentOS/Redhat 5.x / 6.x From Source

The latest release of the python scripting language is Python 3.4.0 However due to backwards incompatibilities with Python 2, it has not been adopted for CentOS / Redhat Linux 5. The primary reason for this, is because release of ‘yum’ (package management) used in EL5 requires Python 2. Because of this, Python cannot be upgraded in place to version 3 without breaking the package manager.

Therefore to use Python 3, it will need to be installed outside of /usr.

Installing

Below is the list of command (with inline comments) on what is required to compile and install Python 3 from source. The installation will be done into the prefix /opt/python3. This will ensure the installation does not conflict with system software installed into /usr.
- - - - - - - - - - - - #1 will install following- - - - - - - - - - - - - - - - - - 
Installed:
  bzip2-devel.i386 0:1.0.3-6.el5_5                bzip2-devel.x86_64 0:1.0.3-6.el5_5                expat-devel.i386 0:1.95.8-11.el5_8
  expat-devel.x86_64 0:1.95.8-11.el5_8            gdbm-devel.i386 0:1.8.0-28.el5                    gdbm-devel.x86_64 0:1.8.0-28.el5
  openssl-devel.i386 0:0.9.8e-22.el5_8.4          openssl-devel.x86_64 0:0.9.8e-22.el5_8.4          readline-devel.i386 0:5.1-3.el5
  readline-devel.x86_64 0:5.1-3.el5               sqlite-devel.i386 0:3.3.6-6                       sqlite-devel.x86_64 0:3.3.6-6

Dependency Installed:
  gdbm.i386 0:1.8.0-28.el5                         keyutils-libs-devel.x86_64 0:1.2-1.el5         krb5-devel.x86_64 0:1.6.1-70.el5
  libselinux-devel.x86_64 0:1.33.4-5.7.el5         libsepol-devel.x86_64 0:1.15.2-3.el5           libtermcap-devel.x86_64 0:2.0.8-46.1
  sqlite.i386 0:3.3.6-6

Dependency Updated:
  expat.i386 0:1.95.8-11.el5_8                     expat.x86_64 0:1.95.8-11.el5_8             gdbm.x86_64 0:1.8.0-28.el5
  krb5-libs.i386 0:1.6.1-70.el5                    krb5-libs.x86_64 0:1.6.1-70.el5            krb5-workstation.x86_64 0:1.6.1-70.el5
  libselinux.i386 0:1.33.4-5.7.el5                 libselinux.x86_64 0:1.33.4-5.7.el5         libselinux-python.x86_64 0:1.33.4-5.7.el5
  libselinux-utils.x86_64 0:1.33.4-5.7.el5         libsepol.i386 0:1.15.2-3.el5               libsepol.x86_64 0:1.15.2-3.el5
  openssl.i686 0:0.9.8e-22.el5_8.4                 openssl.x86_64 0:0.9.8e-22.el5_8.4         sqlite.x86_64 0:3.3.6-6

Complete!
- - - - - - - - - - - - #1 will install above & if these are already installed we will see following- - - - - - - - - - - - - - 
[root@bhmed-dt-2q ~]# yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel
Loaded plugins: logchanges, security
Setting up Install Process
Package openssl-devel-0.9.8e-22.el5_8.4.x86_64 already installed and latest version
Package openssl-devel-0.9.8e-22.el5_8.4.i386 already installed and latest version
Package bzip2-devel-1.0.3-6.el5_5.x86_64 already installed and latest version
Package bzip2-devel-1.0.3-6.el5_5.i386 already installed and latest version
Package expat-devel-1.95.8-11.el5_8.x86_64 already installed and latest version
Package expat-devel-1.95.8-11.el5_8.i386 already installed and latest version
Package gdbm-devel-1.8.0-28.el5.x86_64 already installed and latest version
Package gdbm-devel-1.8.0-28.el5.i386 already installed and latest version
Package readline-devel-5.1-3.el5.x86_64 already installed and latest version
Package readline-devel-5.1-3.el5.i386 already installed and latest version
Package sqlite-devel-3.3.6-6.x86_64 already installed and latest version
Package sqlite-devel-3.3.6-6.i386 already installed and latest version
Nothing to do
[root@bhmed-dt-2q ~]#
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  1. # Install required build dependencies
    1. $ yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel
  2. # Fetch and extract source. Please refer to http://www.python.org/download/releases to ensure the latest source is used.
    1. $ wget 
      1. https://www.python.org/ftp/python/3.4.0/Python-3.4.0.tgz --no-check-certificate
      2. https://www.python.org/ftp/python/3.4.0/Python-3.4.0.tar.xz --no-check-certificate
    2. $ tar -xjf Python-3.2.tar.bz2
    3. $ cd Python-3.2
  3. # Configure the build with a prefix (install dir) of /opt/python3, compile, and install.
    1. $ ./configure --prefix=/opt/python3
    2. $ make
    3. $ sudo make install
  4. #Python 3 will now be installed to /opt/python3.
    1. $ /opt/python3/bin/python3 -V
      1. Output: Python 3.2
  5. Ensure your python 3 scripts and applications query the correct interpreter.
    1. #!/opt/python3/bin/python3 Generally Work here is completed python3 can be accessed using this path.
  6. Now make sure you set your new PYTHON as in your PATH. try
    1. $which python: If this return a different version of python may be your system default then you might want to set your new python3.4 path or in the python3.4 directory try ./python that should open your new 3.4 python.
      1. echo $PATH
      2. export PATH=/YOUR/PYTHON/3.4_HOMEDIR/PATH:$PATH
      3. echo $PATH : (this is temporary, you can add to config files to make this export permanent) notice that i am adding it to the front so it gets picked before our system default.
  7. Now lets install some packages:
    1. # Create your virtual env: so this installation will not clash with system default python and/or you are not using the wrong python pip
      1. $ pyvenv-3.4 py3.4env
    2. # Activate it:
      1. $ source py3.4env/bin/activate
    3. # This should be pointing to the right pip in your new python 3.4 Home
      1. (py3.4env) $ which pip
    4. # This checks if your env is clean ..this would not return any  output
      1. (py3.4env) $ pip freeze
      2. (py3.4env) $ pip install numpy
      3. (py3.4env) $ pip install pytz