Monday 5 December 2016

Installing Python on Linux without disrupting existing one

Python is important package that comes installed on Linux systems. Corrupting the pre-installed version makes many of the packages non-usable. I could not yum after the pre-installed version of Python was corrupt.

Here is how you can install a version of Python on Linux operating system, keeping the pre-installed one safe. Here, we use CentOS 6.4 version, which is a pretty old release.

Issues solved by this document

pip is an important module you need while working on Python. Following are the errors I have faced while trying to install pip module and this document helps you in solving them too:
  • zipimport.ZipImportError: can't decompress data; zlib not available

    [root@localhost tmp]# python2.7 get-pip.py 
    Traceback (most recent call last):
      File "get-pip.py", line 20061, in 
        main()
      File "get-pip.py", line 194, in main
        bootstrap(tmpdir=tmpdir)
      File "get-pip.py", line 82, in bootstrap
        import pip
    zipimport.ZipImportError: can't decompress 
    data; zlib not available
    

  • There was a problem confirming the ssl certificate: Can't connect to HTTPS URL because the SSL module is not available.

    [root@localhost tmp]# python2.7 get-pip.py 
    pip is configured with locations that require TLS/SSL, 
    however the ssl module in Python is not available.
    Collecting pip
      Could not fetch URL https://pypi.python.org/simple/p
    ip/: There was a problem confirming the ssl certificat
    e: Can't connect to HTTPS URL because the SSL module i
    s not available. - skipping
      Could not find a version that satisfies the requirem
    ent pip (from versions: )
    No matching distribution found for pip
    [root@localhost tmp]#
    

Verify the current version

You may use python --version command on the terminal to find the current version.

Installing a different version

  1. You may download a version you would need to install from Python Download. By default, Python comes as tarball for Linux.
  2. Once you have the tarball downloaded, copy it to a location where we extract and do the installation. Here, I copied the 2.7.12 version to /tmp. Use the version which is available for your platform and remember to use the commands accordingly.
  3. Extract tarball and change directory to the extracted folder.
    tar xvf Python-2.7.12.tar.xz
    cd Python-2.7.12
  4. Install required Linux packages. gcc is a utility for compiling and installing Python from source, zlib-devel and openssl-devel are required to avoid errors while installing pip module at a later stage. You would need to be root or a sudoer to install these packages:
    yum install gcc
    yum install zlib-devel
    yum install openssl-devel
  5. Run configure with option to export shared libraries. This is necessary if you would need to use pyinstaller module.
    ./configure --enable-shared
  6. Generate the build:
    make
  7. Install Python without replacing the existing version. Using altinstall option will help you to use the new installation with version suffix during execution. e.g. python2.7 --version:
    make altinstall
  8. Make a link to Python library path:
  • On 32 bit OS
    ln -s /usr/local/lib/libpython2.7.so.1.0 /usr/lib/libpython2.7.so.1.0
  • On 64 bit OS
     ln -s /usr/local/lib/libpython2.7.so.1.0 /usr/lib64/libpython2.7.so.1.0

 Installing pip module

Here is how you may get pip for the Python version you have just installed:
  1. Running get-pip.py is a better way to install pip. get-pip.py file is available at pip module listing.
  2. Run get-pip.py with the version of Python you have installed. For example:
    python2.7 get-pip.py
    This installs a pip utility which you can use with suffix. e.g. pip2.7 install pyinstaller