Installing and using dynamic programming languages
Installing and using Python and PHP in Red Hat Enterprise Linux 10
초록
Providing feedback on Red Hat documentation 링크 복사링크가 클립보드에 복사되었습니다!
We appreciate your feedback on our documentation. Let us know how we can improve it.
Submitting feedback through Jira (account required)
- Log in to the Jira website.
- Click Create in the top navigation bar
- Enter a descriptive title in the Summary field.
- Enter your suggestion for improvement in the Description field. Include links to the relevant parts of the documentation.
- Click Create at the bottom of the dialogue.
1장. Installing and using Python 링크 복사링크가 클립보드에 복사되었습니다!
Python is a high-level programming language that supports multiple programming paradigms, such as object-oriented, imperative, functional, and procedural paradigms. Python has dynamic semantics and can be used for general-purpose programming.
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.
1.1. Python versions 링크 복사링크가 클립보드에 복사되었습니다!
Python 3.12 is the default Python implementation in RHEL 10. Python 3.12 is distributed in a non-modular python3 RPM package in the BaseOS repository and is usually installed by default. Python 3.12 will be supported for the whole life cycle of RHEL 10.
Additional versions of Python 3 will be distributed as non-modular RPM packages with a shorter life cycle through the AppStream repository in minor RHEL 10 releases. You will be able to install these additional Python 3 versions in parallel with Python 3.12.
The unversioned python command points to the default Python 3.12 version.
For details about the length of support, see Red Hat Enterprise Linux Life Cycle and Red Hat Enterprise Linux Application Streams Life Cycle.
1.2. Installing Python 3 링크 복사링크가 클립보드에 복사되었습니다!
The default Python implementation is usually installed by default. To install it manually, use the following procedure.
Procedure
To install Python 3.12, enter:
# dnf install python3
Verification
Verify the Python version installed on your system:
$ python3 --version
1.3. Installing additional Python 3 packages 링크 복사링크가 클립보드에 복사되었습니다!
Packages prefixed with python3- contain add-on modules for the default Python 3.12 version.
Procedure
To install, for example, the
Requestsmodule for Python 3.12, enter:# dnf install python3-requestsTo install the
pippackage installer from Python 3.12, enter:# dnf install python3-pip
1.4. Installing additional Python 3 tools for developers 링크 복사링크가 클립보드에 복사되었습니다!
Additional Python tools for developers are distributed through the CodeReady Linux Builder (CRB) repository.
The content in the CodeReady Linux Builder repository is unsupported by Red Hat.
The CRB repository contains, for example, the following packages:
-
python3-pytest -
python3-idle -
python3-debug -
python3-cython
Not all upstream Python-related packages are available in RHEL.
Procedure
Enable the CodeReady Linux Builder repository:
# subscription-manager repos --enable codeready-builder-for-rhel-10-x86_64-rpmsInstall, for example, the
python3-cythonpackage:# dnf install python3-cython
1.5. Using Python 링크 복사링크가 클립보드에 복사되었습니다!
The following procedure contains examples of running the Python interpreter or Python-related commands.
Prerequisites
- Python is installed.
If you want to download and install third-party applications, install the
python3-pippackage.주의Installing Python packages with
pipas the root user places files in system locations. This can override RHEL libraries and might cause system instability or conflicts with supported packages. Red Hat does not support software installed by usingpipat the system level. To avoid these issues, usepipwithin a virtual environment or install packages as a non-root user with the--useroption.
Procedure
To run the Python 3.12 interpreter or related commands, use, for example, the following commands:
$ python3 $ python3 -m venv --help $ python3 -m pip install <package> $ pip3 install <package>
2장. Packaging Python 3 RPMs 링크 복사링크가 클립보드에 복사되었습니다!
You can install Python packages on your system by using the DNF package manager. DNF uses the RPM package format, which offers more downstream control over the software.
Packaging a Python project into an RPM package 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 contents of RPM packages with the rest of the operating system.
- You can execute tests during the build process.
The packaging format of native Python packages is defined by Python Packaging Authority (PyPA) Specifications. Historically, most Python projects used 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:
-
To package Python software that uses the
setup.pyfile, follow this document. -
To package more modern packages with
pyproject.tomlfiles, see theREADMEfile in pyproject-rpm-macros. Note thatpyproject-rpm-macrosis included in the CodeReady Linux Builder (CRB) repository, which contains unsupported packages, and it can change over time to support newer Python packaging standards.
2.1. A spec file description for an example Python package 링크 복사링크가 클립보드에 복사되었습니다!
An RPM spec file for Python projects has some specifics compared to non-Python RPM spec files.
Note that it is recommended for any RPM package name of a Python library to include the python3- prefix.
See the notes about Python RPM spec files specifics in the following example of the python3-pello package.
- An example SPEC file for the pello program written in Python
%global python3_pkgversion 3
Name: python-pello
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: python%{python3_pkgversion}-devel
# Build dependencies need to be specified manually
BuildRequires: python%{python3_pkgversion}-setuptools
# Test dependencies need to be specified manually
# Runtime dependencies need to be BuildRequired manually to run tests during build
BuildRequires: python%{python3_pkgversion}-pytest >= 3
%global _description %{expand:
Pello is an example package with an executable that prints Hello World! on the command line.}
%description %_description
%package -n python%{python3_pkgversion}-pello
Summary: %{summary}
%description -n python%{python3_pkgversion}-pello %_description
%prep
%autosetup -p1 -n Pello-%{version}
%build
# The macro only supports projects with setup.py
%py3_build
%install
# The macro only supports projects with setup.py
%py3_install
%check
%pytest
# Note that there is no %%files section for python-pello
%files -n python%{python3_pkgversion}-pello
%doc README.md
%license LICENSE.txt
%{_bindir}/pello_greeting
# The library files needed to be listed manually
%{python3_sitelib}/pello/
# The metadata files needed to be listed manually
%{python3_sitelib}/Pello-*.egg-info/
- 1
- By defining the
python3_pkgversionmacro, you set which Python version this package will be built for. To build for the default Python version3.12, remove the line. - 2
- When packaging a Python project into RPM, always add the
python-prefix to the original name of the project. The project name here isPelloand, therefore, the name of the Source RPM (SRPM) ispython-pello. - 3
BuildRequiresspecifies what packages are required to build and test this package. InBuildRequires, always include items providing tools necessary for building Python packages:python3-develand the relevant projects needed by the specific software that you package, for example,python3-setuptoolsor the runtime and testing dependencies needed to run the tests in the%checksection.- 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.12. You can use the%{python3_pkgversion}macro, which evaluates to3for the default Python version3.12unless you set it to an explicit version, for example, when a later version of Python is available (see footnote 1). - 5
- The
%py3_buildand%py3_installmacros run thesetup.py buildandsetup.py installcommands, respectively, with additional arguments to specify installation locations, the interpreter to use, and other details.참고Using the
setup.py buildandsetup.py installcommands from thesetuptoolspackage is deprecated and will be removed in the future major RHEL release. You can use pyproject-rpm-macros instead. - 6
- The
%checksection runs the tests of the packaged project. The exact command depends on the project itself, but you can use the%pytestmacro to run thepytestcommand in an RPM-friendly way.
2.2. Common macros for Python 3 RPMs 링크 복사링크가 클립보드에 복사되었습니다!
In a Python RPM spec file, always use the macros for Python 3 RPMs rather than hardcoding their values.
You can redefine which Python 3 version is used in these macros by defining the python3_pkgversion macro on top of your spec file. For more information, see A spec file description for an example Python package. If you define the python3_pkgversion macro, the values of the macros described in the following table will reflect the specified Python 3 version.
| Macro | Normal Definition | Description |
|---|---|---|
| %{python3_pkgversion} | 3 | The Python version that is used by all other macros. Can be redefined to any future Python versions that will be added. |
| %{python3} | /usr/bin/python3 | The Python 3 interpreter. |
| %{python3_version} | 3.12 | The major.minor version of the Python 3 interpreter. |
| %{python3_sitelib} | /usr/lib/python3.12/site-packages | The location where pure-Python modules are installed. |
| %{python3_sitearch} | /usr/lib64/python3.12/site-packages | The location where modules containing architecture-specific extension modules are installed. |
| %py3_build |
Expands to the | |
| %py3_install |
Expands to the | |
| %{py3_shebang_flags} | sP |
The default set of flags for the Python interpreter directives macro, |
| %py3_shebang_fix |
Changes Python interpreter directives to |
2.3. Using automatically generated dependencies for Python RPMs 링크 복사링크가 클립보드에 복사되었습니다!
You can automatically generate dependencies for Python RPMs by using upstream-provided metadata.
Prerequisites
-
A
specfile for the RPM exists. For more information, see A spec file description for an example Python package.
Procedure
Include one of the following directories in the resulting RPM:
-
.dist-info .egg-infoThe RPM build process automatically generates virtual
pythonX.Ydistprovides from these directories, for example:python3.12dist(pello)The Python dependency generator then reads the upstream metadata and generates runtime requirements for each RPM package using the generated
pythonX.Ydistvirtual provides. Example of a generated requirements tag:Requires: python3.12dist(requests)
-
-
Inspect the generated
Requires. -
To remove some of the generated
Requires, modify the upstream-provided metadata in the%prepsection of thespecfile. -
To disable the automatic requirements generator, include the
%{?python_disable_dependency_generator}macro above the main package’s%descriptiondeclaration.
3장. Installing Tcl/Tk 링크 복사링크가 클립보드에 복사되었습니다!
Tcl is a dynamic programming language, while Tk is a graphical user interface (GUI) toolkit. They provide a powerful and easy-to-use platform for developing cross-platform applications with graphical interfaces. As a dynamic programming language, 'Tcl' provides simple and flexible syntax for writing scripts. The tcl package provides the interpreter for this language and the C library. You can use Tk as GUI toolkit that provides a set of tools and widgets for creating graphical interfaces. You can use various user interface elements such as buttons, menus, dialog boxes, text boxes, and canvas for drawing graphics. Tk is the GUI for many dynamic programming languages.
For more information about Tcl/Tk, see the Tcl/Tk manual or Tcl/Tk documentation web page.
3.1. Installing Tcl 링크 복사링크가 클립보드에 복사되었습니다!
The default Tcl implementation is usually installed by default. To install it manually, use the following procedure.
Procedure
To install
Tcl, use:# dnf install tcl
Verification
To verify the Tcl version installed on your system, run the interpreter
tclsh:$ tclshIn the interpreter run this command:
% info patchlevel 8.6- You can exit the interpreter interface by pressing Ctrl+C
3.2. Installing Tk 링크 복사링크가 클립보드에 복사되었습니다!
The default Tk implementation is usually installed by default. To install it manually, use the following procedure.
Procedure
To install
Tk, use:# dnf install tk
Verification
To verify the
Tkversion installed on your system, run the window shellwish. You need to be running a graphical display:$ wishIn the shell run this command:
% puts $tk_version 8.6- You can exit the interpreter interface by pressing Ctrl+C
4장. Using the PHP scripting language 링크 복사링크가 클립보드에 복사되었습니다!
Hypertext Preprocessor (PHP) is a general-purpose scripting language mainly used for server-side scripting. You can use PHP to run the PHP code by using a web server.
4.1. Installing the PHP scripting language 링크 복사링크가 클립보드에 복사되었습니다!
In RHEL 10, PHP is available in PHP 8.3 as the php RPM package.
Procedure
To install PHP 8.3, enter:
# dnf install php
Additional resources
4.2. Using the PHP scripting language with a web server 링크 복사링크가 클립보드에 복사되었습니다!
4.2.1. Using PHP with the Apache HTTP Server 링크 복사링크가 클립보드에 복사되었습니다!
In Red Hat Enterprise Linux 10, the Apache HTTP Server allows you to run PHP as a FastCGI process server. FastCGI Process Manager (FPM) is an alternative PHP FastCGI daemon that allows a website to manage high loads. PHP uses FastCGI Process Manager by default in RHEL 10.
Prerequisites
- The PHP scripting language is installed on your system.
Procedure
Configure the
Apache HTTP Serverandphp-fpmto run PHP scripts.Install the
httpdpackage:# dnf install httpdStart the
Apache HTTP Server:# systemctl start httpdOr, if the
Apache HTTP Serveris already running on your system, restart thehttpdservice after installing PHP:# systemctl restart httpdStart the
php-fpmservice:# systemctl start php-fpmOptional: Enable both services to start at boot time:
# systemctl enable php-fpm httpdTo obtain information about your PHP settings, create the
index.phpfile with the following content in the/var/www/html/directory:# echo '<?php phpinfo(); ?>' > /var/www/html/index.phpTo run the
index.phpfile, point the browser to:http://<hostname>/Optional: Adjust configuration if you have specific requirements:
-
/etc/httpd/conf/httpd.conf- generichttpdconfiguration -
/etc/httpd/conf.d/php.conf- PHP-specific configuration forhttpd -
/usr/lib/systemd/system/httpd.service.d/php-fpm.conf- by default, thephp-fpmservice is started withhttpd -
/etc/php-fpm.conf- FPM main configuration -
/etc/php-fpm.d/www.conf- defaultwwwpool configuration
-
Run the "Hello, World!" PHP script using the Apache HTTP Server.
Create a
hellodirectory for your project in the/var/www/html/directory:# mkdir helloCreate a
hello.phpfile 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>Start the
Apache HTTP Server:# systemctl start httpdTo run the
hello.phpfile, point the browser to:http://<hostname>/hello/hello.phpAs a result, a web page with the "Hello, World!" text is displayed.
See
httpd(8),httpd.conf(5), andphp-fpm(8)man pages for more information.
4.2.2. Using PHP with the nginx web server 링크 복사링크가 클립보드에 복사되었습니다!
You can run PHP code through the nginx web server.
Prerequisites
- The PHP scripting language is installed on your system.
Procedure
Configure the
nginxweb server andphp-fpmto run PHP scripts.Install the
nginxpackage:# dnf install nginxStart the
nginxserver:# systemctl start nginxOr, if the
nginxserver is already running on your system, restart thenginxservice after installing PHP:# systemctl restart nginxStart the
php-fpmservice:# systemctl start php-fpmOptional: Enable both services to start at boot time:
# systemctl enable php-fpm nginxTo obtain information about your PHP settings, create the
index.phpfile with the following content in the/usr/share/nginx/html/directory:# echo '<?php phpinfo(); ?>' > /usr/share/nginx/html/index.phpTo run the
index.phpfile, point the browser to:http://<hostname>/Optional: Adjust configuration if you have specific requirements:
-
/etc/nginx/nginx.conf-nginxmain configuration -
/etc/nginx/conf.d/php-fpm.conf- FPM configuration fornginx -
/etc/php-fpm.conf- FPM main configuration -
/etc/php-fpm.d/www.conf- defaultwwwpool configuration
-
Run the "Hello, World!" PHP script using the nginx server.
Create a
hellodirectory for your project in the/usr/share/nginx/html/directory:# mkdir helloCreate a
hello.phpfile in the/usr/share/nginx/html/hello/directory with the following content:# <!DOCTYPE html> <html> <head> <title>Hello, World! Page</title> </head> <body> <?php echo 'Hello, World!'; ?> </body> </html>Start the
nginxserver:# systemctl start nginxTo run the
hello.phpfile, point the browser to:http://<hostname>/hello/hello.phpAs a result, a web page with the "Hello, World!" text is displayed.
See
nginx(8)andphp-fpm(8)man pages for more information.
4.3. Running a PHP script using the command line 링크 복사링크가 클립보드에 복사되었습니다!
A PHP script is usually run using a web server, but also can be run using the command line.
Prerequisites
- The PHP scripting language is installed on your system.
Procedure
Open a text editor and create a PHP file named
hello.phpwith the following content:<?php echo 'Hello, World!'; ?>-
Open a terminal and navigate to the directory containing
hello.php. Run the PHP script from the command line:
$ php hello.php Hello, World!See
php(1)man pages for more information.