How to Install / Update Composer on Shared Hosting Servers

In composer on 3~6 minutes
How to Install / Update Composer on Shared Hosting Servers

Usually, shared hosting servers do not let users to install or update the default system installed tools. For example, if you tried to update an outdated Composer installation with composer selfupdate command, most of the time you will get an error message something similar to the below.

[Composer\Downloader\FilesystemException]
Filesystem exception:
Composer update failed: "/opt/cpanel/composer/bin/composer" could not be written.
rename(/opt/cpanel/composer/bin/composer): Failed to open stream: Permission denied

This kind of errors happen because we do not have the permission to write or update system directories and files such as /opt/cpanel/composer/bin/composer in this case.

However, fortunately, we can override the default system installed Composer by manually installing it in somewhere else. For that, first, log in to your cPanel dashboard. If you have been using an old version of PHP, try to switch it to a latest available version. You can switch PHP versions by opening the “Select PHP Version” icon in the cPanel dashboard. After that, search for “Terminal” in the cPanel dashboard and open it. Usually, it should be under the “Advanced” section.

The Composer dependency management tool can be installed on the project level or globally for the user. However, if you are going to install it globally for the user, you must have the necessary permissions to edit the .bashrc file in the home directory.

Install Composer For Project Level

Go to your PHP Composer project directory with the cd command and run the following command to download the Composer setup file.

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"

Then run the following command, it will create the “composer” file that can be used to execute Composer commands.

php composer-setup.php --filename=composer

Finally, remove the composer-setup.php setup file with the following command.

php -r "unlink('composer-setup.php');"

Now you can execute Composer commands like the below.

php composer --version

The major drawback of this method is, that the updated Composer is only available in the project directory and it is not globally available. Therefore, you will have to repeat this method for each and every PHP Composer project and you should be inside the project directory. Or otherwise, every time you will have to type long paths to the “composer” file to execute Composer commands.

However, if you have “write” access to your home directory as described earlier, you can install it globally for the user.

Install Composer Globally

With this method, we are going to override the default system installed Composer file. First, go to your home directory with cd command.

cd ~/

Create a directory called bin with the mkdir command. Sometimes, that bin directory can be already existing so just ignore the warning message.

mkdir bin

Go to the bin directory.

cd bin

Now download and install the Composer as explained in the previous method.

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php composer-setup.php --filename=composer
php -r "unlink('composer-setup.php');"

Go to the home directory.

cd ~/

Open the .bashrc file. You can use nano or vi editor for that.

nano .bashrc

Add the following line at the bottom of the .bashrc file. It will override the default system installed Composer.

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

Press [Ctrl] + [x] then [y] and [Enter] to save and exit.

Run the following command to load changes immediately.

source .bashrc

Now you should be able to run Composer commands from anywhere.

composer --version

Visit https://getcomposer.org/download for more information.

Share: