Ubuntu 22.04 Tutorials

How to Automate Ubuntu Server System Updates and Package Installation

Ubuntu 22.04

Keeping your Ubuntu system updated and installing essential packages can be done efficiently using a simple Bash script. This guide will walk you through creating a script that automates system updates, upgrades installed packages, and installs specific packages of your choice. You can run the script manually or schedule it to execute automatically at a set time using cron jobs.

Step-by-Step Guide

Step 1: Create the Bash Script

Start by creating a new script file, for example, update_and_install.sh. This script will handle all the necessary steps for updating your system.

Example Script: update_and_install.sh

#!/bin/bash

# Update package list
echo "Updating package list..."
sudo apt update

# Check if dpkg was interrupted and run 'sudo dpkg --configure -a' if necessary
if [ $? -ne 0 ]; then
    echo "Checking for dpkg errors..."
    dpkg_error=$(sudo apt update 2>&1 | grep "dpkg was interrupted")
    if [ -n "$dpkg_error" ]; then
        echo "Error detected: dpkg was interrupted. Running 'sudo dpkg --configure -a'..."
        sudo dpkg --configure -a
    fi
fi

# Upgrade installed packages
echo "Upgrading installed packages..."
sudo apt upgrade -y

# Dist-upgrade to ensure the kernel and other important packages are upgraded
echo "Performing dist-upgrade..."
sudo apt dist-upgrade -y

# Remove unnecessary packages
echo "Removing unnecessary packages..."
sudo apt autoremove -y

# Install specific packages (replace with the ones you need)
# Example: sudo apt install -y package1 package2
echo "Installing specified packages..."
sudo apt install -y <package1> <package2>

# Cleanup any unnecessary files
echo "Cleaning up..."
sudo apt clean

# Check if a reboot is required
if [ -f /var/run/reboot-required ]; then
    echo "A reboot is required to complete the installation."
else
    echo "No reboot is necessary."
fi

echo "System update and package installation completed."

Step 2: Customize the Script

  • Replace the placeholders: In the section where it says sudo apt install -y <package1> <package2>, replace <package1> <package2> with the names of the packages you want to install. For example:
  sudo apt install -y git curl

If you don’t have any specific packages to install, you can omit this part.

Step 3: Make the Script Executable

Before you can run the script, you need to make it executable. To do this:

  1. Open a terminal.
  2. Navigate to the directory where your script is saved.
  3. Run the following command:
   chmod +x update_and_install.sh

Step 4: Run the Script

Now, you can manually run the script to update your system and install packages. In the terminal, run:

./update_and_install.sh

This will update your package list, upgrade installed packages, remove unnecessary ones, and install the specified packages.

Step 5: Schedule the Script with Cron (Optional)

To automate the process, you can schedule the script to run at a specific time using cron. Here’s how to set it up to run, for example, daily at 2 AM.

  1. Open the cron table for editing:
   crontab -e
  1. Add a line to schedule the script. For a daily update at 2 AM, add:
   0 2 * * * /path/to/update_and_install.sh

Replace /path/to/update_and_install.sh with the actual path to your script.

  1. Save and exit. Now your system will automatically run the script at the specified time.

Conclusion

This simple Bash script is a great way to ensure your Ubuntu system is always up-to-date and equipped with the necessary packages. By running it manually or automating it through cron jobs, you can save time and keep your system maintained with minimal effort.


Discover more from Patrick Domingues

Subscribe to get the latest posts sent to your email.

author avatar
Patrick Domingues

2 Comments

  1. 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 !

Leave a Comment

Stay Informed

Receive instant notifications when new content is released.