Building RPMS

Sometimes you want to customize an existing rpm. For example, in 2017, firefox on Fedora and some others requires pulseaudio. This can be changed by rebuilding its rpm. Other times, something like RHEL9 comes out, and you find that one of your favorite packages isn't included.

This is a very simplified quick start to setting up rpmbuilds. For more detail about working with spec files, see the Fedora project's article.

There are now tools that make it easier than it used to be. Old articles talk about creating your $HOME/.rpmacros and build tree, but newer tools automate this. I'm going to be using AlmaLinux, a RedHat clone for my examples.

First we need some basic build tools. I find that, on a minimal install (which sometimes doesn't include tar and bzip2) that the following (usually) suffice.
dnf -y install gcc gcc-c++ make automake kernel-devel tar bzip2 rpm-build
rpmdevtools dnf-plugins-core mock

The Fedora project's page says to add your user to mock. To avoid errors later (when installing srpms), add a mockbuild user and mock group. The mockbuild user doesn't need a password. More importantly, doing anything as root when it's not needed, means risking your system with one typo. So, take the few seconds and create the mock user and group.
sudo useradd mockbuild
sudo groupadd mock
sudo usermod -a -G mock <username>

Next create the build environment.
rpmdev-setuptree

Now we can get the srpm. I'll use tint2 for this example, a task bar which I use with openbox, and isn't in the Alma or EPEL repos. To get the srpm there are various, mostly untested by me, suggestions, such as creating an srpm repo (which some people find to not work). For me, especially when building rpms for Alma9 I just go to rpmfind, and do a search. It will have a link to download the srpm. For tint2, I use the srpm for Fedora 34, as it's supposedly close in nature to RHEL9. I download it to $HOME/rpmbuild/SRPMS.

If you cd to ~/rpmbuild/SRPMS, you'll see a tint2.srpm file for Fedora34. Install it with rpm -ivh tint2-<version>.srpm, or possibly .src.rpm. (This is why we created the mockbuild user, if there is no such user, one gets errors like, Can't find mockbuild, using root.) You can install it as your user, it automatically uses the mockbuild user and mock group.

Once you've installed the source rpm, you will have a tint2.spec in ~/rpmbuild/SPECS.

Check for dependencies. While in the SPECS directory
sudo dnf -y builddep tint2.spec

In this case I know it will have all I need. If I build feh, however, a small image-viewer, I would get an error that some packages were not found. If I look at the output, it will tell me there are no matching packages for perl-Test-Command. So, in that case, I would go back to rpmfind, download the perl-Test-Command srpm, and repeat the process of installing the srpm, and running builddep. After that, (I'll go over this below), one runs rpmbuild -ba perl-Test-Command.spec, which produces an rpm that you can install. Then you can go back to dnf -y builddep feh.spec.

However, we're still on tint2. Once it's pulled in everything it needs, you can build the rpm. Still in the SPECS directory
rpmbuild -ba tint2.spec

This may take awhile. When it completes, it will create ~/rpmbuild/RPM/x86_64/tint2-<version>.rpm. It will also create a few other rpms but you don't need them.
sudo rpm -ivh ~/rmpbuild/RPMS/x86_64/tint2-<version>.rpm 

You now have tint2.

Going back to feh, when you run rpmbuild -ba perl-Test-Command.spec, you'll see that you have a $HOME/rpmbuild/RPMS/noarch directory. On an x86_64 system most packages will be created under RPMS/x86_64, but there may be some under this noarch subdirectory, including the perl-Test-Command rpm.

Lastly a quich mention of editing spec files. Another program I like, weechat, is also not yet available in any repos. Once again, I can pull the srpm from rpmfind. Once I install the srpm, I find that it can't find a package for guile22-devel. However, RHEL9 does have a package for guile30-devel. Edit the spec file, changing the BuildRequires guile22-devel with BuildRequires guile30-devel. If you now run dnf -y builddep, it will pull in all needed dependencies.