In this post, I describe how to upgrade a Moodle installation from one major version to a newer one using the command line and git.
When it comes to open-source e-learning platforms, Moodle is pretty much the only game in town. With masses of features and numerous contributed plugins, it's very powerful and customisable. On the downside, it has a very heavy performance footprint, and upgrading it can be rather an involved process. There aren't any helpful Debian packages and the documentation is a bit patchy in places. Using git to keep Moodle up to date takes a lot of the pain away, but it's still difficult to find clear step-by-step instructions. The following guide collects together a few bits of information I've collected about git and Moodle from various places on the internet.
- Back up your Moodle installation:
cp -a /path/to/moodle /path/to/moodle.bakmysqldump -u [moodle_database_user] -p [moodle_database_name] > moodle_database_backup.sql
- Enable maintenance mode and switch to the newer branch — you can skip the branch checkout if you're only upgrading between minor versions. Replace
XYwith the correct version number, e.g. 26 for Moodle 2.6.x:cd /path/to/moodle$(which php) admin/cli/maintenance.php --enablegit remote updategit checkout -b MOODLE_XY_STABLE origin/MOODLE_XY_STABLEgit pull
- Upgrade the database and disable maintenance mode:
$(which php) admin/cli/upgrade.php$(which php) admin/cli/maintenance.php --disable
- Login to the site and check the version number is correct on Site administration → Notifications
- Upgrade any contributed plugins by visiting Site administration → Plugins → Plugins overview and clicking “Check for available updates”.
- (Optional) Remove the old branch with
git branch -d MOODLE_AB_STABLEwhereABis equivalent to the old version number, e.g. 25 for Moodle 2.5.x.
Comments
Who's Alex?
Not sure why everyone is thanking someone called Alex since it was me who wrote this article, but I’m glad it’s proving useful!
Re: $(which php)
Hi John, it's for nesting bash commands. Type $(command) and it will attempt to run command and then run its output. For example, on my laptop, which php returns /usr/bin/php, so $(which php) is equivalent to running /usr/bin/php. For simple commands that are in your users $PATH it's probably unnecessary, but it can be handy for more complex commands.
I got this message when I try to update from 3.3 to 3.3.1
After I run this command : $(which php) admin/cli/upgrade.php
I got this message: No upgrade needed for the installed version 3.3+ (Build: 20170525)
(2017051500.02). Thanks for coming anyway!
I also try to update using git command:
sudo git pull, but the notification still show old version. Please help. Thanks
Re: I got this message when I try to update from 3.3 to 3.3.1
Hi John,
What output do you get from git pull?
Did not work if only the branch was cloned
Originally I had cloned with
> git clone --depth=1 --single-branch -b MOODLE_28_STABLE git://git.moodle.org/moodle.git moodle_code
Therefore the new branch was not available. I had to
> git remote set-branches --add origin MOODLE_31_STABLE
> git fetch --depth=1
> git checkout -b MOODLE_31_STABLE --track origin/MOODLE_31_STABLE
Why should I pull after the git checkout? Of course I need to pull when I want to update within the branch (MOODLE_31_STABLE)
Hi Alex, I am a long term Moodle user and was trying to pull a tutorial together for a friend of mine. Anyway, rather than write from scratch I did a Google search, found your article. Tested it. Works like a charm. Thank you