Buy

Installation, Composer and Git!

Hello Drupal people! I'm Ryan, and I come from the magical world of Symfony, full of gumdrops, rainbows, interfaces, services, dependency injection and lollipops. Along with a few other oompa loompas, I lead the Symfony documentation team, so I may not seem like the most obvious person to be teaching you about Drupal 8. But Drupal 8 has taken a huge leap forward by using common coding patterns and libraries. This makes Drupal a lot easier and more accessible to a lot of people.

This series is meant for developers who have used Drupal before. Instead of learning how to use it, we're going to rip apart the layers and see how this machine runs. That's going to make you more dangerous and uncover possibilities you wouldn't otherwise know about.

Download D8

Start by downloading Drupal 8, which at this moment isn't quite released, but it will very soon! This unzips the file to my Downloads directory. I'll move it to a drupal8 directory. We can see all of our shiny new files here in my shiny PhpStorm editor.

The Built-in PHP Web Server

Move into the drupal8 directory. We need a webserver! But I'm not going waste time setting up Apache or Nginx locally. Instead, I'll use the built-in PHP web server. Start it by running php -S localhost:8000:

php -S localhost:8000

This serves files from this directory and will hang there until you stop it. I highly recommend using this to develop.

In the browser, navigate to http://localhost:8000. Hello Drupal 8 install screen! Pick the standard installation to get a few more features.

Fixing php.ini problems

On the next step, I have a problem: the xdebug.max_nesting_level setting in php.ini is set too low. Wah wah.

Bah, it's easy to fix. Go back to the terminal and open a new tab. Run php --ini:

php --ini

This will tell you where the php.ini file lives. Open it with your favorite editor. I like vim, because it gives me street cred.

Tip

