Automate Ubuntu Updates and Package Installation Carefully
Automate Ubuntu package maintenance with a clear package list, failure handling and a verified maintenance window. Keep routine upgrades separate from release changes and package removal.
Separate updates from package provisioning
Updating existing software and adding new packages have different review requirements. Define the target Ubuntu release, approved repositories, maintenance window and packages before scheduling either operation. Back up important data and keep a recovery path for remote servers.
Use apt-get for scripting and stop on errors. Do not silently attempt broad repairs, distribution upgrades or package removal as a side effect of a routine update script.
Review the proposed work first
sudo apt-get update
apt-get -s upgrade
Inspect the proposal and package-manager errors. A simulated upgrade is useful for review but does not guarantee that a later run sees identical repository contents. Coordinate with any existing unattended-upgrades policy so competing jobs are not launched together.
Use an explicit script for approved maintenance
After approval, the following simple example updates existing packages and installs two named packages. Run it from an interactive administrative account or through an approved management system:
#!/usr/bin/env bash
set -euo pipefail
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install git curl
if [ -f /var/run/reboot-required ]; then
printf '%s\n' 'A reboot is required; schedule it with the service owner.'
fi
The absence of -y keeps the interactive review visible. An unattended deployment requires an approved noninteractive strategy, logging and failure notification; placing this interactive script in a user’s cron table does not provide those controls.
Validate the outcome
Check exit status, package versions and the affected services. Review prompts or held packages rather than reporting unconditional success. Plan restarts separately and test the hosted workload afterward.
The earlier example used angle-bracket package placeholders that Bash would interpret as redirection, followed failures with further actions, and included automatic removals and a distribution upgrade. Those behaviors have been removed. Use unattended-upgrades for an intentionally designed automatic-security-update workflow rather than combining unrelated maintenance tasks in one unchecked daily cron job.
Practical checks and next steps
For unattended security patching, use a separately reviewed unattended-upgrades policy. For application provisioning, retain package versions, installer results and a rollback plan.
Explore Linux & Ubuntu for related guides.
Content review: November 24, 2026. Reviewed the stated scope, guidance and source references; tenant, hardware and production changes were not executed as part of this editorial review. Original publication date retained.
I rather use the unattended-upgrades package.
It allows you to configure what do you want to update/upgrade and when.
You can manage reboot time (when required) and try your config in dry run before applying.
It’s great tool that I use on all Linux Ubuntu Server.
Cheers from Canada !
Hello Xavier,
Thank you for your feedback, that does make it a whole lot easier. 🙂
Patrick Domingues