Drupal: Programmatically installing and enabling modules in the .install file

To make configuration management easier, we decided to make sure that all behavior-related changes are in the source code repository. So when I needed to add the reCAPTCHA module to the project, I needed to figure out how to programmatically install and enable the module with update code in another module’s .install file.

Here is some sample code to do so:

 * Install and enable the captcha module.
function yourmodule_update_1() {
  $ret = array();
  variable_set('recaptcha_public_key', 'PUBLIC KEY GOES HERE');
  variable_set('recaptcha_private_key', 'SECRET KEY GOES HERE');
  $ret[] = array(
    'success' => true,
    'query' => 'Installed recaptcha module and enabled it',
  return $ret;
  • Just wondering how much this technique scales.

    We are been trying various techniques to automate the config replication with out manual work.

    So I would like to know how you handle when the configs get large and diverse. I think it will become a tedious job very fast.

    • We tend to not have very diverse changes, because there’s really only one site (multisite with Domain Access) for this codebase, and now there are two branches: trunk and devel. I’ll post more notes as we try working with the two branches.

      Doing configuration changes in code is certainly easier to deploy across multiple systems than trying to do everything through the web-based system, for sure. =) Putting things in the .install file means that it’s easy to remember what to do after an update, because I’ll always run update.php. You can automate as much as you can, then display messages about other updates required.

      With the naming system of Drupal’s .install update functions, though, you could get conflicts if more than one developer adds update functions to the same file without checking the other person’s changes. I guess people could update their copies and check before committing, but it might get tricky when you’re merging changes across branches. I’m sure Drupal developers have thought about this before. If we run into that problem or we think we’ll run into that problem, I’ll post my notes here.

  • Hi, this is my first time visiting your site. I am new to this website development and I am not in this profession, this is only my hobby. I am using Drupal with my site. I encounter many difficulties; like terminologies, codes, block visibility, user access, and even simple permission to a file. I made it though through persistence. I am glad I found your site via Google. I just want to keep in touch to developers in our country. I think your post is great, but I don’t have any idea on how to implement it.

  • Hi, thanks for the example. How do you install multiple modules while returning $ret()?

  • Pass drupal_install_modules your array of modules, and populate $ret accordingly. Although a better way to do this now is to use Features, which is much easier than hacking in code all the time.