How to Perform Non-Interactive dpkg Installations on Ubuntu for Automated Server Deployment


3 views

When automating server deployments with bash scripts, interactive prompts from packages like MySQL Server can break the automation flow. The standard dpkg -i command may pause execution to request configuration input (like admin passwords), which isn't ideal for unattended installations.

The most effective method uses environment variables to suppress interactive prompts:

sudo DEBIAN_FRONTEND=noninteractive DEBIAN_PRIORITY=critical dpkg -i ./files/*.deb

For more complex scenarios, consider these approaches:

1. Using debconf-set-selections

Pre-set configuration values before installation:

echo "mysql-server mysql-server/root_password password yourpassword" | sudo debconf-set-selections
echo "mysql-server mysql-server/root_password_again password yourpassword" | sudo debconf-set-selections
sudo DEBIAN_FRONTEND=noninteractive dpkg -i mysql-server.deb

2. Combined apt-get Approach

For better dependency handling:

sudo DEBIAN_FRONTEND=noninteractive apt-get install -y -q ./your-package.deb

3. Force Non-interactive Mode

For stubborn packages:

sudo DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true dpkg -i package.deb

Some packages may still require post-install configuration. For MySQL specifically:

sudo mysql_secure_installation --use-default
# OR
sudo mysql -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'yourpassword';"

Here's a full working example for MySQL:

#!/bin/bash
export DEBIAN_FRONTEND=noninteractive
sudo apt-get update
sudo apt-get install -y debconf-utils

echo "mysql-server mysql-server/root_password password yourpassword" | sudo debconf-set-selections
echo "mysql-server mysql-server/root_password_again password yourpassword" | sudo debconf-set-selections

sudo apt-get install -y mysql-server
sudo mysql -e "CREATE DATABASE mydb;"
sudo mysql -e "GRANT ALL PRIVILEGES ON mydb.* TO 'myuser'@'localhost' IDENTIFIED BY 'mypassword';"
  • Check package documentation for specific configuration requirements
  • Use dpkg-reconfigure if manual configuration is needed after installation
  • For complex deployments, consider creating custom .deb packages with pre-configured defaults

Remember that non-interactive installations may have security implications, especially when dealing with passwords. Always:

  • Secure your deployment scripts
  • Remove password traces from command history
  • Consider using environment files or secret management tools

When automating Ubuntu server deployments, packages like MySQL Server present a unique challenge - their .deb installations trigger interactive configuration prompts during the post-install phase. This breaks CI/CD pipelines and unattended deployment scripts.

While export DEBIAN_FRONTEND=noninteractive works for apt-get installations, dpkg behaves differently. The environment variable gets reset during sudo escalation. Here's a more reliable approach:

sudo DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true \\
    dpkg -i --force-confdef --force-confold ./packages/*

For MySQL specifically, create a debconf preseeding file:

# mysql.preseed
mysql-server-5.7 mysql-server/root_password password yourpassword
mysql-server-5.7 mysql-server/root_password_again password yourpassword

Then load it before installation:

sudo debconf-set-selections < mysql.preseed
sudo dpkg-preconfigure -f noninteractive mysql-server-5.7.deb

Combine with apt-get to resolve dependencies:

sudo apt-get update && \\
sudo DEBIAN_FRONTEND=noninteractive apt-get -yq install ./mysql-server.deb

Here's a production-ready example for LAMP stack deployment:

#!/bin/bash
export NEEDRESTART_MODE=a
export DEBIAN_FRONTEND=noninteractive

# Preseed configurations
cat <

If installations still hang:

  1. Check /var/log/dpkg.log for configuration prompts
  2. Use dpkg --audit to find pending configurations
  3. For stubborn packages, extract with ar -x and manually place files