Setting up your Drupal development environment

People found the 7-minute impromptu braindump I gave at last night’s Drupal Toronto meetup useful enough to vote it the best talk, so I thought I’d write it up. =)

Development

  • Use source code control. No, seriously. Use it. Religiously. It’s going to save you someday. CVS is popular. Subversion is also easy to set up. It’s a must when you’re working with other people, but even if you’re on your own, it’s really handy to be able to go back to previous versions or to check your changes.
  • Get to know your integrated development environment (IDE). For example, I use Eclipse + PHP Development Toolkit (PDT) + Xdebug, and the keyboard shortcuts save me so much time and make it easier for me to think about my code instead of thinking about editing my code. F3 to jump to a function definition, Ctrl-Shift-R to open a file by typing part of the filename, Ctrl-space to complete text… I’ve also gotten XDebug integration to work, so I can step through code and examine variables without putting var_dump everywhere. Know your tools.
  • Add the XDebug extension to your PHP installation. Even if you don’t get XDebug integrated with Eclipse, you’ll like the new and improved var_dump and other debugging functions. I’ve heard good things about Zend Debugger, too.
  • Put your entire Drupal directory into your project in the IDE, not just your site-specific code. You can even check your entire Drupal directory into your source code control repository. This makes it easier to look up functions in Drupal core, not just your sites/all/modules directory. You _could_ version-control just your site-specific code (profiles/, sites/, etc.), but set-up becomes a little more complicated.
  • Use an installation profile to manage your configuration. Experiment with things using the administration interface, then create/update an installation profile that sets up a brand-new Drupal installation. This will save you lots of time and head-scratching when you need to reproduce your setup.
  • Make starting from scratch easier, and do it often. I have a Makefile that drops my database and recreates it, resets my settings.php, and even opens up a Firefox browser that uses my installation profile to set everything up from scratch. Drupal 5: Copy your settings.php to settings-default.php or something like that before you install your system, then copy settings-default.php over settings.php when you want to refresh your setup. Drupal 6: Delete settings.php when you want to refresh your setup.

Firefox

  • Use multiple profiles. You can start completely independent Firefox processes by using profiles. For example, you could have your default profile with all the bells and whistles, and a test profile with just FireBug. You can log in as different users, too. Use this command to start a new Firefox process: firefox -ProfileManager -no-remote
  • Install and use FireBug. Best web development extension ever. I use this to examine the document object model, debug my CSS rules, experiment with my CSS and HTML on the fly, and monitor the network requests.
  • Set up quick searches for drupal.org and php.net. You can define quick searches by creating bookmarks that use keywords and %s placeholders in Location:. Example: Do a quick Google-search of Drupal.org by adding a bookmark with Location: http://google.com/search?q=inurl:drupal.org+%s and Keyword: gd (short for Google Drupal). To use your new quick search, type Ctrl-L or Alt-D to get to the address bar, then type gd keywords. Set up ways to search php.net and drupal.org/project, too. =)

Multi-site / virtual hosts

  • Simplify switching between your computer and your remote server. If you’ve set up your local system for virtual hosts and you have a remote server for testing, you’ll want to be able to switch between testing locally and testing remotely. Set up your local system to respond to all the domain names for the site (ex: example.com, foo.example.com, bar.example.com) and a local subdomain (ex: local.example.com). Set up your remote server to respond to all the domain names for the site (example.com, foo.example.com, bar.example.com) and a remote subdomain (ex: test.example.com). Then set up your /etc/hosts (c:\windows\system32\drivers\etc\hosts on Microsoft Windows) to look like this:

    127.0.0.1 localhost local.example.com
    remote.server.ip.address test.example.com
    
    127.0.0.1 example.com foo.example.com bar.example.com
    #remote.server.ip.address example.com foo.example.com bar.example.com
    

    local.example.com will always resolve to your computer, test.example.com will always resolve to the other computer, and everything else depends on which line is active. # comments out a line. When you want to switch to testing on the remote server, add # to the beginning of the line for 127.0.0.1 example.com… and remove # from the line for your remote server. If you want to automate this even further, write a script that swaps different /etc/hosts files around.

