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

> $config = \Drupal::configFactory()->getEditable('system.theme');  
> $config->set('default', 'test_theme')->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().

2 Answers 2


The current advice is to set this via config:

  ->set('default', $theme)

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:


I just want to contribute to this answer by adding that there's no need to worry about running both of these commands if the theme is already installed. It technically runs the theme_installer service underneath and checks beforehand for installed themes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.