In some setups (I'm looking at you OSX), there will be no value for "Loaded Configuration File". Usually, there is a file in the "Configuration File (php.ini) Path" directory, but it's named something like php.ini.development. Rename this file to php.ini and run php --ini again.

Search for the setting! It already exists in my file, so I'll set it to 256. If it doesn't exist in your file, just add at the bottom:

xdebug.max_nesting_level = 256

For this change to take effect, restart your web server. For us, hit control+c to kill the PHP web server and then start it again:

php -S localhost:8000

That fixes it! Type in your database details: I'll call my database d8_under_hood and pass root with no password for my super secure local computer.

Now go grab some coffee or a sandwich while Drupal does it's installation thing.

Ding! Give your site a clever name and an email address. Um, but enter your email, not mine. The super-secret and secure password I'm using is admin. Select your country and hit save.

Phew! I mean congrats! You now have a working Drupal 8 site!

Storing in Git and talking Composer

You know what I love most about a new project? Creating a new git repo. Seriously, how often do you get to type git init?

git init

In PhpStorm, you can see an example.gitignore file. Refactor-Rename that to .gitignore. Open it and uncomment out the vendor line to ignore that directory:

37 lines .gitignore
... lines 1 - 9
# Ignore core and vendor when managing dependencies with Composer.
# core
/vendor/
... lines 13 - 37

The project also has composer.json and composer.lock files:

45 lines composer.json
{
"name": "drupal/drupal",
... lines 3 - 5
"require": {
"composer/installers": "^1.0.21",
... line 8
},
... lines 10 - 32
"autoload": {
"psr-4": {
"Drupal\\Core\\Composer\\": "core/lib/Drupal/Core/Composer"
}
},
... lines 38 - 43
}

Composer is PHP's package manager, and it has changed everything in our world. If you aren't familiar with it, go watch our Composer tutorial! Seriously, you can use it in Drupal 7... we do in that tutorial...

Because of the composer.json file, you should not need to commit the vendor/ directory. You should also not need to commit the core/ directory where all of Drupal lives, due to some special Composer setup in Drupal. Another developer should be able to clone the project, run composer install and both vendor/ and core/ will be downloaded for them.

When I tried to do that, I had a little trouble with the core/ directory due to an autoloading quirk. Hey, it's not released yet, so there could be a bug. It's cool.

In another screencast, I'll show you the proper way to use Composer with Drupal. But for now it's safe to not commit the vendor/ directory at least. If you run composer install, it'll populate that directory correctly.

Zip back over to the terminal and run git add . and then git status:

git add .
git status

There are a lot of files in core/, so it will be nice to not have to commit those someday. But other than these core/ files, we're not committing much. A new Drupal "project" doesn't contain many files.

Finish this by typing git commit and typing in a clever commit message for your fellow contributors to enjoy. Done!

Please, Please use a Decent Editor

I have a secret to tell you that will make your Drupal 8 experience many times better: use a decent editor, the best is PhpStorm. Atom and Sublime are also pretty good. But if you use Notepad++ or open some directory explorer to dig for files manually, there will be no rainbows, Pixy Sticks or Gumball drops in your Drupal 8 experience. Your editor must be able to auto-complete, have a directory tree and have a keyboard shortcut to open files by filename. Ok, I've warned you!

PhpStorm Symfony Plugin = Joy

If you do use PhpStorm... which would make you my best friend... it has a Symfony plugin that plays nicely with Drupal too. Score! In Preferences, under plugins, click browse repositories and search for "Symfony". You'll find this awesome Symfony plugin that has over 1.3 million downloads! If you don't have this installed yet, do it. I already have it. After installing, it'll ask you to restart PhpStorm. Once it's open again, head back to Preferences, search for Symfony, and you'll find a new Symfony plugin menu. Make sure you check the Enable Plugin for this project box. Remember to check this for each new project.

This plugin will give you some pretty sweet autocompletion that's specific to Drupal and Symfony.

Sweet! We're up and running! Let's get into the code!

Leave a comment!

  • 2016-03-14 weaverryan

    Hi Oliver!

    It doesn't - it comes with *just* PHP. But, if you installed something like MAMP - which includes PHP, MySQL and Apache - you would/could still just run this command to use the php built-in web server instead of Apache. But in that case, MySQL would be running, and you could communicate with it.

    Or you can use Sqlite - it's one of the options during install (and then use the built-in PHP web server).

    Cheers!

  • 2016-03-14 Oliver Williams

    Does the php built in server come with a mysql database?
    On the Set up database page I get the message :
    "Failed to connect to your database server. The server reports the following message: SQLSTATE[HY000] [2002] No such file or directory."

  • 2016-01-19 weaverryan

    Glad you get it rocking! Thanks for sharing this :)

  • 2016-01-18 ddanielyan

    Thanks a lot for for the link. Using the drupal-compser template definitely creates a cleaner installation.
    I ended up with the following items in my .gitignore file:

    # Ignore core and vendor when managing dependencies with Composer.
    /vendor/
    web/core

    # Ignore configuration files that may contain sensitive information.
    web/sites/*/settings*.php
    web/sites/*/services*.yml

    # Ignore paths that contain user-generated content.
    web/sites/*/files
    web/sites/*/private

    This is still not perfect (why do i need to add all the /web/ files like index.php etc.) but definitely beats adding core to your own repository.

  • 2016-01-04 weaverryan

    Hey there!

    Hmm, I don't recall exactly, but I was using https://github.com/drupal-comp..., which certainly seems active. Are you using that? The Drupal Console can also be used to start a new project - but I haven't tried it yet to see if it does it via a Composer install. Have you tried that for a new install?

    Cheers!

  • 2016-01-02 ddanielyan

    at 4:35 you mention, you had issues with not committing the core directory. I ran into the same issue with 8.0 (final). Have you ever been able to solve this issue? What I do not understand that in composer.json core is clearly marked as "replace" but composer still does not install it :-(

  • 2015-12-08 weaverryan

    Hi Vladimir!

    Sorry for my late reply! On my machine, git uses vim for the commit messages. This might be different on your machine: nano is another common editor for git to use (the editor that git uses is configurable).

    For vim, I pressed esc+:+w+q+enter

    Esc exits editing mode, colon enters a command mode, then wq tells vim to "write and quit".

    For nano, you would type, and then hit ctrl+o then ctrl+x iirc.

    I hope that helps!

  • 2015-12-01 weaverryan

    Good catch - but we'll probably leave that one :). Call it an Easter Egg ;)

  • 2015-11-29 Pietrino Atzeni

    I know, easier said than done, but listing the "Editor Must", the graphics shows "1. autocomplete", "2. show a directory tree", and "2. keyboard shortcuts"... too many 2...