Setup A New Drupal Site From The Command Line With Git And Drush
Today I had to create a new Drupal installation and it has been a while that I had to create Drupal installation from scratch. So I thought about writing up this guide for future reference and find a way to create a complete new Drupal install entirely from the command-line to get as fast as possible to a state where I actually start working.
I wrote this guide alongside while creating a new Drupal installation. I’m sure that it’s far from perfect and of naturally YMMV, hence I’d be glad to read any advices in the comments.
This guide assumes you have:
- drush installed.
- git installed.
- love for the command-line.
- basic knowledge about drush, git and of course Drupal itself, since I don’t want to dive into the basics in this guide.
If you don’t know about git yet, stop reading and deal with that first. There are plenty of guides out there, that do a good job of providing a great introduction. Like these:
- The entire Pro Git book, written by Scott Chacon. HIGHLY recommended!
- git - the simple guide - no deep shit!
- Introduction to git by github.
- Got 15 Minutes and want to learn git? Learn git interactively.
Setting up your repository.
So let’s create a fresh repository
myproject and change into directory:
$ git init myproject && cd myproject
I like the idea of starting a new Drupal site from the core repository to be able to update and read the changelogs in git, to have it connected to the official repository to create patches, cherry-pick hotfixes and so forth. But that’s a lot of overhead you might not want.
You could also easily get the most recent Drupal version with
drush dl that lacks the kinda bloated overhead that comes with git and update via
drush upc when needed. Do whatever pleases you. If you chose the drush-way go ahead and skip the following section and jump to the optional “create a remote branch”.
Get the code
But I’ll go on and add the Drupal-core repository as a remote:
$ git remote add core git://drupalcode.org/project/drupal.git
And fetch the code into our repository:
$ git fetch core
Depending on your internet connection this might take a while, since it’s downloading the whole Drupal history, which I understand most of you wouldn’t need. You might want to add something like
; say "finished fetching files" to be notified when it has finished.
We don’t need to have the bleeding edge version, and the Drupal team made a good job of maintaining tags for the various versions of drupal. We can either check the list in the repository-browser or use the following command to list the tags concerning version 7 in the repository:
$ git tag -l '7.*'
Look at the list and find the most recent tag, which is at the time of writing this 7.15, and merge this into your master branch:
$ git merge 7.15
If you’re familiar with git, it should be fairly easy for you to merge the bleeding edge version of 7. I leave that up to you and prefer to stick with a stable release.
Optional: create a remote branch
For most projects you’re probably having a central repository on some server or something like github. For the latter I highly recommend the CLI
hub which can be easily installed via homebrew:
brew install hub. If
hub is available you can create a new (private) repository at github and add it as a remote:
$ hub create
This will setup a public repository at github.com/USER/FOLDERNAME. If you want a private repository at a different location, you can do so by:
$ hub create MyOrganization/RepoName -p
hub is a great too, you should have at least a peak at the manual to get an overview of the features it offers.
Great. At last, we push our current code base to our github repository and set it up as the default upstream so we can omit the
origin master in future pushes:
$ git push -u origin master
This will again probably take a while the first time.
Setting up Drupal.
drush helps us a lot to work with Drupal from our beloved command-line and can do the Drupal installation routine for us with its
site-install task. Have a look at the configuration options it offers you before you continue – you’ll want to add or change some values to your requirements:
Done? Okay, let’s continue and install the site:
$ drush si --db-url=mysql://root@localhost/myprojects --firstname.lastname@example.org --account-name=vortrieb --db-prefix=myproject_ --site-name=MyProject --email@example.com --locale=de-DE
Note that you could also get away with a simple
$ drush si. Drush will fall back to defaults for all omitted parameters - but I don’t recommend this for rather obvious reasons. It’ll take a moment and then echo your login credentials when it’s done.
Adding modules as git submodules using drush
Of course it would be nice to stay consistent and have the modules as submodules. Gladly drush supports this. But first you’ll have to have the
git_deploy module installed and enabled.
$ drush dl git_deploy $ drush en git_deploy
To add a drupal module as git submodule, use the
--gitsubmodule flag and set the package handler to
$ drush dl features --package-handler=git_drupalorg --gitsubmodule
It’s probably a good idea to set up an alias for that, if you do this often - which I assume, since you seem to be using Drupal.
Now everything is wired up to configure your webserver and get to work. You might also want to create your own git branches for development or features. That depends entirely on your workflow. However I highly recommend the flow that is used at github if you don’t work with releases.
Now add your modules and get to the fun parts!
Friday, 12. October 2012.