How can I permanently enable an addon in the userpref through Python? I'm trying to create an installer that installs a Blender addon from the terminal, something like this:
wget https://my-installer/install | python

In which the install script will download a zip for the appropriate blender version and I found the bpy.ops.preferences.addon_install and addon_enable operators.
My script gets run with blender --background --python _tmp_installer.py:
import bpy, os
p = os.path.abspath('blenderneuron.zip')
bpy.ops.preferences.addon_install(filepath=p, overwrite=True)
bpy.ops.preferences.addon_enable(module='blenderneuron')

The output of which I capture:
C:\Users\robin\Documents\GIT\BlenderNEURON&gt;python &lt; installer/install.py
Blender output: Blender 2.91.0 (hash 0f45cab862b8 built 2020-11-25 09:02:56)
Read prefs: C:\Users\robin\AppData\Roaming\Blender Foundation\Blender\2.91\config\userpref.blend
found bundled python: C:\Program Files\Blender Foundation\Blender 2.91\2.91\python
Info: Modules Installed () from 'C:\\Users\\robin\\Documents\\GIT\\BlenderNEURON\\blenderneuron.zip' into 'C:\\Users\\robin\\AppData\\Roaming\\Blender Foundation\\Blender\\2.91\\scripts\\addons'
reloading addon: blenderneuron 1610983621.1903374 1610983643.3685155 'C:\\Users\\robin\\AppData\\Roaming\\Blender Foundation\\Blender\\2.91\\scripts\\addons\\blenderneuron\\__init__.py'
Modules Installed () from 'C:\\Users\\robin\\Documents\\GIT\\BlenderNEURON\\blenderneuron.zip' into 'C:\\Users\\robin\\AppData\\Roaming\\Blender Foundation\\Blender\\2.91\\scripts\\addons'
Info: Modules Installed () from 'C:\\Users\\robin\\Documents\\GIT\\BlenderNEURON\\blenderneuron.zip' into 'C:\\Users\\robin\\AppData\\Roaming\\Blender Foundation\\Blender\\2.91\\scripts\\addons'

Blender quit

From which it seems like everything went OK. My add-on does get installed, and I see it in the list, but it isn't enabled. How can I make sure that it is enabled as if someone went to &quot;Preferences &gt; Addons&quot; and clicked the checkmark there?
Enabling the addon modifies the user preferences, those need to be saved before Blender shuts down or the changes are lost the next time Blender starts. You can use bpy.ops.wm.save_userpref() to do so:
import bpy, os
p = os.path.abspath('blenderneuron.zip')
bpy.ops.preferences.addon_install(filepath=p, overwrite=True)
bpy.ops.preferences.addon_enable(module='blenderneuron')
bpy.ops.wm.save_userpref()