RHEL 9.0 - Installing and Using Dynamic Programming Languages
RHEL 9.0 - Installing and Using Dynamic Programming Languages
A guide to installing and using dynamic programming languages 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
This document describes the basics of installing and using dynamic programming languages, such
as Python and PHP on Red Hat Enterprise Linux 9.
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
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6. . . . . . . . . . . . .
2.1. INSTALLING PYTHON 3 6
2.2. INSTALLING ADDITIONAL PYTHON 3 PACKAGES 6
2.3. INSTALLING ADDITIONAL PYTHON 3 TOOLS FOR DEVELOPERS 6
2.4. USING PYTHON 7
.CHAPTER
. . . . . . . . . . 3.
. . PACKAGING
. . . . . . . . . . . . . .PYTHON
. . . . . . . . . .3. RPMS
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8. . . . . . . . . . . . .
3.1. SPEC FILE DESCRIPTION FOR A PYTHON PACKAGE 8
3.2. COMMON MACROS FOR PYTHON 3 RPMS 10
3.3. USING AUTOMATICALLY GENERATED DEPENDENCIES FOR PYTHON RPMS 11
.CHAPTER
. . . . . . . . . . 4.
. . .HANDLING
. . . . . . . . . . . .INTERPRETER
. . . . . . . . . . . . . . .DIRECTIVES
. . . . . . . . . . . . .IN
. . .PYTHON
. . . . . . . . . SCRIPTS
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
..............
4.1. MODIFYING INTERPRETER DIRECTIVES IN PYTHON SCRIPTS 12
. . . . . . . . . . . 5.
CHAPTER . . USING
. . . . . . . .THE
. . . . .PHP
. . . . .SCRIPTING
. . . . . . . . . . . .LANGUAGE
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
..............
5.1. INSTALLING THE PHP SCRIPTING LANGUAGE 14
5.2. USING THE PHP SCRIPTING LANGUAGE WITH A WEB SERVER 14
5.2.1. Using PHP with the Apache HTTP Server 14
5.2.2. Using PHP with the nginx web server 16
5.3. RUNNING A PHP SCRIPT USING THE COMMAND-LINE INTERFACE 17
5.4. ADDITIONAL RESOURCES 18
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
1. Make sure you are viewing the documentation in the Multi-page HTML format. In addition,
ensure you see the Feedback button in the upper right corner of the document.
2. Use your mouse cursor to highlight the part of text that you want to comment on.
3. Click the Add Feedback pop-up that appears below the highlighted text.
3. Fill in the Description field with your suggestion for improvement. Include a link to the
relevant part(s) of 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.
In the future, additional versions of Python 3 will be distributed as RPM packages with a shorter life
cycle through the AppStream repository. These versions will be installable in parallel with Python 3.9.
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.
The python command is intended for interactive sessions. In production, Red Hat recommends using
python3 or python3.9 explicitly.
You can uninstall the unversioned python command by using the dnf remove /usr/bin/python
command.
If you need a different python command, you can create custom symlinks in /usr/local/bin or
~/.local/bin or 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.
5
Red Hat Enterprise Linux 9 Installing and using dynamic programming languages
Procedure
Verification steps
To verify the Python version installed on your system, use the following command:
$ python3 --version
Procedure
This repository contains, for example, the python3-pytest, python3-Cython packages and many others.
IMPORTANT
The CodeReady Linux Builder repository and its content is unsupported by Red Hat.
To install packages from the repository, use the following the procedure.
Procedure
6
CHAPTER 2. INSTALLING AND USING PYTHON
Additional resources
How to enable and make use of content within CodeReady Linux Builder
Prerequisites
Procedure
$ python3
$ python3 -m pip --help
$ python3 -m pip install package
7
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- 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 1
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
BuildArch: noarch
BuildRequires: python3-devel 2
8
CHAPTER 3. PACKAGING PYTHON 3 RPMS
BuildRequires: python3-setuptools
%description %_description
%package -n python3-pello 3
Summary: %{summary}
%prep
%autosetup -p1 -n Pello-%{version}
%build
# The macro only supported projects with setup.py
%py3_build 4
%install
# The macro only supported projects with setup.py
%py3_install
%check 5
%{pytest}
# Note that there is no %%files section for the unversioned python module
%files -n python3-pello
%doc README.md
%license LICENSE.txt
%{_bindir}/pello_greeting
1 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 thus the name of the Source RPM (SRPM)is
python-pello.
2 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 and the relevant projects needed by the specific software you package, for
example python3-setuptools or the runtime and testing dependencies needed to run the tests in
9
Red Hat Enterprise Linux 9 Installing and using dynamic programming languages
3 When choosing a name for the binary RPM (the package that users will be able to install), add a
versioned Python prefix, which is currently python3-. Therefore, the resulting binary RPM will be
named python3-pello.
4 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.
5 The %check section should run the tests of the packaged project. The exact command depends
very much on the project itself, but it is possible to use the %pytest macro to run the pytest
command in an RPM-friendly way. The %{python3} macro contains a path for the Python 3
interpreter, that is, /usr/bin/python3. We recommend always using the macro rather than a literal
path.
10
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
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
11
Red Hat Enterprise Linux 9 Installing and using dynamic programming languages
#!/usr/bin/python3
#!/usr/bin/python3.9
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
Python scripts matching the pattern ^[a-zA-Z0-9_]+\.py$, not only those with an ambiguous
12
CHAPTER 4. HANDLING INTERPRETER DIRECTIVES IN PYTHON SCRIPTS
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 example above with
a path for the interpreter directive, such as /usr/bin/python3.
Additional resources
Interpreter invocation
13
Red Hat Enterprise Linux 9 Installing and using dynamic programming languages
Procedure
This section describes how to 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
14
CHAPTER 5. USING THE PHP SCRIPTING LANGUAGE
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>/
Example 5.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
15
Red Hat Enterprise Linux 9 Installing and using dynamic programming languages
Additional resources
Setting up the Apache HTTP web server
Prerequisites
Procedure
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>/
16
CHAPTER 5. USING THE PHP SCRIPTING LANGUAGE
Example 5.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!';
?>
</body>
</html>
http://<hostname>/hello/hello.php
Additional resources
Prerequisites
Procedure
# php filename.php
17
Red Hat Enterprise Linux 9 Installing and using dynamic programming languages
Example 5.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
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.
18