Hope that helps! I’d love to find out what you’ve done to tweak your environment. Feel free to share your tips here!

  • Peppy

    oh hell yes on Firebug. that thing is a marvel. a definite godsend when coding html.

  • http://randell.ph/geeky/ randell

    Very helpful tips indeed!

    I’m still waiting for Firebug to work on Firefox 3 beta 5.

    The only tip I can share right now is to subscribe to Sacha’s feeds. XD

  • http://www.slackorama.com Seth

    Care to share your Makefile?

  • http://sachachua.com Sacha Chua

    Makefile:

    clean:
            cp sites/default/settings-default.php sites/default/settings.php
            mysql -u root --execute='DROP DATABASE IF EXISTS database_name; CREATE DATABASE database_name';
            chmod ugo+w sites/default/settings.php
    
    local: clean
            firefox http://yoururl.example.com/install.php?profile=profile_name
    

    I haven’t bothered to make the site, profile_name and database_name variables yet.

    make local drops the database and opens a browser window with my install profile.

    Sometimes I throw an svn update in there, too. =)

  • http://fairhaven.typepad.com rjh

    I’ve found “git” to be a marvelous all purpose tool for configuration management and machine synchronization. It leaves CVS, SVN, etc. in the dust. Any one able to be an Emacs hacker can figure out “git”. (It lacks that GUIness needed for the windows world. In fact, it’s only recently that the cygwin port was sufficiently stable that the bugs can be reliably avoided. Linux and Mac ports are much more mature.)

    I’m finding I use it as much for synchronizing multiple systems as I do for change management. My biggest two uses are:
    a) Maintaining a synchronized Emacs planner directory tree on three different systems (Mac, Linux, Windows). I just use whichever machine and let git manage resolving the changes. This still needs a few tweaks to get the remember logic properly fixed up to automatically assign non-conflicting new numbers for the notes on different systems.

    b) Maintaining a moderately large collection of documents (PDF, Doc, etc.) on those three systems. This is my personal library for manuals, articles, etc. so git doesn’t have to worry about files being edited. They just get organized, re-organized, copied, and added.

    It’s nice to let git do all the work for me, and convenient that if I accidentally destroy something, I can always recover it from the git history.

  • simoncpu

    git sucks, mercurial rocks!

    git sucks, mercurial rocks!

    git sucks, mercurial rocks!

    yeah, yeah i’m just trollling… =)

  • http://fairhaven.typepad.com rjh

    You are right simoncpu. All of the newer source management systems should be considered. What I find crucial is that it be one that supports a fully distributed change model instead of attempting to maintain one true master. As you get into cooperating groups with closely related but different goals you need the distributed change model. It lets you manage shared cooperative sources. When starting people new, put them onto that path.

    I just happen to like and use git. Ex-kernel hacker and all that. Delta management for big binaries matters a lot too. My 5GB of PDFs, etc. would break SVN. “git” hardly even notices.

  • simoncpu

    I’m just trolling, but I forgot _not_ to use my real nickname… waaaaaaaah….

    Feature-wise, I’m of the opinion that git and hg are both the same. I’m just siding with hg because you’re siding with git. wahahahaha… :)

  • http://www.compscigrad.com Jason

    Thanks for posting this on groups.drupal.org/toronto.. I’ve been using eclipse for years… only recently for drupal stuff..

  • http://dianiska.szofi.net snufkin

    install profile is a very useful thing, maybe it helps someone (as a starting point for your own profile perhaps) if i link the dev profile i started a while ago: http://drupal.org/project/drupal_developer

  • http://civicactions.com Gregory Heller

    check out this group on g.d.o http://groups.drupal.org/drubuntu

  • http://graviteklabs.com Chris Bryant

    Thanks for the nice list!

    The google/drupal search one is very useful and while it’s a matter of preference, I would recommend using “site” rather than “inurl” since it will yield more relevant results.

    For instance:

    http://google.com/search?q=site:drupal.org/project+%s

    Provides a nice list of relevant modules:

    http://www.google.com/search?aq=f&q=site%3Adrupal.org%2Fproject+jquery&btnG=Search

    Thanks again!

  • http://themegarden.org Drupal Themes Garden

    Nice article.

    Could you give us some tips on configuring Eclipse + Xdebug ?
    Thanks.

    • http://coldacid.net Chris Charabaruk

      Have you seen this article yet? It’s not great, but it’s the best I can find quickly.

  • http://coldacid.net Chris Charabaruk

    On the issue of source control, I’d like to recommend using Subversion, as well as tracking Drupal and third party modules via vendor branches. If you ever need to edit someone else’s code for your site (for example, to add your own custom tokens to Token, etc.) you’ll be glad you did this, so you don’t reinvent the wheel every time Drupal or a module is updated.

  • http://sachachua.com Sacha Chua

    Does Subversion take kindly to having multiple sources for revisions? We’re currently organizing our source code with one subversion repository that stores all the site-specific modules we’re working with, and I like being able to check out a particular revision of the project tree. I would also like to be able to easily bring in patches from the different third-party modules we’re using, though. I haven’t looked into whether subversion handles this nicely. I suspect git handles this well, but I haven’t looked into it in-depth.

    • http://coldacid.net Chris Charabaruk

      I’ve not used git so I can’t judge their differences. How do you mean multiple sources, btw? For us, I brought in sane SVN practises at work, at least in regards to vendor branches. Anything third party is checked into /vendor//current, and then copied to /vendor//. Bringing into trunk is another copy for initially bringing it in, then doing a three-way merge (current vendor version, previous vendor version, and working copy) when updating.

      For checking out a particular version, appending @ should do the trick.

      You know I’m not the best at teaching something… I tend to absorb knowledge but still can’t spread it around too well to others. The Subversion book is probably your best guide.

    • http://coldacid.net Chris Charabaruk

      I’ve not used git so I can’t judge their differences. How do you mean multiple sources, btw? For us, I brought in sane SVN practises at work, at least in regards to vendor branches. Anything third party is checked into /vendor/<product_name>/current, and then copied to /vendor/<product_name>/<version>. Bringing into trunk is another copy for initially bringing it in, then doing a three-way merge (current vendor version, previous vendor version, and working copy) when updating.

      For checking out a particular version, appending @<rev> should do the trick.

      You know I’m not the best at teaching something… I tend to absorb knowledge but still can’t spread it around too well to others. The Subversion book is probably your best guide.

  • http://www.codelobster.com Stas

    Please, check new IDE for Drupal:

    http://www.codelobster.com

    Regards,Codelobster Team.

    UPDATE: Sacha here! I just fixed a typo in the URL.

    • http://coldacid.net Chris Charabaruk

      I’d be interested in checking it out, but you guys must be so exceptionally new, the paint’s still drying on your DNS records. That, or you might want to check your hosting situation; the site is unavailable at the moment.

  • Guy Saban

    Another FireFox add-on that I have found very useful and I believe it will work well with FireFox Profiles is the Session Manager add-on.

    You can have various session organized in a specific profile. Say you have a profile setup for web development (which includes the add-ons you use for web development). With Session Manager running in that profile you can setup specific sessions organized according to client projects. So when working with customer X just open the session for customer X, etc.

  • Guy Saban

    F3 Shortcut in Eclipse PDT

    After pressing F3 to jump to the function definition is there another shortcut to return to the line from where it jumped.

  • Jeff

    Hi Sacha! Thank you for this wonderful insight.
    I’m new in Drupal and I’m still learning a lot from this particular post of yours after 2 weeks of rereading it.
    I really want to learn to set up a development/training environment for occasional/mobile code sprints the proper way. Using laptops/desktops.
    For further learning..
    Please recommend books/a book that particularly teaches these things you’ve/they’ve mention like:
    1. CVS
    2. Subversion
    3. integrated development environment
    4. Eclipse
    5. PHP Development Toolkit (PDT)
    6. Xdebug
    7. Makefile
    8. Firebug
    9. source code control
    10. git
    11. Emacs
    12. mercurial
    13. eclipse
    14. codelobster
    How to learn these stuffs?
    I’m currently looking at O’Reilly’s books(planning to buy) and at wikipedia.
    But still I want to know your recommendations. Thank you.

  • http://golchitech.blogspot.com Golchi

    This is really cool post. They are new things I have discovered !!!
    Keep on.

  • http://lichter.nl willem

    \ firefox -ProfileManager -no-remote \
    Does anybody know how I turn this into a nice application button that i can just click on in Ubuntu? Starting terminal and writing this down for every time I want to start firefox… Life’s just too short.

  • pokusnik123

    Hi, I am newbie to eclipse and I would like to use solution described above (…You _could_ version-control just your site-specific code (profiles/, sites/, etc.), but set-up becomes a little more complicated…) Can you help me how to setup it?

    I tried use solution at http://groups.drupal.org/node/2663#comment-23334 but I dont know how to setup xdebug to use with index.php

    Thank you for reply