Skip to content

Install Python From Source#

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

Binary install

If you need to install Python from pre-compiled binaries, we do have documentation that uses conda, a package management utility, to install different versions of Python. However, at this time, we do not recommend this Python installation path.

Install required dependencies#

First, enable the optional and required repositories by following these steps:

# Enable the Extra Packages for Enterprise Linux (EPEL) repository
$ sudo yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm

# Enable the CodeReady Linux Builder repository
$ sudo subscription-manager repos --enable codeready-builder-for-rhel-8-x86_64-rpms

# If running RHEL 8 in a public cloud, such as Amazon EC2, enable the CodeReady
# Linux Builder repository from Red Hat Update Infrastructure (RHUI) instead
$ sudo dnf install dnf-plugins-core
$ sudo dnf config-manager --set-enabled "codeready-builder-for-rhel-8-*-rpms"
# Enable the Extra Packages for Enterprise Linux (EPEL) repository
$ sudo yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm 

# On RHEL 7, enable the Optional repository
$ sudo subscription-manager repos --enable "rhel-*-optional-rpms"

# If running RHEL 7 in a public cloud, such as Amazon EC2, enable the
# Optional repository from Red Hat Update Infrastructure (RHUI) instead
$ sudo yum install yum-utils
$ sudo yum-config-manager --enable "rhel-*-optional-rpms"
$ sudo apt-get update
$ sudo apt-get install gdebi-core

Next, use the following commands to install the dependencies required to build and run Python for your Linux distribution: Python for your Linux distribution.

$ sudo yum-builddep python3
$ sudo yum install wget yum-utils make gcc openssl-devel bzip2-devel libffi-devel zlib-devel
$ 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.7.7
$ 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#

Install pip 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

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 can 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.