Make sure $CFG->upgradekey is before require_once() at the end of config.php, e.g.:
⋮
$CFG->upgradekey = 'my_upgrade_key';
require_once(__DIR__ . '/lib/setup.php');
// There is no php closing tag in this file,
// it is intentional because it prevents trailing whitespace problems!