How I build an Ubuntu 10.04 LTS Drupal Development VM in VirtualBox

A development virtual machine can be really handy. It gives you a sandbox of sorts where you can feel free to test and experiment knowing that in a worst-case scenario you can just delete the VM and start over. It can also be a great way to practice server configurations and sketch out "real-world" server setups. Here's the process I follow to setup my Ubuntu 10.04 LTS Drupal development VM in VirtualBox. Aside from the VM-specific steps, these instructions should work for a regular Ubuntu server (VM or not).

Download and install Ubuntu 10.04

Note: Unless otherwise specified, all commands below should be run on the guest VM (Ubuntu).

If you haven't do so already, download and install VirtualBox. Then, download the Ubuntu 10.04 LTS disk image to your host computer.

Next, Create a new virtual machine in VirtualBox. Be sure to select type of "Linux" and OS Type of "Ubuntu" and 32 or 64 bit, accordingly.

Start the machine and, when prompted to select a disk drive, click the icon to the side and open the Virtual Media Manager. Add the ISO file you downloaded to the list of disk images and select it as the media to mount. Walk through the install wizard. When asked to install software, don't choose any packages. We'll add them later. When asked to install the GRUB boot loader, go ahead and choose "yes." Once installation is complete, click on the CD icon in the VM window and unmount the disk image, then choose "Continue" to reboot into the Ubuntu server VM.

Update, upgrade, and setup SSH

Before we do anything else, let's get comfortable in the environment. Working in the small shell window that VirtualBox provides isn't ideal, and it'd be nice to be able to do the rest of the setup over SSH in the terminal of our choice. Install the OpenSSH Server package, upgrade existing packages and then shutdown the server (because we'll have likely updated "linux-headers-server" and other core OS packages, and also so we can do the next step).

If you'd like to make things easier on yourself and not have to type "sudo ..." for every command, run sudo -u root -s to become root for the rest of the session. Otherwise, be sure to add "sudo" before each of the follwing commands.

apt-get install openssh-server
apt-get update
apt-get upgrade
apt-get dist-upgrade
shutdown -h now

Get in the habit of running apt-get update and apt-get upgrade regularly. It's one way you keep your Ubuntu server patched and secure! apt-get dist-upgrade should be run if you see "The following packages have been kept back..." when you run apt-get upgrade. This means that some packages haven't been upgraded because they involve core, operating system-level updates that require a restart of the machine.

Configure networking on the host machine

On the host machine, run the following commands below. The first line sets up a handy environment variable you can use later. Just replace "Ubuntu 10.04 LTS" with whatever name you gave your VM when setting it up in VirtualBox. The commands below assume you haven't changed your network adapter type in VirtualBox from the default. If you have, you'll likely need to change the device name.

ubuntu_vm="Ubuntu 10.04 LTS"
VBoxManage setextradata "$ubuntu_vm" "VBoxInternal/Devices/e1000/0/LUN#0/Config/guestssh/Protocol" TCP 
VBoxManage setextradata "$ubuntu_vm" "VBoxInternal/Devices/e1000/0/LUN#0/Config/guestssh/GuestPort" 22 
VBoxManage setextradata "$ubuntu_vm" "VBoxInternal/Devices/e1000/0/LUN#0/Config/guestssh/HostPort" 2222
VBoxManage setextradata "$ubuntu_vm" "VBoxInternal/Devices/e1000/0/LUN#0/Config/guesthttp/Protocol" TCP 
VBoxManage setextradata "$ubuntu_vm" "VBoxInternal/Devices/e1000/0/LUN#0/Config/guesthttp/GuestPort" 80
VBoxManage setextradata "$ubuntu_vm" "VBoxInternal/Devices/e1000/0/LUN#0/Config/guesthttp/HostPort" 8888

Now, start the VM back up. You should now be able to SSH into your Ubuntu VM as follows:

ssh localhost -p 2222

Install Apache, PHP and MySQL

Now, install some generally useful packages that will be used for the rest of the process.

apt-get install build-essential git-core subversion cvs python-software-properties curl

