Create ScotchBox for Composer-based Drupal 8 with Drush 9

ScotchBox Composer Drupal 8

Setting up ScotchBox 3.5 (Ubuntu 16.04) for Drupal 8 using the official Composer template.

cd ~/scotch # assuming your ScotchBoxes live in ~/scotch folder
git clone scotchbox-NEW;
cd scotchbox-NEW;
# Increase CPU and RAM, add SSH forwarding, to allow using host SSH keys, start vagrant
sed -i.tmp 's/end/    config.ssh.forward_agent = true\n    config.vm.provider "virtualbox" do |v|\n        v.memory = 8192\n        v.cpus = 4\n    end\n\nend/' Vagrantfile;
vagrant up --debug;
vagrant ssh;

# set the timezone
sudo timedatectl set-timezone Europe/Copenhagen

# update .bashrc with better history and longer history, insert in ~/.bashrc:
echo "
# Bind up/down arrow to history search
bind '\"\e[A\":history-search-backward'
bind '\"\e[B\":history-search-forward'
# Settings for history function
export HISTFILESIZE=50000
export HISTSIZE=50000
export HISTCONTROL=ignoreboth:erasedups
export HISTIGNORE='&:e:c:l:ca:cd:cd -'
# ls aliases
alias la='ls -hal'
alias l='ls -hl'
alias l='ls -hl'
alias dst='drush st'
alias duli='drush uli'
alias dcr='drush cr'
alias dexp='drush config-export'
alias dimp='drush config-import --preview=diff'
" >> ~/.bashrc
source ~/.bashrc;

# add d8.local to virtualhost and restart
# NOTE: also add " d8.local" (without quotation) to /etc/hosts file on host machine (not vagrant)
sudo -i
echo "
<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/public/d8.local/web
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    ServerName d8.local
          <Directory "/var/www/public/d8.local/web">
              Options Indexes FollowSymLinks
              AllowOverride all
              Require all granted
" >> /etc/apache2/sites-available/000-default.conf
systemctl restart apache2.service

# remove global Drush 8 (Drupal 8 Composer-version includes Drush 9)
sudo rm /usr/local/bin/drush

# remove Composer
sudo rm /usr/local/bin/composer -rf
rm ~/.cache/composer -rf
rm ~/.local/share/composer -rf
rm ~/.config/composer -rf

# create ~/.local/bin for Drush and Composer, found with echo $PATH
mkdir -p ~/.local/bin

# install Composer
php -r "copy('', 'composer-setup.php');"
php -r "if (hash_file('SHA384', 'composer-setup.php') === '93b54496392c062774670ac18b134c3b3a95e5a5e5c8f1a9f115f203b75bf9a129d5daa8ba6a13e2cc8a1da0806388a8') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

# move to location in $PATH and check version, update if necessary
mv composer.phar ~/.local/bin/composer
composer --version
composer selfupdate -vvv

# fix "Failed to initialize global composer: Composer could not find the config file: ~/.config/composer/composer.json"
echo "{}" > ~/.config/composer/composer.json
composer selfupdate -vvv

# fix drush path with Drush launcher to avoid typing vendor/bin/drush
wget -O drush.phar
chmod +x drush.phar
mv drush.phar ~/.local/bin/drush
drush --version # should output "Drush Launcher Version: 0.6.0 - The Drush launcher could not find a Drupal site to operate on. ..." 

# fix Drupal Console path with Drupal Console Launcher to avoid typing vendor/bin/drupal
wget -O drupal.phar
chmod +x drupal.phar
mv drupal.phar ~/.local/bin/drupal
drupal --version # Should output "Drupal Console Launcher 1.8.0"

# download Drupal 8 with Composer
# recommended method: 
cd /var/www/public/
composer create-project drupal-composer/drupal-project:8.x-dev d8.local --stability dev --no-interaction
cd d8.local

drush site-install --db-url=mysql://root:root@localhost/d8 --account-name=admin --account-pass=content -y;

composer require 'drupal/admin_toolbar'
drush en admin_toolbar_tools -y;

composer require 'drupal/devel'
drush en devel devel_generate -y && drush devel-generate-content 20;

# set URI aka "\$base_url" and create one-time login
echo -e "DRUSH_OPTIONS_URI=http://d8.local" >> .env
drush uli --uri=http://d8.local

# URL:

# test SSH forwarding works
# ignore or Pass 'Yes' when The authenticity of host can't be established in Expect Shell script during Automation
# add: "-o StrictHostKeyChecking=no"
ssh -T -o StrictHostKeyChecking=no; # test if it works with Github
ssh -T -o StrictHostKeyChecking=no; # test if it works with Gitlab
# success message is "Hi your_username! You've successfully authenticated.