How to create an RPM package from a tarball

Last updated on February 28, 2021 by Brian Nichols

Creating RPM package files can be both as easy or complicated as you desire. If you're needing to create an RPM package from a tarball (.tar.gz) that a vendor sent you, this tutorial will be beneficial for you. In this tutorial, I will describe a step-by-step procedure for building an RPM package from a tar file. The procedure includes creating the required directory structure, configuring a .spec file required for the rpmbuild process, and building and installing the RPM package. An additional step is added for those who may need to create more RPM packages in the future via a skeleton file.

We will be utilizing the rpmbuild command (included with rpm package) to build your RPM package(s).

Step One: Install rpm Package

First, install the tools needed for building your RPM package.

For Debian-based Linux:

$ sudo apt-get install rpm

For Red Hat-based Linux:

$ sudo yum install rpm

Step Two: Create Directory Structure and Land Your Source File

Next, create the appropriate directory structure needed for rpmbuild. If you did not create these manually, the rpmbuild process would create them anyway. The benefit of creating the directories now is that you can land your source file (the zipped tarball) in the correct directory before rpmbuild complains about it missing. I chose my home directory as the location for my rpmbuild location. Choose it as you see fit.

$ mkdir {~/rpmbuild,~/rpmbuild/BUILD,~/rpmbuild/BUILDROOT,~/rpmbuild/RPMS,~/rpmbuild/SOURCES,~/rpmbuild/SPECS,~/rpmbuild/SRPMS}

Next, move your source/zipped file into your new ~/rpmbuild/SOURCES directory.

$ mv ~/test-package-2.1.0.1.tar.gz ~/rpmbuild/SOURCES

Step Three: Utilize a Skeleton File for Reusability

Next, prepare to create your skeleton file - template.spec. This file will be the default file each time you create a .spec file for your new RPM package. Essentially with vim, if you have a certain file (bash script, python script, etc) that you're always creating with a certain layout/framework, you can create a skeleton (default) file for that extension. This makes your life a tad bit easier by filling in the same layout/content each time you build a new RPM package.

Create the directory where your skeleton file will reside.

$ sudo mkdir -p /usr/share/vim/vimfiles/

Then create a skeleton file with the following content.

$ sudo vim /usr/share/vim/vimfiles/template.spec
Name: 
Version: 
Release:  
Summary: 

Group: 
License: 
URL: 
Source0: 
BuildRoot:      %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)

BuildRequires:  
Requires:       

%description

%prep
%setup -q

%build
%configure
make %{?_smp_mflags}

%install
rm -rf $RPM_BUILD_ROOT
make install DESTDIR=$RPM_BUILD_ROOT

%clean
rm -rf $RPM_BUILD_ROOT

%files
%defattr(-,root,root,-)
%doc

%changelog

Next, to complete the skeleton implementation, you now need to update your user-specific vimrc file (~/.vimrc) (or whichever vim configuration file you prefer) to utilize the skeleton file we created earlier.

$ vim ~/.vimrc
if has("autocmd")
  augroup templates
    autocmd BufNewFile *.spec 0r /usr/share/vim/vimfiles/template.spec
  augroup END
endif

Step Four: Configuring Your RPM Package Spec File

Now let's begin creating/editing the .spec file that will be used to build your RPM package.

$ vim ~/rpmbuild/SPECS/test-package-2.1.0.1.spec

You will see the same information populated from your skeleton file we created earlier. The following information describes the items in more detail.

Here is an example of a basic .spec file.

Name: test-package
Version: 2
Release: 1.0.1
Summary: My test software/package
License: Vendor
Source0: %{name}-%{version}.%{release}.tar.gz
Buildroot: %{_tmppath}/%{name}/%{version}
Buildarch: x86_64

%description
My test software/package to land my newly built application and scripts.

%prep
# Create the RPM from the tar file
%setup -c -n %{name}-%{version}.%{release}-64bit

%install
# Create the new test-package directory
mkdir -p $RPM_BUILD_ROOT/usr/share/%{name}-%{version}.%{release}-64bit

# Copy the contents of the RPM into our new directory
cp -Rp $RPM_BUILD_DIR/%{name}-%{version}.%{release}-64bit/* $RPM_BUILD_ROOT/usr/share/%{name}-%{version}.%{release}-64bit

%clean
rm -rf $RPM_BUILD_ROOT
rm -rf $RPM_BUILD_DIR

%files
# Set the permissions/ownership of all files in your new directory as needed
%defattr(-,minecraftuser,minecraftuser,-)
/usr/share/%{name}-%{version}.%{release}-64bit

Step Five: Build and Install the RPM Package

Next, create the RPM package with your new spec file. Note that the -ba stands for "Build binary and source packages". You can use other options here if needed that will help during the rpmbuild process, such as -bp for "Executes the "%prep" stage from the spec file", or -bc for "Do the "%build" stage from the spec file (after doing the %prep stage)". These additional options will pause the rpmbuild process at certain points to help with troubleshooting. For our case, we will just build the RPM package.

$ rpmbuild -ba ~/rpmbuild/SPECS/test-package-2.1.0.1.spec

Now, install the RPM package. Since we aren't dealing with dependencies for this package, no need to check.

$ sudo rpm -ivh --nodeps ~/rpmbuild/RPMS/x86_64/test-package-2-1.0.1.x86_64.rpm

You can now check your final directory location for your installed RPM.

Troubleshooting

If any errors are output during the rpmbuild command, make sure to read the output for the specific issue that's occurring.

If you need to uninstall your package, check and ensure it is installed. And then uninstall it.

$ sudo rpm -qa | grep test-package
$ sudo rpm -e --nodeps test-package-2-1.0.1.x86_64

To conclude, I demonstrated how to lay down the appropriate foundation/directory structure for building an RPM package, creating a skeleton file for reusability, creating your .spec file, building the RPM package with the rpmbuild command, and finally installing the package.

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal or credit card

Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean