diff --git a/src/Composer/Command/GlobalCommand.php b/src/Composer/Command/GlobalCommand.php
index 9e87c7e17..e13b8aaf2 100644
--- a/src/Composer/Command/GlobalCommand.php
+++ b/src/Composer/Command/GlobalCommand.php
@@ -13,6 +13,7 @@
namespace Composer\Command;
use Composer\Factory;
+use Composer\Util\Filesystem;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\StringInput;
@@ -75,8 +76,22 @@ EOT
// change to global dir
$config = Factory::createConfig();
- chdir($config->get('home'));
- $this->getIO()->writeError('Changed current directory to '.$config->get('home').'');
+ $home = $config->get('home');
+
+ if (!is_dir($home)) {
+ $fs = new Filesystem();
+ $fs->ensureDirectoryExists($home);
+ if (!is_dir($home)) {
+ throw new \RuntimeException('Could not create home directory');
+ }
+ }
+
+ try {
+ chdir($home);
+ } catch (\Exception $e) {
+ throw new \RuntimeException('Could not switch to home directory "'.$home.'"', 0, $e);
+ }
+ $this->getIO()->writeError('Changed current directory to '.$home.'');
// create new input without "global" command prefix
$input = new StringInput(preg_replace('{\bg(?:l(?:o(?:b(?:a(?:l)?)?)?)?)?\b}', '', $input->__toString(), 1));