Intro

Centos 7 minimal ships with python 2.7.5 and using such an old python version can cause problems when using some python packages for network automation. This post describes how to install python 2.7.13 and python 3.6.2 on a Centos 7 minimal server.

Install Python

Install required YUM packages.

cmd
sudo yum install -y gcc wget zlib zlib-devel openssl openssl-devel

Use wget to download the python tar files.

cmd
cd /tmp
wget https://www.python.org/ftp/python/2.7.13/Python-2.7.13.tgz
wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tgz

Extract the tar files.

cmd
tar -xvf Python-2.7.13.tgz
tar -xvf Python-3.6.2.tgz

Build the python versions and make an alternate install.

cmd
cd /tmp/Python-2.7.13
./configure --prefix=/usr/local
sudo make altinstall

cd /tmp/Python-3.6.2
./configure --prefix=/usr/local
sudo make altinstall

sudo make altinstall prevents replacement of the /usr/bin/python binary. Its recommended to leave the system python alone on RHEL distributions as yum uses the system python for updates.

Create a symlink for /usr/bin/python3.6

cmd
sudo ln -s /usr/local/bin/python3.6 /usr/bin/python3.6
Note
This is not required for python2.7

Install Pip

Install pip with both python2.7 and python3.6

cmd
cd /tmp
wget "https://bootstrap.pypa.io/get-pip.py"
sudo /usr/local/bin/python2.7 get-pip.py
sudo /usr/local/bin/python3.6 get-pip.py

Create symlinks for pip

cmd
sudo ln -s /usr/local/bin/pip2.7 /usr/bin/pip2
sudo ln -s /usr/local/bin/pip3.6 /usr/bin/pip3

Upgrade PIP and Setuptools

Old versions of setuptools and pip can also cause issues with some network automation libraries so update them with pip

cmd
sudo pip2 install -U pip setuptools
sudo pip3 install -U pip setuptools

Confirmation

Confirm python and pip versions.

cmd
python2.7 --version
# output

Python 2.7.13

python3.6 --version
# output

Python 3.6.2

pip2.7 --version
# output

pip 9.0.1 from /usr/local/lib/python2.7/site-packages (python 2.7)

pip3.6 --version
# output

pip 9.0.1 from /usr/local/lib/python3.6/site-packages (python 3.6)

Clean up

cmd
cd /tmp
 rm -f get-pip.py
 sudo rm -rf Python-*

Outro

There it is, modern python versions installed on Centos 7 minimal the traditional way. This post is mostly for documentation, in practice you would use your configuration management tools to install these versions of python.

# python