If you are upgrading for example to Moodle 5.0, Remove old third-party plugins incompatible with Moodle 5.0 and then then update dependencies. If you are upgrading Via Git, take these steps;
cd /var/www/moodle
sudo git fetch
sudo git checkout MOODLE_500_STABLE
sudo git pull origin MOODLE_500_STABLE
Time to clean up old plugins
sudo apt install composer // Only if you do not have composer installed yet
cd /var/www/moodle
sudo composer install –no-dev
Go ahead and set permissions, complete the Upgrade and purge caches
sudo chown -R www-data:www-data /var/www/moodle
sudo chown -R www-data:www-data /var/www/moodledata
Complete upgrade
sudo -u www-data /usr/bin/php /var/www/moodle/admin/cli/upgrade.php
Purge caches:
sudo -u www-data /usr/bin/php /var/www/moodle/admin/cli/purgecaches.php