Create New Debian Package From Scratch (Extra Tips for PHP Pear Modules)

Pear often has alot of packages that are not yet in the debian repository… It make sense to create new pear-debian packages just to follow the protocol.

Firstly, you will have to make sure that the package that you are trying to create is not yet available in the repository else you will be re-inventing the wheel. Next, decide a name for the package. The name must be unique and you can check it easily using

dpkg -S ${pkgName}

Rename the source directory containing the installation files as ${PackageName}-${PackageVersion}. Next, tar and compress the source directory and name it ${PackageName}-${PackageVersion}.tar.gz

Create a ‘deb’ dir under your home directory. Then, transfer the entire source directory and .gz file over.

If you are trying to create a pear module, you need to download and extract the source files from http://pear.php.net/packages.php. Do a fresh tar following the naming convention ${PackageName}-${PackageVersion}.tar.gz. For example, the original downloaded pear package is HTML_AJAX-0.5.2.tgz but I want my new package to be named pear-html-ajax-0.5.2, I would do this:

cd ~/deb
cp ../HTML_AJAX-0.5.2.tgz ./
tar -xzvf HTML_AJAX-0.5.2.tgz
rm HTML_AJAX-0.5.2.tgz
mv HTML_AJAX-0.5.2 pear-html-ajax-0.5.2
tar -czvf pear-html-ajax-0.5.2.tar.gz pear-html-ajax-0.5.2

If everything seems ok, your directory structure should be something like this:

- deb
- pear-html-ajax-0.5.2
+ AJAX
+ docs
+ examples
+ js
+ tests
AJAX.php
package.xml
pear-html-ajax-0.5.2.tar.gz

Install the dh-make package. Then, in the package dir (in this case, pear-html-ajax-0.5.2), run

dh_make -e yourname@infoxchange.net.au -f ../${pkgName.tar.gz}

* note the extra debian dir and orig.tar.gz file created
* consult man dh-make and man debhelper if necessary

- deb
- pear-html-ajax-0.5.2
+ AJAX
+ debian
+ docs
+ examples
+ js
+ tests
AJAX.php
package.xml
pear-html-ajax_0.5.2.orig.tar.gz
pear-html-ajax-0.5.2.tar.gz

The debian dir contains important information about the installation process. cd debian and you will see many files. The .ex files are sample templates and can usually be ignored.

* changelog – As the name implies.
* copyright – As the name implies.
* compat – Set the rules under which debhelper scripts (dh_*) operates. (no modification needed)
* control – Contains important installation parameters. Change the following fields: section, depends and description.
* dirs – directory where the files will be installed.
* rules – Rules for the installation procedure. This is the hardcore part. Major changes needed.

If you are creating a pear package, you need to move package.xml into the package dir (in this case, pear-html-ajax-0.5.2). I have created a sample pear rule and you can copy and paste it into your rule file just to save time (see below).

So when you’re ready to try creating a package, do:

cd ~/deb/$your_new_directory
dpkg-buildpackage -rfakeroot (or sudo dpkg-buildpackage)

The dpkg-buildpackage command will try to create a .deb package in the directory above your current directory, using the files in the ‘debian’ directory inside your current directory.

A proper install will include some other files other than the .deb file.

- deb
+ pear-html-ajax-0.5.2
pear-html-ajax_0.5.2.orig.tar.gz
pear-html-ajax_0.5.2-1.diff.gz
pear-html-ajax_0.5.2-1.dsc
pear-html-ajax_0.5.2-1_i386.changes
pear-html-ajax_0.5.2-1_i386.deb
pear-html-ajax-0.5.2.tar.gz

* see http://www.debian.org/doc/maint-guide/ch-build.en.html

If that seems to complete properly, you should be able to try installing it on your own machine with:

cd ~/deb
dpkg -i ${your_new_package}.deb

Check that all the files have been installed in the right place. Eg, all pear files should go to /usr/share/php.

Also note that there are other ways of creating debian packages… this is the standard way.

——- SAMPLE RULES FILE FOR PEAR —–

#!/usr/bin/make -f

# IX custom script for making pear modules into debian package. you need to have php-pear installed first.

PEAR ?= /usr/bin/pear

# Your package name. Make sure it is consistent with other files.
package = pear-sample
configure: configure-stamp
configure-stamp:
dh_testdir
touch configure-stamp
build: build-stamp
build-stamp: configure-stamp
dh_testdir
touch build-stamp

clean:
dh_clean build-stamp configure-stamp

install: build
dh_testdir
dh_testroot
dh_clean -k
dh_installdirs

# Add here commands to install the package into debian/package.
# if the script does not work test with this line and see error
$(PEAR) install -n -R debian/$(package) package.xml;
# remove unwanted pear files left by installation
rm -f debian/$(package)/usr/share/php/.filemap;
rm -f debian/$(package)/usr/share/php/.lock;
rm -rf debian/$(package)/usr/share/php/.channels;
rm -rf debian/$(package)/usr/share/php/.depdblock;
rm -rf debian/$(package)/usr/share/php/.depdb;
rm -rf debian/$(package)/usr/share/php/.registry/.channel.pecl.php.net;
rm -rf debian/$(package)/usr/share/php/.registry/.channel.__uri;

# remove duplicated files, these files are in /usr/share/doc/package
rm -rf debian/$(package)/usr/share/php/tests;
rm -rf debian/$(package)/usr/share/php/docs;

# remove created tmp dir
rm -rf debian/$(package)/tmp

# Build architecture-independent files here.

binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
dh_testdir
dh_testroot
dh_installdocs
dh_installexamples
dh_installchangelogs
dh_compress
dh_fixperms
dh_installdeb
dh_gencontrol
dh_md5sums
dh_builddeb
binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install configure

Like it.? Share it:

One Response to Create New Debian Package From Scratch (Extra Tips for PHP Pear Modules)

  1. Toms Coupons

    I have a few extra questions. I am looking forward to more great content, thanks!