How to Bypass Post-Install Configuration Steps in apt-get for Unattended Package Installation


6 views

When managing Debian/Ubuntu systems, you've likely encountered situations where package installation fails due to post-install configuration steps. This commonly occurs with services like RabbitMQ, PostgreSQL, or MySQL where the default post-install behavior automatically starts the service.

Several scenarios necessitate bypassing these steps:

  • Port conflicts with existing services
  • Unattended installations in automated environments
  • Custom configuration requirements before service start
  • Docker container builds where services shouldn't start immediately

The most effective approach combines environment variables with apt-get parameters:

DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends rabbitmq-server

Key parameters:

  • DEBIAN_FRONTEND=noninteractive: Suppresses interactive prompts
  • -y: Automatic yes to prompts
  • -q: Quiet mode
  • --no-install-recommends: Avoids unnecessary dependencies

For services with particularly stubborn post-install scripts:

echo "#!/bin/sh" > /usr/sbin/policy-rc.d
echo "exit 101" >> /usr/sbin/policy-rc.d
chmod +x /usr/sbin/policy-rc.d
DEBIAN_FRONTEND=noninteractive apt-get install -y your-package
rm -f /usr/sbin/policy-rc.d

Here's how to implement this in a Dockerfile:

FROM ubuntu:20.04
RUN echo '#!/bin/sh' > /usr/sbin/policy-rc.d \
    && echo 'exit 101' >> /usr/sbin/policy-rc.d \
    && chmod +x /usr/sbin/policy-rc.d \
    && DEBIAN_FRONTEND=noninteractive apt-get update \
    && DEBIAN_FRONTEND=noninteractive apt-get install -y rabbitmq-server \
    && rm -f /usr/sbin/policy-rc.d

For more control, use dpkg directly:

apt-get download your-package
dpkg --unpack your-package.deb
# Manual configuration if needed
dpkg --configure -a

If installation fails despite these measures:

  1. Check logs in /var/log/apt/term.log
  2. Examine package scripts with apt-get download package && dpkg -e package.deb
  3. Force installation with apt-get -f install

When installing certain packages like RabbitMQ on Debian/Ubuntu systems, the package's post-install scripts automatically attempt to start the service. While convenient in most scenarios, this becomes problematic when:

  • Another service is already using the required port
  • You're installing in a chroot environment
  • You're building a Docker image and want to defer service startup
  • You need to modify configuration before the first run

The most reliable way to skip post-install configuration is by setting these environment variables:

DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true apt-get install -y rabbitmq-server

For Dockerfiles or automated scripts, you can combine this with the --no-install-recommends flag:

RUN apt-get update && \
    DEBIAN_FRONTEND=noninteractive \
    DEBCONF_NONINTERACTIVE_SEEN=true \
    apt-get install -y --no-install-recommends rabbitmq-server

If you need to run the configuration later, use:

dpkg-reconfigure rabbitmq-server

For RabbitMQ specifically, you might want to prevent automatic startup permanently:

echo "#!/bin/sh
exit 0" > /etc/init.d/rabbitmq-server
chmod +x /etc/init.d/rabbitmq-server

Or alternatively, mask the service in systemd:

systemctl mask rabbitmq-server.service

For complete control, you can extract and modify the package scripts:

mkdir /tmp/pkg
cd /tmp/pkg
apt-get download rabbitmq-server
dpkg-deb -R rabbitmq-server*.deb .
nano DEBIAN/postinst  # Modify the script
dpkg-deb -b . ../modified-rabbitmq.deb
dpkg -i ../modified-rabbitmq.deb
  • Always test these approaches in a development environment first
  • Some packages may have hard dependencies on their post-install scripts
  • Consider using apt-get -o Dpkg::Options::="--force-confold" to preserve existing configs