Install Python From Source

These instructions describe how to install Python from source on a Linux server.

Install required dependencies

Info

To install the necessary build dependencies for Python, you might need to enable additional repositories for third-party or source packages by using the following commands:

$ sudo yum install epel-release
$ sudo sed -i.bak "/^#.*deb-src.*universe$/s/^# //g" /etc/apt/sources.list
$ sudo apt-get update

Use the following commands to install the dependencies required to build and run Python for your Linux distribution.

$ sudo yum-builddep python python-libs
$ sudo yum install libffi-devel sqlite-devel zlib zlib-devel
$ sudo apt-get build-dep python
$ sudo apt-get install libffi-dev libgdbm-dev libsqlite3-dev libssl-dev zlib1g-dev
$ sudo zypper install \
    automake \
    fdupes \
    gcc \
    gcc-c++ \
    gcc-fortran \
    gdbm-devel \
    gettext-tools \
    gmp-devel \
    intltool \
    libbz2-devel \
    libexpat-devel \
    libffi-devel \
    libnsl-devel \
    lzma-devel \
    make \
    ncurses-devel \
    netcfg \
    openssl-devel \
    pkgconfig \
    readline-devel \
    sqlite-devel \
    xz \
    zlib-devel

Specify Python version

Specify the version of Python that you want to install:

Terminal

$ export PYTHON_VERSION=3.6.9
$ export PYTHON_MAJOR=3

A list of available Python versions can be found on python.org.

Download and extract Python

Download and extract Python, then navigate into the Python directory:

Terminal

$ curl -O https://www.python.org/ftp/python/${PYTHON_VERSION}/Python-${PYTHON_VERSION}.tgz
$ tar -xvzf Python-${PYTHON_VERSION}.tgz
$ cd Python-${PYTHON_VERSION}

Build and install Python

Configure, make, and install Python:

Terminal

$ ./configure \
    --prefix=/opt/python/${PYTHON_VERSION}
    --enable-shared
    --enable-ipv6
    LDFLAGS=-Wl,-rpath=/opt/python/${PYTHON_VERSION}/lib,--disable-new-dtags

$ make
$ sudo make install

Install pip and virtualenv

Install pip and virtualenv into the version of Python that you just installed:

Terminal

$ curl -O https://bootstrap.pypa.io/get-pip.py
$ sudo /opt/python/${PYTHON_VERSION}/bin/python${PYTHON_MAJOR} get-pip.py

Terminal

$ sudo /opt/python/${PYTHON_VERSION}/bin/pip install virtualenv

Verify Python installation

Verify that Python is installed by running the following command:

Terminal

$ /opt/python/${PYTHON_VERSION}/bin/python${PYTHON_MAJOR} --version

(Optional) Add Python to the system PATH

Info

You can configure Python on the system PATH so that users will be able to use pip within a terminal to install packages to their home directory, similar to how R works with install.packages().

The recommended method to add Python to the PATH is to append the version of Python that you installed to the system-wide PATH variable. For example, this can be defined in a script within the /etc/profile.d/ directory:

File: /etc/profile.d/python.sh

PATH=/opt/python/<PYTHON-VERSION>/bin/:$PATH

where <PYTHON-VERSION> is the version of Python that you installed earlier.

(Optional) Install multiple versions of Python

If you want to install multiple versions of Python on the same server, you can repeat these steps to specify, download, and install a different version of Python alongside existing versions.