Install Composer & Drush 8 On Shared Hosting

Author:
admin
Created:
Monday, November 30th, 2015
Last Updated:
Tuesday, February 28th, 2017

Disclaimer: Accessing the information on this page means you agree to the Sites Terms of Service


Check out the updated easy way to install composer in a shared hosting environment blog

The Intro to Composer & Drush

Drupal 8 is officially out and it's time to learn composer! Drupal 8 has included a lot of various dependencies on other projects across the web, and one of them is the use of composer which is a PHP dependency manager. It's like yum or apt-get, but not like them at the same time...

All of the install directions for composer (and drush), assume you are made out of money and have your very own private VPS or even better, full hardware server at your disposal... All of the install instructions point to installing the components at a global level, so the entire server has access to use them. The problem, is that shared hosting servers don't like people installing global stuff...

So now where? Well, I decided to start tackling Drupal 8 and without Drush, life ceases to exist, so the second step to getting a Drupal 8 site going, is to install the wonderful backened CLI manager Drush. In order to run drush for Drupal 8 though, you first need to have composer and use composer to download and install Drush 8 for you. Drush 8 is the only version that works on Drupal 8 and you guessed it, Drush 8 can only work if composer is installed... What used to be a simple download, add some .bashrc info and you were off and running, has now turned into yet another learning curve for yet another project that has absolutely nothing to do with Drupal, but you'll find you can't use Drupal without it. Welcome to Composer.

Using a few pieces across the web, I was able to quite easily and quickly piece together composer and drush so they would work within the home directory of my shared hosting server. Let's get started!

Step 1 - Installing Composer

The first step, is to install Composer.

1) Assuming you are logged into the server through SSH, create a bin directory in the root of your home folder.

For example:

mkdir ~/bin

or

mkdir /home/youruser/bin

(Note: Some hosting companies have run out of storage, and moved home folders to a different location such as /home1 as in my case. do a "pwd" at the root of your home folder to see what your full path is, but in most cases 'mkdir ~/bin' will work)

2) Now that you have a bin folder created, we need to download and stick composer into it.

Hopefully you have the curl command available. Some hosting providers don't give access to curl, but they do give access to wget.

curl -sS https://getcomposer.org/installer | php -- --install-dir=~/bin --filename=composer

This will download the composer file into your ~/bin folder, with the executable file name composer

3) Next we need to add an alias in your .bashrc (linux) .bash_profile (MacOS) file, to run the composer file from command line. I use linux, so all references will be made to .bashrc. Just remember to substitute for .bash_profile if you're on a Mac.

vi ./.bashrc

If you aren't familiar with the vi (vim) command, you'll have to brush up on it first.

Normally, your .bashrc file will look something like this:

# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

Inside the .bashrc file, we'll need to add the following export path:

export PATH="$HOME/bin:$PATH"

The final output will look like:

# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

# User specific aliases and functions
export PATH="$HOME/bin:$PATH"

using export= is different than creating an alias. Alias' are usually tied to a single file, whereas the export= will allow you to run any file within the target directory. So, you can have a lot more than just composer inside your /home/user/bin folder and you'll be able to run any of those commands.

Save the .bashrc file and then reload it with the command:

source ~./.bashrc

4) Composer should be installed and up and running.

Test it out by typing composer in the command line. If it is installed correctly, you'll see the help commands that are available to use with composer, something such as:

   ______
  / ____/___  ____ ___  ____  ____  ________  _____
/ /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 1.0-dev (b6bac0c2d31e4f84d607638277322d8d256e61af) 2015-11-29 17:21:38

Usage:
  command [options] [arguments]

Options:
  -h, --help                     Display this help message
  -q, --quiet                    Do not output any message
  -V, --version                  Display this application version
      --ansi                     Force ANSI output
      --no-ansi                  Disable ANSI output
#
#
#
  selfupdate      Updates composer.phar to the latest version.
  show            Show information about packages
  status          Show a list of locally modified packages
  suggests        Show package suggestions
  update          Updates your dependencies to the latest version according to composer.json, and updates the composer.lock file.
  validate        Validates a composer.json and composer.lock

Great. Now for Drush. More on the composer installation can be found at: https://getcomposer.org/doc/00-intro.md

Step 2 - Installing Drush

Disclaimer: This post was originally written before the drush.phar was available (or atleast known to me). The newer drush docs suggest how to install and use drush.phar which is php's version of a compressed php file and in all honesty, is an easier thing to deal with initially. If you want to use composer to handle the install and update of drush though, read on.

Now that we have composer installed, we can proceed to installing Drush. As a disclaimer, read through the composer documentation and familiarize yourself with how composer works. You're going to need it later for more involved Drupal 8 stuff.

1) If you're used to older versions of Drush, you probably think you can just download it and follow the readme file. Not the case with drush 8. It requires that you install drush using composer, which downloads drush automatically for you.

Since composer will download drush automatically, which version should you get?? Well, you can run the command without specifying a version, and it will grab the latest stable, which is 8.0.1 as of this writing. Otherwise, if you wanted 7, you could choose that, or 6 or even the dev-master. I'll show you how to grab the current stable first, then link over to the drush docs for info on grabbing other versions.

Let's run the following command to have composer grab the latest stable version of Drush for us:

composer global require drush/drush

This will output some stuff on the screen such as:

