Building Linux RPM Package from Source Code

Sometimes for some reasons you might not be able to find RPM package that you needed on the Internet. In that case you have to build the binary by your own. Certainly an easy way is just build the binary and install it, but a better way is to build a RPM package so that it is distributable and can be under Linux package management. So below is the steps for building RPM package from source code, referencing from https://gist.github.com/fernandoaleman/1376973 with some changes.

# Step: 1
# Install rpmbuild

yum install rpm-build

# Step: 2
# Create rpmbuild folders in your home directory

mkdir -p ~/rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS}

# Step: 3
# Download the source file

wget -P ~/rpmbuild/SOURCES "http://git-core.googlecode.com/files/git-1.7.7.3.tar.gz"
tar -xzvf ~/rpmbuild/SOURCES/git-1.7.7.3.tar.gz -C /tmp

# Step: 4
# Prepare the .spec file, and made any necessary changes
#
# If the source file did not include .spec file, you have to prepare one by your own
ls -l /tmp/git-1.7.7.3/*.spec
cp -p /tmp/git-1.7.7.3/git.spec ~/rpmbuild/SPECS
vim ~/rpmbuild/SPECS/git.spec

# Step: 5
# Build rpm
#
# If you get any errors during build, it is usually because of dependencies. Simply
# install the dependencies with `yum install [dependency]` and run rpmbuild again.

rpmbuild -ba --define "_topdir %(echo $HOME)/rpmbuild" ~/rpmbuild/SPECS/git.spec

You can also find the above code at https://gist.github.com/VeryCrazyDog/1d1fef411d9e20f4c12b