What is a post-installation script for a package?

Solution 1:

The name of the script itself says what it does. It means that the script contains commands to be executed after installation is complete.

Let's understand it in a better way through an example:

  • Assume you're installing a newer kernel. Once the installation of the .deb file is complete, it needs to tell GRUB that the kernel is installed, so that you can boot into it. So the post-installation of the package will tell GRUB that the kernel is installed by executing the commands update-grub and update-initramfs. For more info see the FAQ section.

A more generic example can be this:

  • Assume you're installing the package foo. Let's say now that the package needs a service called bar to be disabled. How will it disable it? The developers will add the command systemctl disable bar.service, which will disable the service, in the foo package's post-installation script. So the package's post-installation script consists of additional commands, updates etc...

    Installation consists of the following steps:

    1. Extract the control files of the new package.

    2. If another version of the same package was installed before the new installation, execute prerm script of the old package.

    3. Run preinst script, if provided by the package.

    4. Unpack the new files, and at the same time back up the old files, so that if something goes wrong, they can be restored.

    5. If another version of the same package was installed before the new installation, execute the postrm script of the old package. Note that this script is executed after the preinst script of the new package, because new files are written at the same time old files are removed.

    6. Configure the package.

Post-installation comes under the "Configure the package". It consists of:

  1. Telling other applications about the successful installation of the package.

  2. Precising paths to the package so that users and other applications can use/execute it.

  3. Look for any errors in the installation and print the error message.


FAQ section


  • Where are these scripts located?

    These scripts are located in /var/lib/dpkg/info. These scripts have a .postinst extension. You can run locate .postinst to find more of these scripts.

    A sample post-installation script looks like this:

    #!/bin/sh
    set -e
    if [ -x /usr/sbin/update-initramfs ]; then
    /usr/sbin/update-initramfs -u -k all
    fi
    
  • What does it mean if the post-installation script returns exit status 1?

    Generally, this means that the post-installation script returns exit 1 to indicate that commands inside the post-installation script failed for one reason or another.

  • How do I fix exit status 1?

    You can try googling the errors with the commands and fix them. If you're facing errors with apt and/or dpkg, then you can remove the post installation script and safely move it back after you've fixed the commands. However, this may not always be the correct solution. You may want to ask a new question.

    Some useful commands for fixing common post-installation script errors are mentioned in the table below:

    Command Description Forceful Syntax
    apt purge Removes the package along with its configuration files. Depends on the situation. sudo apt purge <package_name>
    dpkg -r Removes the package forcefully. Offers a lot of useful arguments. Yes, when used with certain arguments sudo dpkg -r --force-all <package_name>
    dpkg -P Purges the package forcefully. Offers a lot of useful arguments. Yes, when used with certain arguments. sudo dpkg -P --force-all <package_name>
  • I am asking a new question regarding errors related to these scripts, what information should I provide?

    You should provide information such as:

    • Your Ubuntu version (output of lsb_release -d).
    • Output of sudo apt update.
    • Full output of sudo apt -f install.
    • Contents of the post-installation script of the package.

    Other details should be provided when asked.

  • Can I change/remove these scripts?

    NOPE! Avoid messing with these scripts. If there are syntax errors or errors with any command, apt will fail each and every installation for that package. If by chance, there are any syntax errors then you need to manually use backend tools like dpkg to fix everything up. Can I remove them? NO! Don't remove them unless it's necessary, removing these scripts may make these packages unusable.

  • How do I re execute these scripts?

    Want to re execute the post installation script of a package? As I said earlier, running the post-installation script comes under "Configuration" of the package. So, simple run:

    sudo dpkg --configure <package>
    

    Where <package> is the name of the package you want to configure.


Friends and relatives of this script.

Try running ls in /var/lib/dpkg/info and grep the output with the pattern aptitude. You'll find a lot of similar kinds of scripts as it's output. Some of them are .postrm, .preinst, prerm and .list along with the signatures file. Here is a table showing the functions of each of these scripts:

Script Function
Post removal (.postrm) This script consists of commands to be run AFTER the removal is complete.
Pre-installation (.preinst) This script consists of commands to be run before the installation.
Pre-removal (.prerm) this script consists of the commands to be run before the removal process
Lists file (.list) This file contains the list of directories owned/belongs to the package.

For more information refer to the manpage of dpkg