Thursday, March 1, 2012

How to Create a Debian Package

In my previous blog, I've described how to create an RPM package. This time, I'm going to show you how to create a Debian package. I'm going to use the same code that's used in RPM tutorial blog. Creating a Debian package can be pretty complicated if we have to do it manually. Luckily there are some helper scripts that can make our lives easier.
  1. Install devscripts and debhelper
  2. sudo apt-get install devscripts debhelper
  3. Create a simple hello.cpp
  4. #include <iostream>
    using namespace std;
    
    int main() {
        cout << "Hello World" << endl;
        return 0;
    }
    
  5. Create a Makefile
  6. CC = g++ 
    CCFLAGS = -g -Wall
    SRC = hello.cpp
    TARGET = hello
    
    .PHONY: clean install
    
    all: hello
    
    install: $(TARGET)
        if [ -d ${DESTDIR} ]; then rm -rf ${DESTDIR}; fi
        mkdir -p ${DESTDIR}
        cp $(TARGET) ${DESTDIR} 
    
    hello: 
        $(CC) $(CCFLAGS) -o $(TARGET) $(SRC)
    
    clean:
        rm -rf $(TARGET)
    
  7. In the directory where we created the above files, create a new debian directory
  8. mkdir debian
  9. Create a debian/changelog file
  10. dch --create -v 0.1 --package hello
    We can create this changelog file without using dch script, but the changelog file requires a particular format and the dch script can help to create a changelog template. The changelog file should look like this.
    hello (0.1) UNRELEASED; urgency=low
    
      * Initial release. (Closes: #123)
    
     -- foo <foo@bar.com>  Thu, 01 Mar 2012 14:16:42 +0800
    
  11. Create a debian/compat
  12. The content of this file is very simple. It just contains a number 8. This file is used to tell debhelper to use compatibility helper version 8. The file should look like this.
    8
  13. Create a debian/control file
  14. This control file is used to describe the source and binary package and other information. This control file also has a particular format. Take a note that there's a space after the Description field. This to tell that it's an extended description. The description and extended description should be different; otherwise lintian (a tool to check the correctness of files rquired to create Debian packages) will complain. The control file should look like this.
    Source: hello
    Maintainer: foo <foo@bar.com>
    Section: misc
    Priority: optional
    Standards-Version: 3.9.1
    Build-Depends: debhelper (>= 8)
    
    Package: hello
    Architecture: any
    Depends: ${shlibs:Depends}, ${misc:Depends}
    Description: say hello
     say hello world
    
  15. Create a debian/rules file
  16. This file is basically a Makefile.
    #!/usr/bin/make -f
    %:
        dh $@
    
    override_dh_auto_install:
        $(MAKE) DESTDIR=$$(pwd)/debian/hello/usr/bin install
    
    The override_dh_auto_install is required because in our example, we have a custom make install target.

  17. Create a debian/copyright file
  18. For this tutorial, just create an empty copyright file.

  19. To summarize, we should've already created all these files
  20. /home/foo/testdeb/hello/hello.cpp
                           /Makefile
                           /debian/control
                           /copyright
                           /changelog
                           /compat
                           /rules
    
  21. Create a Debian package
  22. debuild -uc -us
    -uc means don't sign the .changes file
    -us means don't sign the source package
    For more information about the options, read man dpkg-buildpackage.

    You should now see your source and Debian packages in /home/foo/testdeb

  23. Install the newly created Debian package
  24. sudo dpkg -i hello_0.1_i386.deb
  25. Test if hello program was successfully installed
  26. hello
    You should see "Hello World" output

  27. Remove the hello package
  28. sudo dpkg -r hello

No comments:

Post a Comment