Installing Composer on a Dreamhost VPS, or perhaps other VPS's

; Date: Fri Jan 12 2018

Tags: PHP

Modern PHP software development has been modernized by Composer. My exposure to Composer is because the Drupal team adopted the Symfony framework, which in turn is based on Composer. Installing Drupal any longer seemingly requires to first install Composer. Which isn't installed by default on Dreamhost VPS's, but it's not too hard to get it going.

Fortunately it's a lot simpler than it was the first time I tried this a couple years ago.

Because Dreamhost VPS's don't allow root access, you must install Composer and its dependencies in your home directory. The sticking point I had a couple years ago was to install the PHAR extension.

Installing PHAR

The first thing is to verify whether PHAR is installed:

$ php -m | grep Phar
Phar

If you get this output, then skip this next step.

SKIPPABLE STEP: The corresponding Dreamhost Wiki page says to create a PHPRC file, /home/USERNAME/.php/VERSION/phprc, and add these lines:

extension = phar.so 
suhosin.executor.include.whitelist = phar

On my VPS I found the PHPRC file to already exist and to already contain those lines.

Once you add those lines to your PHPRC, the above command should work. END SKIPPABLE STEP

Switching PHP version for command-line use

Another thing you may wish to do is to change the default PHP version used at the shell command line. What you do is enable this PATH variable change:

export PATH=/usr/local/VERSION/bin:$PATH 

On Dreamhost VPS's, they have multiple PHP versions installed as so:

$ ls -d /usr/local/php*
/usr/local/php53  /usr/local/php54  /usr/local/php55  /usr/local/php56	/usr/local/php70  /usr/local/php71

So, pick your poison. And after switching the PHP version, make sure the PHAR extension is enabled for that version.

Installing Composer

Now that the dependency has been squared away we can install Composer.

There are two modes to consider :- 1) an installation local to a given project, 2) a global installation.

For a local installation:

$ cd ~/<path-to-your-project>
$ curl -sS https://getcomposer.org/installer | php
$ mv composer.phar composer
$ ./composer

The last command should produce a long bit of USAGE output. What this does is download the Componser installer, and run it using PHP. That creates composer.phar in your current directory. For convenience you can rename that file to just composer, and because the PHAR extension is installed you can simply run the PHAR archive.

A global installation is extremely similar but that you'll add a directory to the PATH variable.

For example I keep ${HOME}/bin in my PATH variable on my VPS and install scripts there. The Dreamhost Wiki says to install it in ~/.php/composer, however. The process is the same:

$ cd ~/bin
$ curl -sS https://getcomposer.org/installer | php
$ mv composer.phar composer
$ ./composer

What will be different is to follow this up with adding a line to your ~/.bashrc:

export PATH=/home/<username>/.php/composer:$PATH

In this case it is for installation in ~/.bin/composer, and if you've done as written above you would use ${HOME}/bin in the PATH variable instead.

Once the PATH variable is set up correctly:

$ composer

Should work

Links

Source: (help.dreamhost.com) https://help.dreamhost.com/hc/en-us/articles/214899037-Installing-Composer-overview

Source: (help.dreamhost.com) https://help.dreamhost.com/hc/en-us/articles/214202148-How-do-I-change-the-PHP-version-my-shell-uses-

Source: (getcomposer.org) https://getcomposer.org/

About the Author(s)

(davidherron.com) David Herron : David Herron is a writer and software engineer focusing on the wise use of technology. He is especially interested in clean energy technologies like solar power, wind power, and electric cars. David worked for nearly 30 years in Silicon Valley on software ranging from electronic mail systems, to video streaming, to the Java programming language, and has published several books on Node.js programming and electric vehicles.