How to automate the installation of postfix on Ubuntu?

Better Stack Team
Updated on December 21, 2023

Automating the installation of Postfix on Ubuntu can be done using a package management tool like apt and a script or a configuration management tool like Ansible. Below are two methods to automate the installation of Postfix on Ubuntu:

Method 1 - Using a Shell Script

You can create a shell script to automate the installation of Postfix. Here's a basic example:

  1. Create a new file, e.g.,, using a text editor:

  2. Add the following script to the file:

    # Update package information
    sudo apt update
    # Install Postfix and its dependencies
    sudo apt install -y postfix
    # During the installation, you will be prompted for configuration.
    # You can automate this by pre-configuring Postfix with your desired settings.
    # Replace '' with your domain name.
    echo "postfix postfix/main_mailer_type select Internet Site" | sudo debconf-set-selections
    echo "postfix postfix/mailname string" | sudo debconf-set-selections
    echo "postfix postfix/destinations string, localhost.localdomain, localhost" | sudo debconf-set-selections
    # Restart Postfix
    sudo systemctl restart postfix
  3. Save the file and exit the text editor.

  4. Make the script executable:

    chmod +x
  5. Run the script to install Postfix:


This script will update package information, install Postfix with the specified settings, and restart Postfix.

Method 2 - Using Ansible

If you prefer a more structured and repeatable approach, you can use Ansible to automate the installation of Postfix:

  1. Install Ansible on your local machine if you haven't already:

    sudo apt update
    sudo apt install -y ansible
  2. Create an Ansible playbook, e.g., install_postfix.yml, with the following content:

    - name: Install Postfix
      hosts: your_target_server
      become: yes
        - name: Update package cache
            update_cache: yes
        - name: Install Postfix
            name: postfix
            state: present
        - name: Configure Postfix
            name: postfix
            question: "postfix/main_mailer_type"
            value: "Internet Site"
            vtype: "string"
            state: "present"
          notify: Restart Postfix
        - name: Restart Postfix
            name: postfix
            state: restarted

    Replace your_target_server with the hostname or IP address of the server where you want to install Postfix, and replace with your desired domain name.

  3. Run the Ansible playbook to install Postfix:

    ansible-playbook install_postfix.yml

    Ansible will handle the installation and configuration of Postfix based on the playbook.

Choose the method that best fits your automation needs and preferences. Shell scripts are simpler but less structured, while Ansible provides a more organized and maintainable way to automate system tasks.

Got an article suggestion? Let us know
Explore more
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github