RHEL 9.3 - Installing and using Dynamic Programming Languages
RHEL 9.3 - Installing and using Dynamic Programming Languages
Installing and using Python and PHP in Red Hat Enterprise Linux 9
The text of and illustrations in this document are licensed by Red Hat under a Creative Commons
Attribution–Share Alike 3.0 Unported license ("CC-BY-SA"). An explanation of CC-BY-SA is
available at
http://creativecommons.org/licenses/by-sa/3.0/
. In accordance with CC-BY-SA, if you distribute this document or an adaptation of it, you must
provide the URL for the original version.
Red Hat, as the licensor of this document, waives the right to enforce, and agrees not to assert,
Section 4d of CC-BY-SA to the fullest extent permitted by applicable law.
Red Hat, Red Hat Enterprise Linux, the Shadowman logo, the Red Hat logo, JBoss, OpenShift,
Fedora, the Infinity logo, and RHCE are trademarks of Red Hat, Inc., registered in the United States
and other countries.
Linux ® is the registered trademark of Linus Torvalds in the United States and other countries.
XFS ® is a trademark of Silicon Graphics International Corp. or its subsidiaries in the United States
and/or other countries.
MySQL ® is a registered trademark of MySQL AB in the United States, the European Union and
other countries.
Node.js ® is an official trademark of Joyent. Red Hat is not formally related to or endorsed by the
official Joyent Node.js open source or commercial project.
The OpenStack ® Word Mark and OpenStack logo are either registered trademarks/service marks
or trademarks/service marks of the OpenStack Foundation, in the United States and other
countries and are used with the OpenStack Foundation's permission. We are not affiliated with,
endorsed or sponsored by the OpenStack Foundation, or the OpenStack community.
Abstract
Install and use Python 3, package Python 3 RPMs, and learn how to handle interpreter directives in
Python scripts. Install the PHP scripting language, use PHP with the Apache HTTP Server or the
ngninx web server, and run a PHP script from a command-line interface.
Table of Contents
Table of Contents
. . . . . . . . . .OPEN
MAKING . . . . . . SOURCE
. . . . . . . . . .MORE
. . . . . . .INCLUSIVE
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3. . . . . . . . . . . . .
. . . . . . . . . . . . . FEEDBACK
PROVIDING . . . . . . . . . . . . ON
. . . .RED
. . . . .HAT
. . . . .DOCUMENTATION
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4. . . . . . . . . . . . .
.CHAPTER
. . . . . . . . . . 1.. .INTRODUCTION
. . . . . . . . . . . . . . . . . TO
. . . .PYTHON
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5. . . . . . . . . . . . .
1.1. PYTHON VERSIONS 5
1.2. MAJOR DIFFERENCES IN THE PYTHON ECOSYSTEM SINCE RHEL 8 5
.CHAPTER
. . . . . . . . . . 2.
. . INSTALLING
. . . . . . . . . . . . . .AND
. . . . .USING
. . . . . . . PYTHON
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7. . . . . . . . . . . . .
2.1. INSTALLING PYTHON 3 7
2.2. INSTALLING ADDITIONAL PYTHON 3 PACKAGES 7
2.3. INSTALLING ADDITIONAL PYTHON 3 TOOLS FOR DEVELOPERS 8
2.4. USING PYTHON 9
.CHAPTER
. . . . . . . . . . 3.
. . PACKAGING
. . . . . . . . . . . . . .PYTHON
. . . . . . . . . .3. RPMS
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
..............
3.1. SPEC FILE DESCRIPTION FOR A PYTHON PACKAGE 10
3.2. COMMON MACROS FOR PYTHON 3 RPMS 12
3.3. USING AUTOMATICALLY GENERATED DEPENDENCIES FOR PYTHON RPMS 13
.CHAPTER
. . . . . . . . . . 4.
. . .HANDLING
. . . . . . . . . . . .INTERPRETER
. . . . . . . . . . . . . . .DIRECTIVES
. . . . . . . . . . . . .IN
. . .PYTHON
. . . . . . . . . SCRIPTS
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
..............
4.1. MODIFYING INTERPRETER DIRECTIVES IN PYTHON SCRIPTS 15
. . . . . . . . . . . 5.
CHAPTER . . INSTALLING
. . . . . . . . . . . . . .TCL/TK
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
..............
5.1. INTRODUCTION TO TCL/TK 17
5.2. INSTALLING TCL 17
5.3. INSTALLING TK 17
.CHAPTER
. . . . . . . . . . 6.
. . .USING
. . . . . . .THE
. . . . .PHP
. . . . .SCRIPTING
. . . . . . . . . . . .LANGUAGE
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
..............
6.1. INSTALLING THE PHP SCRIPTING LANGUAGE 19
6.2. USING THE PHP SCRIPTING LANGUAGE WITH A WEB SERVER 19
6.2.1. Using PHP with the Apache HTTP Server 20
6.2.2. Using PHP with the nginx web server 21
6.3. RUNNING A PHP SCRIPT USING THE COMMAND-LINE INTERFACE 23
6.4. ADDITIONAL RESOURCES 24
1
Red Hat Enterprise Linux 9 Installing and using dynamic programming languages
2
MAKING OPEN SOURCE MORE INCLUSIVE
3
Red Hat Enterprise Linux 9 Installing and using dynamic programming languages
4. Enter your suggestion for improvement in the Description field. Include links to the relevant
parts of the documentation.
4
CHAPTER 1. INTRODUCTION TO PYTHON
With Red Hat Enterprise Linux, many packages that are installed on the system, such as packages
providing system tools, tools for data analysis, or web applications, are written in Python. To use these
packages, you must have the python* packages installed.
Additional versions of Python 3 are distributed as non-modular RPM packages with a shorter life cycle
through the AppStream repository in minor RHEL 9 releases. You can install these additional Python 3
versions in parallel with Python 3.9.
For details about the length of support, see Red Hat Enterprise Linux Life Cycle and Red Hat Enterprise
Linux Application Streams Life Cycle.
In RHEL 9, the unversioned form of the python command points to the default Python 3.9 version and
it is an equivalent to the python3 and python3.9 commands. In RHEL 9, you cannot configure the
unversioned command to point to a different version than Python 3.9.
The python command is intended for interactive sessions. In production, it is recommended to use
python3, python3.9, or python3.11 explicitly.
You can uninstall the unversioned python command by using the dnf remove /usr/bin/python
5
Red Hat Enterprise Linux 9 Installing and using dynamic programming languages
You can uninstall the unversioned python command by using the dnf remove /usr/bin/python
command.
If you need a different python or python3 command, you can create custom symlinks in /usr/local/bin
or ~/.local/bin, or use a Python virtual environment.
Several other unversioned commands are available, such as /usr/bin/pip in the python3-pip package. In
RHEL 9, all unversioned commands point to the default Python 3.9 version.
6
CHAPTER 2. INSTALLING AND USING PYTHON
The unversioned python command points to the default Python 3.9 version.
Procedure
Verification steps
To verify the Python version installed on your system, use the --version option with the python
command specific for your required version of Python.
$ python3 --version
$ python3.11 --version
Procedure
7
Red Hat Enterprise Linux 9 Installing and using dynamic programming languages
Additional resources
The python3-pytest package and its dependencies are available in the AppStream repository.
python3*-idle
python3*-debug
python3*-Cython
IMPORTANT
The content in the CodeReady Linux Builder repository is unsupported by Red Hat.
NOTE
To install packages from the CRB repository, use the following procedure.
Procedure
Additional resources
How to enable and make use of content within CodeReady Linux Builder
8
CHAPTER 2. INSTALLING AND USING PYTHON
Package manifest
Prerequisites
If you want to download and install third-party applications for Python 3.11, install the
python3.11-pip package.
Procedure
To run the Python 3.9 interpreter or related commands, use, for example:
$ python3
$ python3 -m venv --help
$ python3 -m pip install package
$ pip3 install package
To run the Python 3.11 interpreter or related commands, use, for example:
$ python3.11
$ python3.11 -m venv --help
$ python3.11 -m pip install package
$ pip3.11 install package
9
Red Hat Enterprise Linux 9 Installing and using dynamic programming languages
The packaging format of native Python packages is defined by Python Packaging Authority (PyPA)
Specifications. Most Python projects use the distutils or setuptools utilities for packaging, and defined
package information in the setup.py file. However, possibilities of creating native Python packages have
evolved over time. For more information about emerging packaging standards, see pyproject-rpm-
macros.
This chapter describes how to package a Python project that uses setup.py into an RPM package. This
approach provides the following advantages compared to native Python packages:
Dependencies on Python and non-Python packages are possible and strictly enforced by the
DNF package manager.
You can cryptographically sign the packages. With cryptographic signing, you can verify,
integrate, and test content of RPM packages with the rest of the operating system.
Preamble (contains a series of metadata items that are used in the Body)
An RPM SPEC file for Python projects has some specifics compared to non-Python RPM SPEC files.
IMPORTANT
A name of any RPM package of a Python library must always include the python3- or
python3.11- prefix.
Other specifics are shown in the following SPEC file example for the python3*-pello package. For
description of such specifics, see the notes below the example.
Name: python-pello 2
Version: 1.0.2
Release: 1%{?dist}
Summary: Example Python library
License: MIT
URL: https://github.com/fedora-python/Pello
Source: %{url}/archive/v%{version}/Pello-%{version}.tar.gz
10
CHAPTER 3. PACKAGING PYTHON 3 RPMS
BuildArch: noarch
BuildRequires: python%{python3_pkgversion}-devel 3
%description %_description
%package -n python%{python3_pkgversion}-pello 4
Summary: %{summary}
%prep
%autosetup -p1 -n Pello-%{version}
%build
# The macro only supported projects with setup.py
%py3_build 5
%install
# The macro only supported projects with setup.py
%py3_install
%check 6
%{pytest}
# Note that there is no %%files section for the unversioned python module
%files -n python%{python3_pkgversion}-pello
%doc README.md
%license LICENSE.txt
%{_bindir}/pello_greeting
By defining the python3_pkgversion macro, you set which Python version this package will be
11
Red Hat Enterprise Linux 9 Installing and using dynamic programming languages
1 By defining the python3_pkgversion macro, you set which Python version this package will be
built for. To build for the default Python version 3.9, either set the macro to its default value 3 or
remove the line entirely.
2 When packaging a Python project into RPM, always add the python- prefix to the original name of
the project. The original name here is pello and, therefore, the name of the Source RPM (SRPM)
is python-pello.
3 BuildRequires specifies what packages are required to build and test this package. In
BuildRequires, always include items providing tools necessary for building Python packages:
python3-devel (or python3.11-devel) and the relevant projects needed by the specific software
that you package, for example, python3-setuptools (or python3.11-setuptools) or the runtime
and testing dependencies needed to run the tests in the %check section.
4 When choosing a name for the binary RPM (the package that users will be able to install), add a
versioned Python prefix. Use the python3- prefix for the default Python 3.9 or the python3.11-
prefix for Python 3.11. You can use the %{python3_pkgversion} macro, which evaluates to 3 for
the default Python version 3.9 unless you set it to an explicit version, for example, 3.11 (see
footnote 1).
5 The %py3_build and %py3_install macros run the setup.py build and setup.py install commands,
respectively, with additional arguments to specify installation locations, the interpreter to use, and
other details.
6 The %check section should run the tests of the packaged project. The exact command depends on
the project itself, but it is possible to use the %pytest macro to run the pytest command in an
RPM-friendly way.
12
CHAPTER 3. PACKAGING PYTHON 3 RPMS
Additional resources
Prerequisites
A SPEC file for the RPM exists. For more information, see SPEC file description for a Python
package.
Procedure
1. Make sure that one of the following directories containing upstream-provided metadata is
included in the resulting RPM:
.dist-info
.egg-info
The RPM build process automatically generates virtual pythonX.Ydist provides from these
directories, for example:
python3.9dist(pello)
The Python dependency generator then reads the upstream metadata and generates
13
Red Hat Enterprise Linux 9 Installing and using dynamic programming languages
The Python dependency generator then reads the upstream metadata and generates
runtime requirements for each RPM package using the generated pythonX.Ydist virtual
provides. For example, a generated requirements tag might look as follows:
Requires: python3.9dist(requests)
3. To remove some of the generated requires, use one of the following approaches:
a. Modify the upstream-provided metadata in the %prep section of the SPEC file.
Additional resources
14
CHAPTER 4. HANDLING INTERPRETER DIRECTIVES IN PYTHON SCRIPTS
#!/usr/bin/python3
#!/usr/bin/python3.9
#!/usr/bin/python3.11
The BRP script generates errors when encountering a Python script with an ambiguous interpreter
directive, such as:
#!/usr/bin/python
or
#!/usr/bin/env python
Prerequisites
Some of the interpreter directives in your Python scripts cause a build error.
Procedure
Use the following macro in the %prep section of your SPEC file:
# %py3_shebang_fix SCRIPTNAME …
As a result, all listed files and all .py files in listed directories will have their interpreter
directives modified to point to %{python3}. Existing flags from the original interpreter
directive will be preserved and additional flags defined in the %{py3_shebang_flags}
macro will be added. You can redefine the %{py3_shebang_flags} macro in your SPEC file
to change the flags that will be added.
You can specify multiple paths. If a PATH is a directory, pathfix.py recursively scans for any
15
Red Hat Enterprise Linux 9 Installing and using dynamic programming languages
Python scripts matching the pattern ^[a-zA-Z0-9_]+\.py$, not only those with an ambiguous
interpreter directive. Add the command above to the %prep section or at the end of the
%install section.
Modify the packaged Python scripts so that they conform to the expected format. For this
purpose, you can use the pathfix.py script outside the RPM build process, too. When
running pathfix.py outside an RPM build, replace %{python3} from the preceding example
with a path for the interpreter directive, such as /usr/bin/python3 or /usr/bin/python3.11.
Additional resources
Interpreter invocation
16
CHAPTER 5. INSTALLING TCL/TK
For more information about Tcl/Tk, see the Tcl/Tk manual or Tcl/Tk documentation web page .
Procedure
Verification steps
To verify the Tcl version installed on your system, run the interpreter tclsh.
$ tclsh
% info patchlevel
8.6
5.3. INSTALLING TK
The default Tk implementation is usually installed by default. To install it manually, use the following
procedure.
Procedure
# dnf install tk
Verification steps
To verify the Tk version installed on your system, run the window shell wish. You need to be
17
Red Hat Enterprise Linux 9 Installing and using dynamic programming languages
To verify the Tk version installed on your system, run the window shell wish. You need to be
running a graphical display.
$ wish
% puts $tk_version
8.6
18
CHAPTER 6. USING THE PHP SCRIPTING LANGUAGE
Procedure
To install the php:8.1 module stream with the default profile, use:
The default common profile installs also the php-fpm package, and preconfigures PHP for use
with the Apache HTTP Server or nginx.
common - The default profile for server-side scripting using a web server. It includes the most
widely used extensions.
minimal - This profile installs only the command-line interface for scripting with PHP without
using a web server.
devel - This profile includes packages from the common profile and additional packages for
development purposes.
For example, to install PHP 8.1 for use without a web server, use:
Additional resources
19
Red Hat Enterprise Linux 9 Installing and using dynamic programming languages
You can run the PHP code using the FastCGI process server.
Prerequisites
Procedure
Or, if the Apache HTTP Server is already running on your system, restart the httpd service
after installing PHP:
5. To obtain information about your PHP settings, create the index.php file with the following
content in the /var/www/html/ directory:
http://<hostname>/
20
CHAPTER 6. USING THE PHP SCRIPTING LANGUAGE
Example 6.1. Running a "Hello, World!" PHP script using the Apache HTTP Server
# mkdir hello
2. Create a hello.php file in the /var/www/html/hello/ directory with the following content:
# <!DOCTYPE html>
<html>
<head>
<title>Hello, World! Page</title>
</head>
<body>
<?php
echo 'Hello, World!';
?>
</body>
</html>
http://<hostname>/hello/hello.php
Additional resources
Prerequisites
Procedure
21
Red Hat Enterprise Linux 9 Installing and using dynamic programming languages
Or, if the nginx server is already running on your system, restart the nginx service after
installing PHP:
5. To obtain information about your PHP settings, create the index.php file with the following
content in the /usr/share/nginx/html/ directory:
http://<hostname>/
Example 6.2. Running a "Hello, World!" PHP script using the nginx server
# mkdir hello
# <!DOCTYPE html>
<html>
<head>
<title>Hello, World! Page</title>
</head>
<body>
<?php
echo 'Hello, World!';
22
CHAPTER 6. USING THE PHP SCRIPTING LANGUAGE
?>
</body>
</html>
http://<hostname>/hello/hello.php
Additional resources
Prerequisites
Procedure
# php filename.php
Example 6.3. Running a "Hello, World!" PHP script using the command-line interface
1. Create a hello.php file with the following content using a text editor:
<?php
echo 'Hello, World!';
?>
# php hello.php
23
Red Hat Enterprise Linux 9 Installing and using dynamic programming languages
httpd.conf(5) — The manual page for httpd configuration, describing the structure and location
of the httpd configuration files.
nginx(8) — The manual page for the nginx web server containing the complete list of its
command-line options and list of signals.
php-fpm(8) — The manual page for PHP FPM describing the complete list of its command-line
options and configuration files.
24