Before we get to the web server setup, we need to change Ubuntu's package definitions a bit. Ubuntu 10.04 LTS is configured to pull PHP 5.3.x, but many Drupal 6.x contrib modules aren't compatible with PHP 5.3, so we need to downgrade to PHP 5.2.x. We'll follow some instructions from Khalid at 2bits. We'll take his Approach #3 by adding Ralph Janke's PHP 5.2 repository for Lucid and "pinning" the PHP version to 5.2, to make sure we don't inadvertantly upgrade to 5.3 (I've provided the apt preferences file in my Github repository, for easy access).

add-apt-repository ppa:txwikinger/php5.2
wget http://github.com/jrbeeman/drupal-patches/raw/master/ubuntu-10.04-apt-php-prefs.txt -O /etc/apt/preferences.d/php
apt-get update

Now, we'll install the web server, PHP packages (including APC), and MySQL. Please note that you'll be prompted for a MySQL root user password. The last command below also disables some unneeded Apache modules and enables a few that are good to have for Drupal.

apt-get install apache2 php5 php5-cli php5-gd php-pear php5-dev apache2-dev
apt-get install mysql-server php5-mysql
a2dismod cgi autoindex
a2enmod deflate expires rewrite vhost_alias
pecl install apc

Configure Apache and PHP

Update PHP settings by editing both /etc/php5/apache2/php.ini and /etc/php5/cli/php.ini and setting the variables below as shown:

safe_mode = Off
expose_php = Off
memory_limit = 128M
display_errors = Off
log_errors = On  
error_log = /var/log/php/php.log  ; (or php_cli.log)

Update APC settings by editing /etc/php5/conf.d/apc.ini:

extension=apc.so
apc.enabled=1
apc.enable_cli=1
apc.shm_segments=1
apc.shm_size=32
apc.cache_by_default=1
apc.stat=1

Create the error log files:

mkdir /var/log/php
touch /var/log/php/php.log
touch /var/log/php/php_cli.log
chown -R www-data:www-data /var/log/php
chmod -R 0755 /var/log/php

Due to the way Drupal handles URLs, in particular how it handles creating a URL to itself, we need to tell Apache to listen on port 8888. This gets around the "HTTP Request Fail" issue in the status report, among other things:

Edit /etc/apache2/ports.conf and add Listen 8888, so that the beginning of the file looks like:

NameVirtualHost *:80
Listen 80
Listen 8888

Install and configure Drush

Setup Drush and Drush Make to be used server-wide. The commands below grab the latest verison as of this writing, but you can check the Drush and Drush Make project pages to see if there are newer releases:

cd /usr/local/src
wget http://ftp.drupal.org/files/projects/drush-6.x-3.3.tar.gz
wget http://ftp.drupal.org/files/projects/drush_make-6.x-2.0-beta8.tar.gz
tar xzf drush-6.x-3.3.tar.gz
tar xzf drush_make-6.x-2.0-beta8.tar.gz
mkdir /etc/drush
cp /usr/local/src/drush/examples/example.drushrc.php /etc/drush/drushrc.php
ln -s /usr/local/src/drush/drush /usr/local/bin/drush

Edit /etc/drush/drushrc.php to add Drush Make to include path:

$options['i'] = '/usr/local/src/drush_make';

Install Drupal

Create a database, download, and install Drupal (be sure to put a password in for the MySQL grant statement where appropriate below). I like to keep my sites in sub-directories of /var/www. These commands will prepare Apache for that working setup. I've provided a couple of files that are downloaded with wget to make this simpler, but feel free to modify them along the way if you wish.

mysql -u root -p -e "CREATE DATABASE drupal_dev; GRANT ALL PRIVILEGES ON drupal_dev.* TO drupal@localhost IDENTIFIED BY 'password'; FLUSH PRIVILEGES;"
wget http://github.com/jrbeeman/drush-make/raw/master/drupal-demo.make -O /var/www/drupal-demo.make
wget http://github.com/jrbeeman/drupal-patches/raw/master/ubuntu-drupal-demo_apache2-conf -O /etc/apache2/sites-available/drupal-demo
a2dissite 000-default
a2ensite drupal-demo
chown -R www-data:www-data /var/www
sudo -u www-data drush make /var/www/drupal-demo.make /var/www/drupal-demo
sudo -u www-data cp /var/www/drupal-demo/sites/default/default.settings.php /var/www/drupal-demo/sites/default/settings.php
/etc/init.d/apache2 restart

