Install Python From Source#
These instructions describe how to install Python from source on a Linux server.
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:
$ sudo subscription-manager repos --enable codeready-builder-for-rhel-9-$(arch)-rpms
# enable the CodeReady Linux Builder repository from Red Hat Update Infrastructure (RHUI)
$ sudo dnf install dnf-plugins-core
$ sudo dnf config-manager --set-enabled codeready-builder-for-rhel-9-*-rpms
Enable the Extra Packages for Enterprise Linux (EPEL) repository
$ sudo yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
$ sudo subscription-manager repos --enable codeready-builder-for-rhel-8-x86_64-rpms
# enable the CodeReady Linux Builder repository from Red Hat Update Infrastructure (RHUI)
$ 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
Enable the Optional repository:
$ sudo subscription-manager repos --enable "rhel-*-optional-rpms"
# Optional repository from Red Hat Update Infrastructure (RHUI)
$ 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:
$ 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 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.9.14
$ 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:
$ 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#
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 (where <PYTHON-VERSION>
is the version of Python that you installed earlier):
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, you can repeat these steps to specify, download, and install a different version of Python alongside existing versions.