How to Install PHP Composer on Shared/Virtual Hosting: A Complete Guide
PHP Composer is the de facto standard dependency manager for PHP, enabling developers to declare, install, and manage the libraries their projects depend on. Whether you're building a Laravel application, a Symfony API, or a custom PHP project, Composer is an indispensable tool in your workflow.
This comprehensive guide walks you through every step of installing PHP Composer on a virtual or shared hosting environment — from SSH access to global configuration and real-world usage.
What Is PHP Composer and Why Do You Need It?
PHP Composer is a package manager that handles project-level dependency management. Rather than manually downloading libraries and managing autoloading, Composer resolves dependencies automatically, downloads the correct versions, and generates an optimized autoloader.
Key benefits include:
- Automated dependency resolution — Composer handles version conflicts and nested dependencies.
- Reproducible builds — The
composer.lockfile ensures every team member and deployment environment uses identical package versions. - Vast ecosystem — Access to over 350,000 packages on Packagist, the default Composer repository.
- PSR-4 autoloading — Eliminates manual
requireandincludestatements across your codebase.
If you're running PHP applications on Shared Web Hosting or a VPS Hosting environment, installing Composer is one of the first steps toward a professional, maintainable development workflow.
Prerequisites
Before you begin, confirm the following requirements are met:
| Requirement | Details |
|---|---|
| Hosting access | SSH access enabled on your hosting account |
| PHP version | PHP 7.2 or higher (PHP 8.x strongly recommended) |
| PHP extensions | openssl, phar, mbstring, json must be enabled |
| Permissions | Ability to write to your home directory or a directory in your $PATH |
| Terminal client | Terminal (macOS/Linux) or PuTTY / Windows Terminal (Windows) |
> Note: If you are on a managed shared hosting plan without SSH access, contact your hosting provider to enable it. AlexHost's Shared Web Hosting plans include SSH access, giving you the control you need.
Step 1: Connect to Your Server via SSH
Open your terminal application and establish an SSH connection to your hosting account:
ssh username@yourdomain.comReplace username with your actual SSH username and yourdomain.com with your domain name or server IP address.
Example:
ssh john@203.0.113.45Once authenticated, you will land in your home directory (typically /home/username/). You can confirm your current location with:
pwdStep 2: Verify PHP Is Available
Before downloading Composer, confirm that PHP is installed and accessible from the command line:
php -vYou should see output similar to:
PHP 8.2.10 (cli) (built: Sep 5 2023 08:15:22) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.10, Copyright (c) Zend TechnologiesIf PHP is not found, or the version is below 7.2, contact your hosting provider or upgrade your environment. Users on a VPS Hosting plan have full control to install or update PHP versions as needed.
Also verify that the required extensions are loaded:
php -m | grep -E 'openssl|phar|mbstring|json'All four extensions should appear in the output.
Step 3: Download the Composer Installer
Composer provides an official PHP-based installer script. Download it using the following command:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"This downloads the composer-setup.php installer script into your current working directory.
Step 4: Verify the Installer Integrity
This step is critical for security. Always verify the SHA-384 hash of the installer before executing it to ensure the file has not been tampered with or corrupted during download.
4a. Fetch the expected hash from the official Composer Public Keys page:
Visit https://composer.github.io/pubkeys.html to retrieve the latest installer hash.
4b. Run the verification command:
HASH="$(php -r "echo hash_file('sha384', 'composer-setup.php');")"Then compare it against the published hash:
php -r "if (hash_file('sha384', 'composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"If you see "Installer verified", proceed. If you see "Installer corrupt", stop immediately, delete the file, and re-download it.
Alternatively, you can use the one-liner from the official Composer documentation, replacing EXPECTED_HASH with the value from the public keys page:
php -r "if (hash_file('sha384', 'composer-setup.php') === 'EXPECTED_HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"Step 5: Run the Composer Installer
With the installer verified, execute it to install Composer:
php composer-setup.phpUpon successful execution, you will see output like:
All settings correct for using Composer
Downloading...
Composer (version 2.x.x) successfully installed to: /home/username/composer.phar
Use it: php composer.pharA composer.phar file is now created in your current directory. At this stage, you can already use Composer by calling php composer.phar, but making it globally accessible is far more convenient.
Step 6: Clean Up the Installer Script
Remove the setup script since it is no longer needed:
php -r "unlink('composer-setup.php');"Step 7: Install Composer Globally
To run Composer from anywhere on your system using just the composer command, move the composer.phar binary to a directory included in your system's $PATH.
On a VPS or Dedicated Server (with sudo access):
sudo mv composer.phar /usr/local/bin/composer
sudo chmod +x /usr/local/bin/composerOn Shared Hosting (without sudo access):
If you do not have sudo privileges, install Composer locally within your home directory:
mkdir -p ~/bin
mv composer.phar ~/bin/composer
chmod +x ~/bin/composerThen add ~/bin to your $PATH by appending the following line to your ~/.bashrc or ~/.bash_profile:
export PATH="$HOME/bin:$PATH"Reload the shell configuration:
source ~/.bashrc> Pro Tip: If you need full root access and greater control over your PHP environment, consider upgrading to a VPS Hosting plan or a Dedicated Server for unrestricted system-level configuration.
Step 8: Verify the Installation
Confirm Composer is installed and accessible globally:
composer --versionExpected output:
Composer version 2.x.x 2024-xx-xx xx:xx:xxFor a full diagnostic of your environment, run:
composer diagnoseThis command checks your PHP configuration, network connectivity, and Composer settings, reporting any potential issues.
Step 9: Using Composer to Manage PHP Dependencies
Now that Composer is installed, here is how to use it effectively in your PHP projects.
9a. Initialize a New Project
Navigate to your project directory and initialize a new Composer project:
cd /path/to/your/project
composer initThe interactive wizard will prompt you for:
- Package name (e.g.,
yourname/project) - Description
- Author information
- Minimum stability
- Required dependencies
This generates a composer.json file — the heart of your project's dependency configuration.
9b. Install a Package
To add a library to your project, use the require command:
composer require vendor/package-nameReal-world examples:
# Install Guzzle HTTP client
composer require guzzlehttp/guzzle
# Install Carbon date library
composer require nesbot/carbon
# Install Monolog logging library
composer require monolog/monologComposer will resolve dependencies, download the packages into the vendor/ directory, and update composer.json and composer.lock.
9c. Install All Dependencies from composer.json
When deploying a project or cloning a repository, install all declared dependencies with:
composer installFor production environments, use the --no-dev flag to skip development-only packages:
composer install --no-dev --optimize-autoloader9d. Update Dependencies
To update all packages to their latest allowed versions:
composer updateTo update a specific package:
composer update vendor/package-name9e. Autoloading
Composer automatically generates an autoloader. Include it at the top of your PHP entry point:
<?php
require 'vendor/autoload.php';This single line gives you access to all installed packages and any custom namespaces you define in composer.json.
Troubleshooting Common Issues
| Problem | Cause | Solution |
|---|---|---|
composer: command not found | Binary not in $PATH | Add the install directory to $PATH and reload shell |
PHP Fatal error: Allowed memory size exhausted | PHP memory limit too low | Run php -d memory_limit=-1 /usr/local/bin/composer install |
SSL certificate problem | Missing CA certificates | Install ca-certificates package or update PHP's openssl config |
The requested PHP extension ... is missing | Required extension disabled | Enable the extension in php.ini or contact your host |
Permission denied on /usr/local/bin | No sudo access | Install Composer locally in ~/bin as described in Step 7 |
Keeping Composer Up to Date
Composer includes a built-in self-update mechanism. Run the following command periodically to ensure you have the latest version:
composer self-updateTo roll back to the previous version if needed:
composer self-update --rollbackStaying current ensures you benefit from the latest security patches, bug fixes, and performance improvements.
Choosing the Right Hosting Environment for PHP Development
The hosting environment you choose significantly impacts your ability to use tools like Composer effectively.
- Shared Web Hosting — Suitable for smaller PHP projects. SSH access is available, but system-level changes are restricted. Composer can be installed locally in your home directory.
- VPS Hosting — Ideal for most PHP developers. Full root access allows global Composer installation, custom PHP configurations, and complete control over your stack.
- VPS with cPanel — Combines the power of a VPS with the convenience of a graphical control panel, making PHP and Composer management even more accessible.
- Dedicated Servers — Best for high-traffic PHP applications requiring maximum performance, isolation, and resource availability.
Pair your hosting with a proper SSL Certificate to secure your PHP applications and API endpoints in production.
Conclusion
Installing PHP Composer on a virtual or shared hosting environment is a straightforward process that dramatically improves your PHP development workflow. By following this guide, you have:
- ✅ Connected to your server via SSH
- ✅ Verified PHP availability and required extensions
- ✅ Downloaded and cryptographically verified the Composer installer
- ✅ Installed Composer globally or locally depending on your access level
- ✅ Learned how to initialize projects, install packages, and manage dependencies
Composer is not just a convenience — it is a professional standard. Keeping it updated and using it consistently across your projects ensures your PHP applications remain secure, maintainable, and scalable.
Whether you're running a small site on Shared Web Hosting or a complex application on a Dedicated Server, AlexHost provides the infrastructure and SSH-enabled environments you need to work with Composer effectively.
