Python Compile with zlib and SSL for non-root privileges

Sometimes there is a requirement to have your own Python version, and there is no root privileges on a system.

So, compiling a Python, which also requires having local pip, will cause not simple, and specific Python compilation options: SSL and zlib support.

And, as we don’t have root access, we can’t just say
yum|apt install openssl-dev zlib-dev
and you will also have to compile openssl, zlib and point those new binaries to Python’s ./configure

Let’s say, we do compile and put all required binaries and libraries we compile to ~/venv, don’t mess with Python’s virtualenv dir. After successful compile and pointing to newly compiled Python, you can create virtual entertainment at any local folder, using new virtualenv command.

Compile SSL and zlib libraries for Python compile

Download and extract SSL and zlib sources to some temporary folder

wget https://zlib.net/zlib-1.2.11.tar.gz
tar zxfv zlib-1.2.11.tar.gz
wget https://www.openssl.org/source/openssl-1.0.2r.tar.gz
tar xzvf openssl-1.0.2r.tar.gz 

Choose and create main user directory which will contain bin and lib target folders

mkdir ~/venv
add new bin directory to PATH to .profile: 
export PATH=~/venv/bin:$PATH

Compile SSL ans zlib at their extracted path

cd zlib-1.2.11
./configure --prefix=~/venv
make && make install
cd openssl-1.0.2r
./config --prefix=~/venv
make && make instal

Prepare Python Setup.dist for compile with SSL and zlib

Download and extract Python sources

wget https://www.python.org/ftp/python/2.7.16/Python-2.7.16.tgz
tar zxfv Python-2.7.16.tgz 
cd Python-2.7.16

Set environment variables which point to new SSL binaries and libraries

export LDFLAGS="-L~/venv/lib"
export LD_LIBRARY_PATH="~/venv/lib"
export CPPFLAGS="-I~/venv/include -I~/venv/include/openssl"
export CFLAGS="-I~/venv/include/openssl"

Update Modules/Setup.dist file

  • uncomment modules
    • zlib 
    • _md5
    • all _sha* modules 
    • _ssl
  • change SSL variable 
    SSL=~/venv/ssl

Compile Python with SSL and zlib. Set user environment for new user Python binaries

Configure and Compile with zlib, SSL config not supported

 ./configure --prefix=~/venv --with-zlib=~/venv
make && make install 

Update user’s ~/.profile and switch to local user’s new Python binaries

export PATH=~/venv/bin:$PATH 
source ~/.profile

Check Python’s binary path

which python
should display > ~/venv/bin/python

Check correct SSL and md5 support

python -c "import ssl, hashlib; print(ssl.OPENSSL_VERSION)" 

Install pip and virtualenv

Download and install pip to old user local area

wget --no-check-certificate https://bootstrap.pypa.io/get-pip.py -O - | python - --user 

Update pip to new library path

which pip
should display > ~/venv/bin/pip
pip install --upgrade pip

Install virtualenv

pip install virtualenv

Create virtualenv using newly compiled Python binaries

which virtualenv
should display > ~/venv/bin/virtualenv
virtualenv ~/venv/env 

Use new virtualenv, based on newly compiled Python

source ~/venv/env/bin/activate
pip install <whatever>
deactivate
 ~/venv/env/bin/python <any python script on virtualenv>

А здоровье мы вам обещаем

«… а здоровье мы вам обещаем …» — такая фраза прозвучала в одном из тостов моих родственников в поздравлении на свадьбе братика.
Читать далее А здоровье мы вам обещаем