Description: Script to upgrade Ubuntu to the latest LTS release.
Date: 2025-10-02 03:54:56
Download command: wget https://doc.laptopministry.org/uploads/1759377296_upuntu.sh
#!/bin/bash
# Script to upgrade Ubuntu to the latest LTS release
# Run with: sudo bash upgrade-to-latest-lts.sh
# Script developed by Laptop Ministry on June 14th 2025.
# Copyleft 2025.
# Exit on error
set -e
# Log file for debugging
LOGFILE="/var/log/ubuntu-lts-upgrade.log"
echo "Ubuntu LTS Upgrade started at $(date)" | tee -a "$LOGFILE"
# Function to log messages
log() {
echo "[$(date)] $1" | tee -a "$LOGFILE"
}
# Function to check if the current release is LTS
is_lts() {
lsb_release -a 2>/dev/null | grep -q "LTS"
return $?
}
# Function to get the current Ubuntu version
get_current_version() {
lsb_release -rs 2>/dev/null || echo "unknown"
}
# Function to check if the version is the latest LTS (simplified check)
# As of June 2025, 24.04 is the latest LTS; update if a newer LTS is known
is_latest_lts() {
local version="$1"
# List of known LTS versions in ascending order (add newer LTS as needed)
local lts_versions=("20.04" "22.04" "24.04")
local latest_lts="${lts_versions[-1]}"
# Compare current version with the latest known LTS
if [[ "$version" == "$latest_lts" ]]; then
return 0 # Is latest LTS
fi
return 1 # Not latest LTS
}
# Function to check disk space (require at least 5GB free)
check_disk_space() {
local free_space
free_space=$(df -h / | tail -1 | awk '{print $4}' | sed 's/G//')
if (( $(echo "$free_space < 5" | bc -l) )); then
log "ERROR: Insufficient disk space ($free_space GB free). Need at least 5GB."
exit 1
fi
log "Disk space check passed ($free_space GB free)."
}
# Function to prompt for backup
prompt_backup() {
read -p "Have you backed up all critical data? (y/n): " response
if [[ ! "$response" =~ ^[Yy]$ ]]; then
log "ERROR: Please back up your data before proceeding."
exit 1
fi
log "Backup confirmed by user."
}
# Function to update and upgrade packages
update_system() {
log "Updating and upgrading current system..."
apt update >> "$LOGFILE" 2>&1 || { log "ERROR: Failed to update package lists."; exit 1; }
apt upgrade -y >> "$LOGFILE" 2>&1 || { log "ERROR: Failed to upgrade packages."; exit 1; }
apt dist-upgrade -y >> "$LOGFILE" 2>&1 || { log "ERROR: Failed to perform dist-upgrade."; exit 1; }
apt autoremove -y >> "$LOGFILE" 2>&1 || { log "WARNING: Failed to clean up unused packages."; }
log "System updated successfully."
}
# Function to configure release upgrades for LTS
configure_lts() {
log "Configuring release upgrades for LTS..."
local config_file="/etc/update-manager/release-upgrades"
if ! grep -q "^Prompt=lts" "$config_file"; then
sed -i 's/Prompt=.*/Prompt=lts/' "$config_file" || { log "ERROR: Failed to configure $config_file."; exit 1; }
fi
log "LTS upgrade configuration set."
}
# Function to perform the release upgrade
perform_upgrade() {
log "Starting release upgrade..."
do-release-upgrade -f DistUpgradeViewNonInteractive >> "$LOGFILE" 2>&1 || {
log "ERROR: Release upgrade failed. Check $LOGFILE for details."
exit 1
}
log "Release upgrade completed."
}
# Function to post-upgrade checks
post_upgrade_checks() {
log "Running post-upgrade checks..."
apt update >> "$LOGFILE" 2>&1 || { log "WARNING: Failed to update package lists post-upgrade."; }
apt upgrade -y >> "$LOGFILE" 2>&1 || { log "WARNING: Failed to upgrade packages post-upgrade."; }
apt autoremove -y >> "$LOGFILE" 2>&1 || { log "WARNING: Failed to clean up unused packages."; }
apt install -f -y >> "$LOGFILE" 2>&1 || { log "WARNING: Failed to fix broken packages."; }
log "Post-upgrade checks completed."
}
# Main script
log "Starting Ubuntu LTS upgrade process..."
# Check if running as root
if [[ $EUID -ne 0 ]]; then
log "ERROR: This script must be run as root (use sudo)."
exit 1
fi
# Prompt for backup
prompt_backup
# Check disk space
check_disk_space
# Install update-manager-core
log "Installing update-manager-core..."
apt install -y update-manager-core >> "$LOGFILE" 2>&1 || { log "ERROR: Failed to install update-manager-core."; exit 1; }
# Configure for LTS upgrades
configure_lts
# Main upgrade loop
while true; do
current_version=$(get_current_version)
log "Current Ubuntu version: $current_version"
if ! is_lts; then
log "WARNING: Current version is not an LTS release. Attempting to upgrade to next LTS."
fi
if is_latest_lts "$current_version"; then
log "System is already on the latest LTS release ($current_version). Exiting."
break
fi
# Update system before upgrade
update_system
# Perform the release upgrade
perform_upgrade
# Run post-upgrade checks
post_upgrade_checks
# Reboot (optional, uncomment if desired)
# log "Rebooting system..."
# reboot
done
# Final verification
current_version=$(get_current_version)
log "Final Ubuntu version: $current_version"
if is_lts && is_latest_lts "$current_version"; then
log "Successfully upgraded to the latest LTS release ($current_version)."
else
log "WARNING: Upgrade completed, but system may not be on the latest LTS. Current version: $current_version."
fi
exit 0
Back to Codes