Posit - Install Python From Source

How to install Python from source on a Linux server.
Install Python from pre-compiled binaries

Posit recommends installing Python appropriately from pre-compiled binaries using the Install Python procedures.

Install required dependencies

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

Enable the Extra Packages for Enterprise Linux (EPEL) repository:

sudo dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm

Enable the CodeReady Linux Builder repository:

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:

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

sudo apt-get update
sudo apt-get install \
    curl \
    gcc \
    libbz2-dev \
    libev-dev \
    libffi-dev \
    libgdbm-dev \
    liblzma-dev \
    libncurses-dev \
    libreadline-dev \
    libsqlite3-dev \
    libssl-dev \
    make \
    tk-dev \
    wget \
    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:

export PYTHON_VERSION=3.12.4
export PYTHON_MAJOR=3

Python.org lists the available versions of Python.

Download and extract Python

Download and extract Python. Then, navigate into the Python directory:

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:

./configure \
    --prefix=/opt/python/${PYTHON_VERSION} \
    --enable-shared \
    --enable-optimizations \
    --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:

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:

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

(Optional) Add Python to the system PATH

Note

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, define this in a script within the /etc/profile.d/ directory (where <PYTHON-VERSION> is the version of Python that you installed earlier):

File: /etc/profile.d/python.sh
PATH=/opt/python/<PYTHON-VERSION>/bin/:$PATH

(Optional) Install multiple versions of Python

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

Back to top