24

So I'm building a .deb, and I need to do a few post-install commands (linking libraries paths, and a few other things).

Is there a way to have it so when you do a sudo dpkg -i <pacakage-name>.deb, a script runs?

jrg
  • 60,611

2 Answers2

28

You can execute commands after installation using the postinst shell script located in the debian folder in the source.

This is a normal shell script containing all the commands to be executed (in order) after the installation of the package. dpkg will run this automatically. So, all you need to do is create a shell script before packing your source package into a binary package, name it as 'postinst' and put it in the debian directory.

For more info, visit the Ubuntu Packaging Guide.

Cashew
  • 103
  • 3
rigved
  • 2,357
  • The page doesn't exist anymore... – Ionică Bizău Dec 28 '14 at 19:21
  • 2
    The page exists, the link is just wrong (it has a duplicate '/html' at the end). Correct link is http://packaging.ubuntu.com/html/ . But perhaps the page has changed, because i found http://packaging.ubuntu.com/html/packaging-new-software.html to be more useful. – Knetic Apr 29 '15 at 16:53
  • Be careful, postinst isn't executed after "installation of the package" it's executed after "extracting the files from the deb package" the install process may fail and the postinst is still being executed. – Miguel Ortiz Nov 15 '18 at 19:21
  • No @Miguel, if the installation process encounters an error before the postinst runs then the postinst isn’t executed. – Stephen Kitt Nov 16 '18 at 16:13
  • @StephenKitt I'm currently working with a deb package which didn't met the dependencies and actually executed the postinst. I'll try to update my answer with the lab info. – Miguel Ortiz Nov 16 '18 at 17:22
2

There's a file called:

<package directory>/DEBIAN/postinst

This script is a normal bash shell script and should contain the appropriate bash commands e.g.:

#!/bin/bash
echo "Post install script is running now"
#systemctl status <your systemd service>

Be sure that when you build the debian package that you remember to copy this post install script into the package.

I made mine executable:

chmod 755 postinst

This postinst script is executed after the apt package manager installs the appropriate files.

It's as simple as that.

Owl
  • 546