Changed current directory to /home/myuser/.composer
Using version ^8.0 for drush/drush
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Installing pear/console_table (1.2.1)
    Downloading: 100%

  - Installing symfony/var-dumper (v2.7.7)
    Downloading: 100%

  - Installing symfony/yaml (v2.7.7)
    Downloading: 100%

  - Installing jakub-onderka/php-console-color (0.1)
    Downloading: 100%

  - Installing jakub-onderka/php-console-highlighter (v0.3.2)
    Downloading: 100%

  - Installing dnoegel/php-xdg-base-dir (0.1)
    Downloading: 100%

  - Installing nikic/php-parser (v1.4.1)
    Downloading: 100%

  - Installing symfony/console (v2.7.7)
    Downloading: 100%

  - Installing psy/psysh (v0.6.1)
    Downloading: 100%

  - Installing drush/drush (8.0.1)
    Downloading: 100%

pear/console_table suggests installing pear/Console_Color2 (>=0.1.2)
symfony/var-dumper suggests installing ext-symfony_debug ()
symfony/console suggests installing symfony/event-dispatcher ()
symfony/console suggests installing symfony/process ()
symfony/console suggests installing psr/log (For using the console logger)
psy/psysh suggests installing ext-pcntl (Enabling the PCNTL extension makes PsyS                                     H a lot happier :))
psy/psysh suggests installing ext-readline (Enables support for arrow-key histor                                     y navigation, and showing and manipulating command history.)
psy/psysh suggests installing ext-pdo-sqlite (The doc command requires SQLite to                                      work.)
drush/drush suggests installing ext-pcntl (*)
drush/drush suggests installing drush/config-extra (Provides configuration workf                                     low commands, such as config-merge.)
Writing lock file
Generating autoload files

2) Assuming you don't have any errors at this point, we need to go edit the .bashrc (linux) .bash_profile (MacOS) file again and add another export:

export PATH="$HOME/.composer/vendor/bin:$PATH"

Now your .bashrc file will look something like:

# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

# User specific aliases and functions
export PATH="$HOME/bin:$PATH"
export PATH="$HOME/.composer/vendor/bin:$PATH"

3) Reload the file using:

source ~./.bashrc

and you should be good to test out drush using:

drush status

If everything went right, you'll be presented with the list of configuration info for drush.

Additional info on installing (or uninstalling) Drush can be found at: http://docs.drush.org/en/master/install-alternative/

4) Finally, initialize drush (Optional... read notes for potential problems)

Run the command:

drush init

This will create some config files in a ~/.drush folder and update your .bashrc file. You need to re-edit your .bashrc file and move the new include information added by drush. You'll find:

# Include Drush prompt customizations.
. $HOME/.drush/drush.prompt.sh

# Include Drush completion.
. $HOME/.drush/drush.complete.sh

# Include Drush bash customizations.
. $HOME/.drush/drush.bashrc

Move this to the BOTTOM of your export area. Your export stuff needs to be called on before it includes the drush files, otherwise you'll end up with the "which" command not being able to find drush in any path locations whenever you fire up your shell terminal.

Once this is done, you will need to go into your .drush folder and run the following commands:

chmod u+x drush.prompt.sh

chmod u+x drush.complete.sh

Another thing to note about drush init:
Prior to 8.0.2, this can break your ssh login. The symptom is that your ssh terminal will log you off or close the ssh session after you source the file and then every time you try to login. I know from experience. If this happens, you can try logging into FTP and update the file, login to your hosting dashboard and use their file manager, or you'll have to contact the provider and have them fix the .bashrc file.

Specifically, after running 'drush init' my .bashrc was updated to point to a few scripts in the .drush folder which we covered above. The include to ~/.drush/drush.drushrc is what broke my shell login. After researching it further, that file must be renamed anyway. It's for creating your own alias' and stuff, so chances are you can just comment or delete the line that points to it and be done. It might have messed up on me because I didn't realize until after this issue, that the include lines point to the drush scripts which need to be run after the export lines... Again, this was a bug pointed out prior to 8.0.1 and it is supposed to be fixed in 8.0.2 by adding the lines to the end of the .bashrc file. Either way, you're fore-warned.

5) This is an optional note, but it needs to be covered.

Drupal 8 now requires a minimum of PHP 5.5 to run. Drush, requires a minimum of PHP 5.4 to run. While Drush 8 WILL work fine for D6 and D7, If you're in a mixed mode environment with all sorts of versions of Drupal, then your base PHP version might be less than PHP 5.5, which means you've forced your D8 install .htaccess to PHP 5.5, but drush won't run for that D8 install.

Long story short, if you're using a mime handler to force your D8 install to run on PHP 5.5, but everything else is on 5.4 or older, then you MUST add an alias to your .bashrc file to force Drush 8 to use PHP 5.5:

alias drush='DRUSH_PHP=/opt/php55/bin/php $HOME/.composer/vendor/drush/drush/drush'

Note the '/opt/php55/bin/php'. This is where my php 5.5 files are located. If that isn't the right location for you, check your phpinfo file from your Drupal 8 install. It should point you in the right direction. (Reports -> Status -> PHP -> More Information)

Step 3 - Updating Composer & Drush

The beautiful thing about composer, is that you can run a simple command to update everything, or update a particular package. If there's a new version of Drush available and you want it installed, simply run:

composer global update drush/drush

Notice the 'global' option. Running the 'global' option with composer, will put composer into your ~/.composer/vendor folder for checking packages. Otherwise, running composer by itself, installs packages for the individual project (website) you are working on.

To update composer, simply run:

composer global selfupdate

Post Comment

Comments

First, thank you for this.

Second, I get to step three without any errors but when I "drush status" I get command not found.