{"id":1870,"date":"2022-04-24T00:01:17","date_gmt":"2022-04-23T22:01:17","guid":{"rendered":"https:\/\/nguenkam.com\/blog\/?p=1870"},"modified":"2022-04-24T13:02:29","modified_gmt":"2022-04-24T11:02:29","slug":"how-to-create-debian-packages-deb","status":"publish","type":"post","link":"https:\/\/nguenkam.com\/blog\/index.php\/2022\/04\/24\/how-to-create-debian-packages-deb\/","title":{"rendered":"How to create Debian packages (.deb)"},"content":{"rendered":"\n<p>In this tutorial we will see:<\/p>\n\n\n\n<ul><li>How to create a basic  debian package from a binary file<\/li><li>How to create a local debian repository<\/li><li>How to add the repository to the list of software sources<\/li><\/ul>\n\n\n\n<p><em><span class=\"has-inline-color has-vivid-red-color\"><strong>PS<\/strong>:<\/span> let\u00b4s suppose we have a helloword C++ program (<strong>helloword.cpp)<\/strong>. we compile the code and obtain an executable called \u201chelloword\u201d which prints some string on the screen.<\/em><\/p>\n\n\n\n<p>First of all, we need to create the debian package structure. The only files required to build a debian package are:<\/p>\n\n\n\n<ul id=\"block-304219d9-adf6-427c-98cf-90b219712e07\"><li>DEBIAN\/control<\/li><li>custom files to be part of the package (not required)<\/li><\/ul>\n\n\n\n<h4><strong>Step 1: Create the directories<\/strong><\/h4>\n\n\n\n<p>Create a directory for our package and  create a directory <span class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>DEBIAN<\/strong><\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mkdir helloworld &amp;&amp; mkdir helloworld\/DEBIAN<\/code><\/pre>\n\n\n\n<h4><strong>Step 2: Copy files into our package<\/strong><\/h4>\n\n\n\n<p>Inside the&nbsp;<code><em><strong>helloworld<\/strong><\/em><\/code> directory we create a directory tree which represents the path where our program will be installed in the system, and copy the executable into it:<\/p>\n\n\n\n<p>let\u00b4s assume we want to put our apps in <em><span class=\"has-inline-color has-luminous-vivid-orange-color\">\/usr\/local\/bin\/<\/span><\/em> , let\u00b4s put it in <em><strong>helloworld\/usr\/local\/bin\/<\/strong><\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mkdir -p helloworld\/usr\/local\/bin \ncp \/path\/to\/helloworld helloworld\/usr\/local\/bin\/<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h4><strong>Step 3: Create the control file<\/strong> <em><strong><span class=\"has-inline-color has-vivid-green-cyan-color\">(Required)<\/span><\/strong><\/em><\/h4>\n\n\n\n<p>Let\u00b4s create a file <span class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>&#8216;control&#8217;<\/strong><\/span> in the DEBIAN directory<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Package: helloworld\nVersion: 0.2\nMaintainer: Brice Nguenkam\nArchitecture: all\nDescription: Print hello world  on the screen <\/code><\/pre>\n\n\n\n<p>These are the mandatory fields in the control file. For more options see\u00a0<a href=\"http:\/\/www.debian.org\/doc\/debian-policy\/ch-controlfields.html#s-binarycontrolfiles\">http:\/\/www.debian.org\/doc\/debian-policy\/ch-controlfields.html#s-binarycontrolfiles<\/a><\/p>\n\n\n\n<p>At this point we are ready to create\/build the package. So we can skip <em>step 4 &amp; 5,<\/em> if we don\u00b4t need it.<\/p>\n\n\n\n<h4><strong>Step 4: Add a post-installation script<\/strong> <em><span class=\"has-inline-color has-luminous-vivid-amber-color\"><strong>(Optional)<\/strong><\/span><\/em><\/h4>\n\n\n\n<p>We can create a file <span class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>&#8216;postinst&#8217;<\/strong><\/span> in the <span class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>DEBIAN <\/strong><\/span>directory. Make sure it is executable. It will run when the installation is complete.<\/p>\n\n\n\n<p><em><strong><span class=\"has-inline-color has-vivid-red-color\">PS:<\/span><\/strong> In general, we can also add <a rel=\"noreferrer noopener\" href=\"https:\/\/www.hivelocity.net\/kb\/what-are-preinst-postinst-prerm-and-postrm-script-2\/\" target=\"_blank\"><strong>preinst<\/strong>,\u00a0<strong>postinst<\/strong>,\u00a0<strong>prerm<\/strong>, and\u00a0<strong>postrm<\/strong><\/a>\u00a0files in the DEBIAN directory. They are all executable scripts that Debian automatically runs before or after package installation.<\/em><\/p>\n\n\n\n<h4><strong>Step 5: Add an installation script<\/strong> <em><strong><span class=\"has-inline-color has-luminous-vivid-amber-color\">(Optional)<\/span><\/strong><\/em><\/h4>\n\n\n\n<p>If specifics(extra) files need to be installed in the package but normal &#8220;<em><strong>make install<\/strong><\/em>&#8221; (<code>Makefile<\/code>\u00a0or other install system) does not do it, then we should add the filenames and their destination to this install file.<\/p>\n\n\n\n<p>The syntax is simply:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>path\/sourcedir\/file path\/installdir\r\n# for example\r\ndata\/foo.jpg usr\/share\/packagename<\/code><\/pre>\n\n\n\n<p>In This script , for every file we want to install, we will write one line with the name of the file (relative to the main build directory) followed by a space, then the installation directory (relative to the install directory).<\/p>\n\n\n\n<p>For example, let us suppose a &#8216;src\/fis-pack&#8217; binary is not installed, the install file might look like:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>src\/fis-pack usr\/bin<\/code><\/pre>\n\n\n\n<p>This means that when installing the package, there will be an executable command <em><strong>\/usr\/bin\/fis-pack<\/strong><\/em>. <\/p>\n\n\n\n<p><em><strong><span class=\"has-inline-color has-vivid-red-color\">PS:<\/span><\/strong> It&#8217;s normally unnecessary unless you specifically need an empty directory or if there is a problem with the source\u00a0<code>Makefile<\/code>&#8230;<\/em><\/p>\n\n\n\n<h4><strong>Step 6: Create the package<\/strong><\/h4>\n\n\n\n<p>To do that we use the&nbsp;<code>dpkg-deb&nbsp;<\/code>tool.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ dpkg-deb --build helloworld\ndpkg-deb: building package `helloworld' in `helloworld.deb'.\n$ ls\nhelloworld  helloworld.deb<\/code><\/pre>\n\n\n\n<p>This creates a helloworld.deb file, which&nbsp;we can now install on any Debian installation with following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dpkg -i helloworld.deb<\/code><\/pre>\n\n\n\n<p><em><strong><span class=\"has-inline-color has-vivid-red-color\">ps<\/span><\/strong>: You may want to change the name of the package so that it includes the program version and the package architecture. For example:<\/em> (<em>package<strong>&#8211;<\/strong>name<strong>_<\/strong>VersionNumber<strong>_<\/strong>DebianArchitecture)<\/em>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ mv helloworld.deb helloworld-0.2_all.deb\n\n\/\/or if we want to set a specific architectur\n$ mv helloworld.deb helloworld-0.2_amd64.deb<\/code><\/pre>\n\n\n\n<p>There are <strong>various platforms<\/strong>\u00a0: amd64, arm64, etc.\u00a0<em> X64, amd64 and x86-64 <\/em>\u00a0are names for the same processor type.<\/p>\n\n\n\n<p><strong>eg:<\/strong>  <span class=\"has-inline-color has-vivid-red-color\"><strong><em>Tree structure of a project with the Debian package<\/em><\/strong><\/span><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" width=\"368\" height=\"809\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/04\/image-33.png\" alt=\"\" class=\"wp-image-1874\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/04\/image-33.png 368w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/04\/image-33-136x300.png 136w\" sizes=\"(max-width: 368px) 100vw, 368px\" \/><\/figure><\/div>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3><span class=\"has-inline-color has-vivid-cyan-blue-color\">Setting up a local package repository<\/span><\/h3>\n\n\n\n<p>To create a local package repository we need a working. In this case we will assume the use of&nbsp;<code>Apache<\/code>&nbsp;with default settings. To&nbsp;install the Apache Webserver, all we need to do is to run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sudo apt-get install apache2<\/code><\/pre>\n\n\n\n<p>Once installed, to verify that the webserver works, we can navigate to the IP address of the machine (or to \u2018http:\/\/localhost\u2019, if running a browser on the machine itself) which,&nbsp; in our case is http:\/\/10.1.1.4. We should see the<br>famous&nbsp;<strong>It works!&nbsp;<\/strong>message.<\/p>\n\n\n\n<p>The web server software is running but no content has been added, yet. The&nbsp;<code>DocumentRoot<\/code>&nbsp;of the default Apache&nbsp;<code><em><span class=\"has-inline-color has-luminous-vivid-orange-color\">VirtualHost<\/span><\/em><\/code>, is&nbsp;<em><span class=\"has-inline-color has-luminous-vivid-orange-color\"><code>\/var\/www\/html<\/code>:<\/span><\/em> this is where we will create our repository.<\/p>\n\n\n\n<p>Let\u2019s create the \u201cdebian\u201d directory inside<code>&nbsp;\/var\/www\/html<\/code>&nbsp;and copy the <em><strong><span class=\"has-inline-color has-luminous-vivid-orange-color\">helloworld-1.0_amd64.deb <\/span><\/strong><\/em>package inside it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sudo mkdir \/var\/www\/html\/debian\n$ sudo cp \/path\/to\/helloworld-0.2_amd64.deb \/var\/www\/html\/debian\/<\/code><\/pre>\n\n\n\n<p>The next step consists in the generation of a package list. We move into the&nbsp;<code>debian<\/code>&nbsp;directory, and use the&nbsp;<code>dpkg-scanpackages&nbsp;<\/code>utility to accomplish the task. You may need to install the&nbsp;<code>dpkg-dev<\/code>&nbsp;package in case the&nbsp;<code>dpkg-scanpackages<\/code>&nbsp;command is missing:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ dpkg-scanpackages . | gzip -c9  &gt; Packages.gz\ndpkg-scanpackages: info: Wrote 1 entries to output Packages file.<\/code><\/pre>\n\n\n\n<p>Our local repository is now ready.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3><span class=\"has-inline-color has-vivid-cyan-blue-color\">Adding the repository to the software sources<\/span><\/h3>\n\n\n\n<p>At this point to be able to install our package from the local repository we created, we need to edit the&nbsp;<code>\/etc\/apt\/sources.list<\/code>&nbsp;file, add the entry relative to it (change IP address to reflect that of your machine), and<br>synchronize the repositories:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo \"deb &#91;trusted=yes] http:\/\/10.1.1.4\/debian .\/\" | tee -a \/etc\/apt\/sources.list &gt; \/dev\/null<\/code><\/pre>\n\n\n\n<p>Be sure to add the above&nbsp;<code><span class=\"has-inline-color has-luminous-vivid-orange-color\"><strong>[trusted=yes]<\/strong><\/span><\/code>&nbsp;to avoid the following error message:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Release' does not have a Release file.                   \nN: Updating from such a repository can't be done securely, and is therefore disabled by default.<\/code><\/pre>\n\n\n\n<p>Synchronize repositories:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sudo apt-get update\n<\/code><\/pre>\n\n\n\n<p>To install our package, we can now use the apt-get tool:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sudo apt-get install helloworld<\/code><\/pre>\n\n\n\n<p>Execute:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ helloworld<\/code><\/pre>\n\n\n\n<p>To remove the package from the system, just run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sudo apt-get remove linuxconfig<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial we will see: How to create a basic debian package from a binary file How to create a local debian repository How to add the repository to the list of software sources PS: let\u00b4s suppose we have a helloword C++ program (helloword.cpp). we compile the code and obtain an executable called \u201chelloword\u201d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1872,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1,316],"tags":[539,421,538,171,541,542,148,540],"_links":{"self":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1870"}],"collection":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=1870"}],"version-history":[{"count":12,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1870\/revisions"}],"predecessor-version":[{"id":1888,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1870\/revisions\/1888"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/1872"}],"wp:attachment":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=1870"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=1870"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=1870"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}