Preface
Recently, because a server is restricted by the system, it can only be uploaded to use it. Our services are all files, so how can we type all files into rpm packages? ? ? I have also found several of them and finally found a simple and easy-to-use packaging method. Let me explain the deployment, packaging and installation to you below;
This article has used the docker offline package directory as an example, and type it into a rpm package and install it on a new server;
Operating system is: Centos 7.6
Before using rpmbuild to package the Python environment into an RPM package, you need to make sure that the rpmbuild tool is installed and the relevant RPM packaging environment is configured. The following are the specific steps and instructions:
1. Prepare for packaging environment
First, make sure that rpmbuild is installed, which can be installed via the following command:
sudo yum install rpm-build -y
Create in your home directoryrpmbuild
Directory structure:
mkdir -p ~/rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS}
2. Create a SPEC file in a Python environment
exist~/rpmbuild/SPECS
Create a directory.spec
Files used to define relevant information about RPM packages, such as. Here is a basic one
spec
File template:
Name: python-env Version: 1.0 Release: 1%{?dist} Summary: Python Environment Package License: YourLicense Source0: %{name}-%{version}. %description This package provides a Python environment including dependencies. %prep # Here you can unzip the environment package %build # The build operations required for packaging can be defined here %install # Assume that you have packaged the Python environment as a . file, extract it to /opt/python/mkdir -p %{buildroot}/opt/python/ tar -xzvf %{SOURCE0} -C %{buildroot}/opt/python/ %files /opt/python/* %changelog * Tue Nov 12 2024 Your Name <youremail@> - 1.0-1 - Initial package for Python environment
The following is a detailed analysis of the configuration content of this RPM SPEC file:
Basic configuration information
-
Name:
python-env
- The name of the RPM package is defined, specified here as
python-env
。
- The name of the RPM package is defined, specified here as
-
Version:
1.0
- Specifies the version number of the RPM package. In subsequent version updates, this number can be adjusted as needed.
Release:
1%{?dist}
- Defines the release version of the RPM package.
1
It means the first release,%{?dist}
The current release version distribution tag will be automatically added (such as.el7
Indicates that it applies to CentOS 7).
- Defines the release version of the RPM package.
-
Summary:
Python Environment Package
- This is a brief description for summarizing the contents of the RPM package.
License:
YourLicense
- Specified the license for the package. Here it should be replaced with the actual license name (such as
MIT
、GPL
wait).
- Specified the license for the package. Here it should be replaced with the actual license name (such as
-
Source0:
%{name}-%{version}.
- Defines the name of the source code file, usually the same name as the SPEC file, and the format is
。
%{name}
and%{version}
Will be replaced bypython-env
and1.0
, so the actual value ispython-env-1.
。
- Defines the name of the source code file, usually the same name as the SPEC file, and the format is
Detailed description of the package
-
%description
- This field provides a detailed description of the package and will be displayed in the RPM information. This is explained here that this package contains the Python environment and its dependencies.
Build and Installation Process
-
%prep
- This section is used to prepare the build environment and usually performs some decompression or preprocessing operations. In this example, the comment mentions that the environment package can be decompressed here, but there is no actual command.
-
%build
- Defines the actions that need to be performed during the build process, such as compiling code. But for packages that do not require compilation, this part can be empty.
-
%install
- During the installation phase, the operation to install the contents to the target directory is specified.
-
mkdir -p %{buildroot}/opt/python/
: Create a target directory%{buildroot}/opt/python/
,in%{buildroot}
It is the temporary installation path of RPM. -
tar -xzvf %{SOURCE0} -C %{buildroot}/opt/python/
:WillSource0
(Right nowpython-env-1.
) Unzip into the installation path to include the full Python environment.
Included files
-
%files
- Specifies which files are included in the installation package.
-
/opt/python/*
:Will/opt/python/
All content in is added to the final generated RPM package.
Change log
-
%changelog
- Record the change history of the package. Each log includes date, author, version and update instructions. In this example:
- The date is
November 12, 2024
- The author is
Your Name <youremail@>
- The update description is "Initial package for Python environment", indicating the first package.
- The date is
- Record the change history of the package. Each log includes date, author, version and update instructions. In this example:
The purpose of this SPEC file is to package the Python environment into an RPM, which will be decompressed to the specified directory during installation, and can be easily installed on other systems.
3. Prepare the Python environment
Can be usedvirtualenv
orvenv
To create a Python virtual environment, install the required libraries, and then package the entire environment into.
Files, for example:
cd /path/to/python-env tar -czvf python-env-1. . mv python-env-1. ~/rpmbuild/SOURCES/
4. Build RPM package
Use the following command inrpmbuild
Create RPM packages in the environment:
rpmbuild -ba ~/rpmbuild/SPECS/
After execution, the generated RPM package will be saved in~/rpmbuild/RPMS/noarch/
in the directory.
noarch
It is generated based on your system, x86 isx86_64
。
5. Install and uninstall RPM packages
First, upload the RPM package you have to hit to the server;
5.1 Install rpm package
The default installation path is the path in the SPEC file you wrote [this question is /opt/python]
rpm -ivh python-env-1.
5.2 Find the rpm package just installed
rpm -qa | grep python-env-1.0-1.x86_64
5.3 Uninstall the newly installed rpm package
rpm -e python-env-1.0-1.x86_64
This is the article about how to implement Linux to type files or directories into rpm packages. For more related contents of Linux to type files into rpm packages, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!