How to set the default theme programmatically I've tried to set a theme as default theme in drupal 8 using the following code

&gt; $config = \Drupal::configFactory()-&gt;getEditable('system.theme');  
&gt; $config-&gt;set('default', 'test_theme')-&gt;save();


On installing the module the code is not setting the theme as the one I've entered. 
The code is written in hook_install().
The current advice is to set this via config:

\Drupal::configFactory()
  -&gt;getEditable('system.theme')
  -&gt;set('default', $theme)
  -&gt;save();


The previous method using ThemeHandlerInterface::setDefault() is now deprecated.

You might need to install it first too, I'm not sure. In case you do:

\Drupal::service('theme_installer')-&gt;install(['test_theme']);