Update Ubuntu (Tips & Tricks)

A vital part of keeping a secure and functional environment is making sure you update Ubuntu regularly. System updates in Ubuntu typically require use of the package management tool apt. This article won’t be a full coverage tutorial for apt, instead it will just cover its main update functions.

We should use all of these commands in order to keep your system running as up to date and as efficiently as possible. At the end of this article, I’ll include a bash script that includes all of them.

sudo apt update

The apt update command is used to synchronize the software repository file with the official Ubuntu list. If we try to install a software package that apt doesn’t seem to recognize, you may be trying to install something that was added to a repository after your previous update. If that is the issue, running this command may fix your issue.

sudo apt upgrade

The apt upgrade command is used to upgrade all of the software packages installed on a system to the most recent updates. If you have a lot of software packages on your system, this can take a while. Nevertheless, it is still an extremely important action to take regularly.

sudo apt dist-upgrade

The apt dist-upgrade command installs an update on your system’s kernel. Make sure to reboot your system after taking this action, as soon as you would like the changes to take effect.

Ubuntu Update Script

To start, this script defines a function to check the exit status of whatever the previous command was. If the last command exited with an error status, print a message and exit the program. Lower, you see that the script runs the update commands that we just went over and checks them with the function. This script is very simple and it is meant to be so, so that you may modify it to fit any needs. Be sure to run it as root or with sudo.

#!/bin/bash

# sudoadmins.com

chExit() {
  if [ $? -eq 0 ]
  then
    echo "Done..."
  else
    echo "ERROR! Something went wrong!"
    exit 1
  fi
}

echo "Updating $USER@$HOSTNAME..."

sudo apt update
chExit
sudo apt upgrade
chExit
sudo apt dist-upgrade
chExit

echo "Update for $USER@$HOSTNAME is complete."
echo "Reboot your system to take all changes fully into effect."

Conclusion

It is an important part of keeping a secure and functional environment to update Ubuntu regularly. We should use all of these commands on some kind of regular basis order to keep our systems running as up to date and as efficiently as possible. I’ve also included the basis for an update script that can keep us running these updates conveniently.

1