Setting up a Vagrant Scotch Box with Drush support for Drupal in minutes

Fast Drupal with Vagrant Scotch Box

This is how I set up a new Vagrant Scotch Box with Drush support.

NOTE: After updating VirtualBox through the normal, daily Ubuntu update, it might be necessary to reinstall with the below commands.

Using NFS for the synced folders might make pages load faster. It is also recommended to run your virtual machines on an SSD hard drive, for increased performance.

Install Virtualbox, Vagrant and git

sudo apt-get install virtualbox nfs-kernel-server git

To use USB, you might also need to install the VirtualBox Extension Pack. Just click on the "Extension Pack All Platforms" link, and it should install itself into Virtualbox: https://www.virtualbox.org/wiki/Download_Old_Builds_5_1

Download the latest version of Vagrant: Go to https://www.vagrantup.com/downloads.html and find the latest version for Ubuntu (Debian 64-bit):

wget https://releases.hashicorp.com/vagrant/2.1.5/vagrant_2.1.5_x86_64.deb

Install Vagrant:

sudo dpkg -i vagrant_2.1.5_x86_64.deb

Optionally: install two useful vagrant plug-ins:

vagrant plugin install vagrant-vbguest

vagrant plugin install vagrant-notify

Download the Vagrant Scotch Box with Git

git clone https://github.com/scotch-io/scotch-box NEW-scotch-box;
cd NEW-scotch-box;

Activate NFS, it is much faster - https://www.jverdeyen.be/vagrant/speedup-vagrant-nfs/# - edit Vagrantfile, so that the file system uses NFS. Comment out the original config.vm.synced_folder line, like this:

# config.vm.synced_folder ".", "/var/www", :mount_options => ["dmode=777", "fmode=666"]

Uncomment this line, to activate NFS, to make it look like this:

config.vm.synced_folder ".", "/var/www", :nfs => { :mount_options => ["dmode=777","fmode=666"] }

Start up Vagrant and SSH into it - if you get a message about "VT-x is disabled in the BIOS", enable Virtualization in your BIOS, possibly under "Security" and try again:

vagrant up --debug;
vagrant ssh;

Install drush by running these commands from within Vagrant

# Browse to https://github.com/drush-ops/drush/releases and download the drush.phar attached to the latest 8.x release, in this example Drush 8.1.17.
wget https://github.com/drush-ops/drush/releases/download/8.1.17/drush.phar;
php drush.phar core-status;
chmod +x drush.phar;
sudo mv drush.phar /usr/local/bin/drush;
drush init -y;

NOTE: On cPanel you can't run sudo commands. In stead, show your PATHs with echo $PATH (in this example, ~/.local/bin was one of the paths), and run these two commands in stead:

mkdir -p ~/.local/bin;
mv drush.phar ~/.local/bin/drush;

Fix Locale problem, improve terminal and set timezone
Fix "locale: Cannot set LC_ALL to default locale: No such file or directory" problem. Run these two commands from the command line:

echo "export LANGUAGE=en_US.UTF-8">>~/.bashrc;
echo "export LC_ALL=en_US.UTF-8 ">>~/.bashrc;

Improve the terminal experience by adding this at the end of your ~/.bashrc file - it allows you to type the start of a previously entered command, and find the rest by pressing the "arrow up" key:

# Bind up/down arrow to history search
bind '"\e[A":history-search-backward'
bind '"\e[B":history-search-forward'

Update ~/.bashrc:

source ~/.bashrc;

Set the timezone, run these two commands from the command line:

echo "Europe/Copenhagen" | sudo tee /etc/timezone;
sudo dpkg-reconfigure --frontend noninteractive tzdata;

Install Drupal with Drush
Also, disable Update module because it slows Drupal down, download the Admin toolbar module, and generate some test content:

cd /var/www/public;
drush dl --verbose --drupal-project-rename=d8.local;
cd d8.local/;
drush site-install --db-url=mysql://root:root@localhost/d8_test --account-pass=content --yes --verbose;
drush st;
drush pmu update -y;
drush dl admin_toolbar -y && drush en admin_toolbar_tools -y;
drush dl devel && drush en devel devel_generate -y && drush generate-content 20;

Visit your new Drupal 8 site at: http://192.168.33.10/d8.local -- both user name and password is "admin" (without quotes).

Very fast Drupal in Scotch Box
This is the actual speed in the Scotch Box, when clicking around, about five times faster with NFS enabled.
Drupal installation running on a Vagrant Scotch Box
(Animated GIF made in Ubuntu with Byzanz: https://www.maketecheasier.com/record-screen-as-animated-gif-ubuntu/)

Skip Login during start up of Vagrant box
"To configure NFS, Vagrant must modify system files on the host. Therefore, at some point during the vagrant up sequence, you may be prompted for administrative privileges (via the typical sudo program). These privileges are used to modify /etc/exports as well as to start and stop the NFS server daemon."
From: https://www.vagrantup.com/docs/synced-folders/nfs.html
If you get tired of typing your password every time you vagrant up, just add a few lines to your sudo user by following the instructions on the page linked to above, under "Root Privilege Requirement". The message before the login prompt looks something like this "==> default: Preparing to edit /etc/exports. Administrator privileges will be required... vagrant sudo".

For all the commands to copy and paste for the terminal as well as for Scotch Box 3.5, see Create Scotch Box 3.5 with Ubuntu 16.04 and PHP 7 (free version).

Comments

Thanks for this! This was very helpful. Drush is working when I vagrant ssh into Scotchbox, but I still get a database error when I try using a Drush command in the Terminal. I get:
PDOException: SQLSTATE[HY000] [2002] No such file or directory in [error]
/Users/path/to/Drupal/public/core/lib/Drupal/Core/Database/Driver/mysql/Connection.php:142

Would appreciate any pointers on solving this!

I have made it more clear that you need to uncomment the NFS config.vm.synced_folder line, but also comment out the original config.vm.synced_folder line. Perhaps this was the reason of the error?

I just created a fresh Scotch-Box and it seems to work:
vagrant@scotchbox ~ $ cd /var/www/public/d8.local/;
vagrant@scotchbox /var/www/public/d8.local (master *=) $ drush st
Drupal version : 8.2.6
Site URI : http://default
Database driver : mysql
Database hostname : localhost

... etc.