Installing Drupal 9 and Drush On Your Mac

Points To Remember before you start

  • See our article HERE to setup your Drupal(linux) server. Many of these commands also work on a mac
  • Check the composer requirements for the version of Drupal that you will install. 
  • For this article we will install Drupal 8 in the /srv/ directory
  • Install any package with sudo if you are not a root user
  • Don't run composer with sudo
  • This document was created September, 2019, but is updated by Simple Information staff regularly. 
  • If you get stuck, see the IN A JAM section at the bottom

Apache and PHP

There are many articles on how to install Apache and PHP on your mac so we're not going to cover that in detail here.

Here are some sites that we suggest for reference: 

https://jasonmccreary.me/articles/install-apache-php-mysql-mac-os-x/

Composer

Installing PHP Composer

1. Enter to the computer's terminal. 

2. Run the command below in the terminal:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" < /dev/null 2> /dev/null

3. If a password is required after running the command, please enter your Mac's user password to continue. Then, wait until the installation finish.

4. Run the command below in the terminal:

brew install curl

*Here's where life can get a little complicated. The latest version of composer doesn't always work with the latest version of Drupal 8/9. Make sure the composer version you install works with the Drupal 8 version that you will install. At the time of writing this article, Drupal 8.7 only supports composer version 1.7.0 although the latest version is 1.9.0 (https://getcomposer.org/download/)

At the time of writing this article Drupal 8.7 supports composer version 1.7.0

curl -sS -o composer-setup.php https://getcomposer.org/installer && php composer-setup.php --version=1.7.0

Now we make composer globally available to all users

mv composer.phar /usr/local/bin/composer

To check your composer version

composer -V

Drupal Setup

Downloading Drupal core using composer

If you've been using the root user, now is the time to switch to a regular user that can run composer. 

At this point you'll probably want to set your domain (ex. MyDomain.com) to point to your new site. Do do this, you'll need to create a Virtual host in apache.

Map your domain name to your local IP address

Add the following line to /etc/hosts file

127.0.0.1 MyLocalDrupalSite.net

Now when you request for MyLocalDrupalSite.net in your browser, it will direct the request to your local machine.

2. Activate virtual hosts in apache

Uncomment the following line (remove the #) in /private/etc/apache2/httpd.conf

#Include /private/etc/apache2/extra/httpd-vhosts.conf

3. Add the virtual host in apache

Add the following VHost entry to the /private/etc/apache2/extra/httpd-vhosts.conf file

<VirtualHost *:80>
DocumentRoot "/Users/username/Sites/MyLocalDrupalSite"
ServerName MyLocalDrupalSite.net
</VirtualHost>

4. Restart Apache
System preferences > “Sharing” > Uncheck the box “Web Sharing” – apache will stop & then check it again – apache will start.

Now, http://MyLocalDrupalSite.net will be served locally.

Run the following command from the directory for the Drupal root, /Users/username/Sites/MyLocalDrupalSite. (This command may take a few minutes to run; good time for a coffee?)

composer create-project drupal-composer/drupal-project:8.x-dev docroot --no-interaction

This will download the current dev version of the 'drupal-composer/drupal-project' project into a folder named 'docroot' and then it automatically executes composer install which will download the latest stable version of Drupal 8 and all its dependencies.
* if you get an error similar to: Your requirements could not be resolved to an installable set of packages....see our IN A JAM section. 

Now run following bellow command from docroot

composer install

*if you get a memory swap error, see our IN A JAM section. 
Now create a config directory in docroot

mkdir config
cd config
mkdir sync

To check if drupal installed or not, check for the core directory in web folder (ex. /var/www/html/docroot/web or /srv/docroot/web)

Note: There is  no need to install drush explicitly , Its comes with the drupal 8 installation in the “vendor/bin/drush” directory.

You may want to download contributed modules and themes using Composer

Run below command from docroot

composer require 'drupal/*modulename*:*version*'

Examples:-

composer require 'drupal/token:^1.5'
composer require 'drupal/simple_fb_connect:~3.0'
composer require 'drupal/ctools:3.0.0-alpha26'
composer require 'drupal/token:1.x-dev'

References

To know about composer version notation see below link

https://getcomposer.org/doc/articles/versions.md

To know about drupal installation in details see below link

https://www.drupal.org/docs/develop/using-composer/using-composer-to-install-drupal-and-manage-dependencies

Run Drupal

Now you should be able to see the fruits of your hard work by running your instance. 

http://MyLocalDrupalSite.net/core/install.php

IN A JAM

Composer Issues:

Issue : webflo/drupal-core-require-dev 8.8.x-dev requires behat/mink-selenium2-drive requires behat/mink-selenium2-driver 1.3.x

  • Just go to docroot and remove the following lines from composer.json below
  • "require-dev": { "webflo/drupal-core-require-dev": "^8.7.0" },
  • DO NOT RUN composer create-project again

Refer :

https://www.drupal.org/forum/support/upgrading-drupal/2019-01-20/composer-update-issue-867

Issue : If youre getting [ErrorException] proc_open(): fork failed - Cannot allocate memory. Run the below command one after another can fix your issue

Run this command one after another

/bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024
/sbin/mkswap /var/swap.1
/sbin/swapon /var/swap.1