Transwiki:How to create a RPM package in Linux

How to create a RPM package in Linux

Creating basic installation RPM package is really easy. You just have to use rpmbuild command. rpmbuild will be applied to your source files and a RPM package will be created [all source files will be included in the RPM package]. Somewhere you need to specify the path of your source files. Just before creating the RPM package, your source files will be copied to a temporary location [termed as Build Root]. Actually, you need to write a spec file. rpmbuild will read that file to extract parameters and will act based on the parameter values.

Sample RPM Package

In the example, BuildRoot is assigned to: /var/tmp/%{name}-buildroot.
 * %install section is doing the building/creation of the RPM package.
 * From Summary to %description are the headers to specify different information as the names indicate.
 * %install section is creating the directories needed and copying your source files to BuildRoot directory (cp -r your_source_files_path/* /opt/xyz/$RPM_BUILD_ROOT/).
 * <tt>%files</tt> section specifies where rpm package will install your software.
 * <tt>%clean</tt> section just cleans the <tt>BuildRoot</tt> directory after the RPM package is created.

Just a note: As I mentioned Prefix to be /opt/xyz, I provided /opt/xyz in the paths. If you keep Prefix to be null, then you need to remove /opt/xyz from the paths. You can place shell commands under %pre section. These command will run just before the RPM package is being installed(using rpm -i command). Under %post section, you can also place some shell commands that will run immediately after the software installation(using rpm -i command). You can use %pre or %post section for operations like creating databases, users, and assigning permissions

This may not be 100% perfect. I did not test it on any system, but it should be almost ok.