Installing Python3.x on Linux from source

Hariharan M
1 min readDec 17, 2020

If you ever use deep learning libs, then they probably need to compile some native C++ code. They might call modules from natively compiled C++ libs. So heres a clean way to get py3.x (x can be 7/8/9) installed to use with linux via building from source.

Some variables:

export PYHOME=/opt/py3.x #replace with your path.
export LD_LIBRARY_PATH=$PYHOME/libffi-3.2.1/lib:$PYHOME/libffi-3.2.1/lib64:$PYHOME/lib

Install libffi

It is an interface for calling C compiled libs from python, which can be installed with yum/apt-get. Heres building from source.

wget ftp://sourceware.org:/pub/libffi/libffi-3.2.1.tar.gz
tar xfz libffi-3.2.1.tar.gz
cd libffi-3.2.1
./configure --prefix=$PYHOME/libffi-3.2.1
make
make install
cd ..

Install Python

Substitute x with 7/8/9

wget https://www.python.org/ftp/python/3.x.0/Python-3.x.0.tgz
tar xzf Python-3.x.0.tgz
cd Python-3.x.0
./configure --prefix=$PYHOME LDFLAGS=-L$PYHOME/libffi-3.2.1/lib64 PKG_CONFIG_PATH=$PYHOME/libffi-3.2.1/lib/pkgconfig --enable-shared
make && make install

To make it discoverable, add to path.

echo "PATH=$PYHOME/bin/:$PATH" >> ~/.bashrc
echo "export PYTHONPATH=$PYHOME/bin/" >> ~/.bashrc
echo "export LD_LIBRARY_PATH=$PYHOME/libffi-3.2.1/lib:$PYHOME/libffi-3.2.1/lib64:$PYHOME/lib" >> ~/.bashrc
source ~/.bashrc

Coming up next: Installing pytorch, CUDA drivers, cuDNN for GPU run.

References

  1. https://github.com/pyenv/pyenv/issues/1183#issuecomment-529682321
  2. https://tecadmin.net/install-python-3-9-on-centos-8/
  3. https://lsstdesc.org/Coord/_build/html/overview.html

--

--