Walk through Drupal's install wizard by visiting http://localhost:8888. Once you're done, run the following Drush command to quickly enable the modules in the demo site:

cd /var/www/drupal-demo
drush en -y admin \
  content content_copy fieldgroup filefield imagefield nodereference \
  number optionwidgets text userreference bulk_export ctools \
  ctools_custom_content page_manager views_content path search devel \
  features imageapi imageapi_gd imagecache imagecache_ui masquerade \
  pathauto skinr strongarm token panels_mini panels_node panels \
  jquery_update vertical_tabs views views_bulk_operations views_export \
  views_ui fusion_core

Wrap-up

You're done! From here, you've got a fully-functional Ubuntu virtual machine designed for developing Drupal sites.

Todo

There are some issues to work out and other tasks to add to these instructions. Any feedback on these items would be greatly appreciated.

Issues

  • Drupal reports that it can't make HTTP requests, but it can. I believe this is because we're making requests over port 8888, but Drupal thinks it's running on port 80. Not sure how to get around this, but also not sure it's really all that important for development environments.

Tasks

  • TODO: Install and configure Aegir
  • TODO: Install and configure Varnish
  • TODO: Install and configure Solr

References

Comments

Go into the settings.php
and somewhere it says 'Uncomment below to set the host.'
Add the port to the host and uncomment it.

Thanks for the time to write down these steps. I think it's easier to configure than to make work drush in Windows and Cygwin.

Do you develop in that VM, or it's only to run the LAMP ?

There is a simmilar project with VirtualBox, ready to download. I find it quite useful http://drupal.org/project/quickstart

Hey, very cool project! Thanks for letting me know about it. I typically end up developing within the VM.

Thanks for another great tutorial Jeff. One snag I had was that I needed to run apt-get update after adding the php 5.2 apt repo. Other than that, worked like a charm.

Thanks, Roger. I forgot to write that down! I've updated the instructions accordingly.

Nice and clean. Thanks.

Do you maybe know similar "howto", but for RHEL/CentOS ?

I haven't seen one yet, but I haven't really looked, either. I believe, for the most part, the package install sections could be modified to use yum instead. And, you'd probably not need to worry about the PHP 5.2 backport like you need to with Ubuntu 10.04 LTS.

You could also check out the Drubuntu project - http://drupal.org/project/drubuntu, which is a Drush script that sets up a multi-project development box on Ubuntu.

To sort out your issue to make request over port 8888 type this in terminal:
nano /etc/apache2/sites-enabled/drupal-demo

then change port for VirtualHost *.80 to *:8888

restart apache2 and you have done ;-)

when I run the command:
sudo -u www-data drush make /var/www/drupal-demo.make /var/www/drupal-demo

I get the following:
A Drupal installation directory could not be found [error]
The drush command 'make /var/www/drupal-demo.make [error]
/var/www/drupal-demo' could not be found.

First time I ran the command it complained about PEAR Console_Table library, so I installed it, and copied the Table.php to table.inc in the directory requested.

I had no issues up until this command, any idea where to check?

Did you do the step, "Edit /etc/drush/drushrc.php to add Drush Make to include path"...?

i did the path edit
having the same problem.

cd: 22: can't cd to /root
A Drupal installation directory could not be found [error]
The drush command 'make /var/www/drupal-demo.make [error]
/var/www/drupal-demo' could not be found.

Make sure when you Edit /etc/drush/drushrc.php to add the path to remove the comment (# sign). I was having the same issue you are experiencing but it worked after I removed the # sign.

i did remove the # sign.(of course)
still no luck

Drush needs a copy of the PEAR Console_Table library in order to [error]
function, and the attempt to download this file automatically failed
because you do not have permission to write files in
/usr/local/src/drush/includes. To continue you will need to download
the 1.1.3 package from http://pear.php.net/package/Console_Table,
extract it, and copy the Table.php file into Drush's directory as
/usr/local/src/drush/includes/table.inc.
Drush could not execute.

I was able to resolve the "HTTP Request Fails" issue by telling Apache to listen on port 8888, but operate over port 80. This makes sense, now that I think about it. Due to the way I configured VirtualBox to route port 8888 to port 80 on the VM, the Apache on the VM sees all requests as coming over port 80, but Drupal sees the request as coming over port 8888 because it parses the port from the URL.

I've updated the instructions accordingly.

I followed all the instructions above, I performed the installation on my regular ubuntu desktop and not a virtual machine. I was able to successfully complete the above steps but when I try to restart the server I get the following message:

/etc/init.d/apache2 restart
* Restarting web server apache2 [Thu Aug 26 20:06:52 2010] [warn] NameVirtualHost *:80 has no VirtualHosts
... waiting [Thu Aug 26 20:06:53 2010] [warn] NameVirtualHost *:80 has no VirtualHosts

What step am I missing?

Awsome guide, worked like a charm. A few plugins missing for me though, what is the best way to get curl and mcrypt for this installation? Still a linux newbie.

Assuming you're looking for curl and mcrypt for PHP, you can install those by running:

apt-get install php5-curl php5-mcrypt

I am new to MACs, Virtual Box and Ubuntu, but originally got VBox and Ubuntu set up, and have recently updated it to 10.04.

Following your instructions, everything went OK, until I attempted to configure networking on the Host machine. I made the mistake of assuming that Virtual Box would update the version name of Ubuntu...but it was still set to Ubuntu 9.10, and when I ran you commands with the Ubuntu 10.04 LTS, then tried to start up Ubuntu in VirtualBox, I now get an error....And it doesn't matter what I set the name to, or what I run in the command line, I can't get in.

Error: Faired to open a session for the virtual machine Ubuntu 9.10 (same error no matter what I name it, now).

Configuration error: Failed to get MAC Address (VERR_CFGM_VALUE_NOT_FOUND)

Can you help me please? How can I salvage this virtual machine?

You may need to re-run the section "Configure networking on the host machine" using the correct hostname of your VM. The first line of that code snippet, ubuntu_vm="..." and set that value to the name of your VM. This command should be run on the host computer (e.g. your Mac).

Thank you. I did try that, but I may not have done it correctly. I will try again. WCW

This is great! Thank you for posting. What are your thoughts about the warnings in GD Image Filtering and GD Image Rotation in admin/reports/status? I have built this environment previously on my own using Ralph Janke's PHP 5.2 repository for Lucid and saw the same warnings, so I assume the warnings are consistent. I don't mean to complain. I would just like to know if I should be concerned.

Zend Server CE PHP 5.2 doesn't produce those same warnings. But, of course, one is somewhat locked in when using Zend Server.

Thanks

Hi, this is very much like what I did with Drupal Quickstart. You can download the vm image from the project page. In addition, I created some drush scripts and installed a bunch of extras (debugging, profiling, etc).

http://drupal.org/project/quickstart

Nice posts! I'll check them out and see what's inspiring for Quickstart.

Cheers,

Mike

Hi, this is very much like what I did with Drupal Quickstart. You can download the vm image from the project page. In addition, I created some drush scripts and installed a bunch of extras (debugging, profiling, etc).

http://drupal.org/project/quickstart

Nice posts! I'll check them out and see what's inspiring for Quickstart.

Cheers,

Mike

Jeff,
thank you for the posted advice and how to's.

Virtual Box/VM and Quickstart was an eay install.
Getting the ThemeForest.net template to install is next to impossible for me..
http://themeforest.net/item/converge-the-best-premium-drupal-theme/405072

Any suggestions? Their help files seem to be missing or innacurate.

THANKS!

This is great! Thank you for posting. What are your thoughts about the warnings in GD Image Filtering and GD Image Rotation in admin/reports/status? I have built this environment previously on my own using Ralph Janke's PHP 5.2 repository for Lucid and saw the same warnings, so I assume the warnings are consistent. I don't mean to complain. I would just like to know if I should be concerned.

Zend Server CE PHP 5.2 doesn't produce those same warnings. But, of course, one is somewhat locked in when using Zend Server.

Thanks

Free Game Downloads