Many PHP applications -- Laravel, Symfony, Drupal, and plenty of custom scripts -- use Composer to download the libraries they depend on. Composer isn't installed server-wide on Instant Access Internet Services hosting, but you can install your own copy in your home directory in a couple of minutes. This guide shows how to install Composer, choose the right PHP version for the command line, and run PHP scripts from the command line and cron.
What you need
- Shell access. Use Advanced > Terminal in cPanel, or SSH from your computer with your cPanel username. If you don't have either, open a ticket and ask.
- A PHP version your project supports. Current Composer needs PHP 7.2.5 or newer; most modern frameworks need PHP 8.x. Check your project's documentation or its
composer.jsonfile (the"php"line under"require").
Step 1: Choose the PHP version for the command line
This is the step people skip, and it causes most Composer problems. The PHP version your website uses and the PHP version the command line uses are set separately. Changing your site's version in Software > MultiPHP Manager or Select PHP Version does not necessarily change what php means in Terminal. If Composer runs on one version and your website on another, you can end up installing libraries your site can't run.
- Check what the command line uses now:
On our servers, plainphp -v which phpphpover SSH is the PHP 8.3 command-line version. - Check what your website uses in Software > MultiPHP Manager (the PHP Version column).
- If they match, you're done with this step. If not, find the full path to the PHP version you need. cPanel's PHP versions follow this pattern (the same paths used in our cron job guide):
Replace/opt/cpanel/ea-php83/root/usr/bin/php -v83with the version you want, without the dot (82for 8.2,84for 8.4). To see which are installed:
If your site's version is set in Software > Select PHP Version instead, CloudLinux's versions are usually found at paths likels /opt/cpanel/ | grep ea-php/opt/alt/php83/usr/bin/php. Run the path with-vto confirm it works before relying on it.
Make your choice the default in Terminal
Rather than typing the long path every time, add shortcuts (aliases) to your ~/.bashrc file. For example, for PHP 8.3:
echo "alias php='/opt/cpanel/ea-php83/root/usr/bin/php'" >> ~/.bashrc
echo "alias composer='/opt/cpanel/ea-php83/root/usr/bin/php ~/composer.phar'" >> ~/.bashrc
source ~/.bashrc
Now php -v should show 8.3 and, once installed, composer will run with it. Aliases apply to your interactive sessions only. Cron jobs don't read them, so always use full paths in cron (see below).
Step 2: Install Composer in your home directory
- Go to your home directory, outside your website folder, so Composer itself can't be reached from the web:
cd ~ - Open getcomposer.org/download in your browser and copy the four installer commands from the Command-line installation section. They look like this, but the long security hash in the second line changes with every installer release, so always copy the current version from that page rather than from here:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" php -r "if (hash_file('sha384', 'composer-setup.php') === '...current hash...') { echo 'Installer verified'.PHP_EOL; } else { echo 'Installer corrupt'.PHP_EOL; unlink('composer-setup.php'); exit(1); }" php composer-setup.php php -r "unlink('composer-setup.php');" - Paste them into Terminal. If you didn't set up the
phpalias, replacephpin each line with the full path to the version you chose. - You should see
Installer verified, thenComposer (version 2.x.x) successfully installed to: /home/username/composer.phar. - Test it:
(orcomposer --versionphp ~/composer.phar --versionwithout the alias).
If you see Installer corrupt, the download was damaged or the hash didn't match. Don't continue -- delete composer-setup.php and try again with freshly copied commands.
Step 3: Use Composer in your project
Change into the folder that contains your project's composer.json file, then run the command you need:
| Command | When to use it |
|---|---|
composer install --no-dev --optimize-autoloader | Setting up a live site. Installs the exact versions recorded in composer.lock, skipping development-only tools. |
composer install | Setting up a development or staging copy. |
composer require vendor/package | Adding a new library to your project. |
composer update vendor/package | Updating one library to the newest version your composer.json allows. |
composer update | Updating everything. Test on a staging copy first -- this can pull in major changes. |
composer check-platform-reqs | Checks whether this PHP version has every extension your project needs. |
composer dump-autoload -o | Rebuilds the class autoloader after adding your own classes. |
Composer creates a vendor folder with the libraries. Commit composer.json and composer.lock to your Git repository, not vendor.
A good habit: run composer update on your own computer or a staging copy, test it, then upload the new composer.lock and run composer install --no-dev on the live site. install is quicker and uses much less memory than update.
Keep application files out of public reach
Frameworks such as Laravel and Symfony expect only their public folder to be visible on the web; the vendor folder, the .env file with your passwords, and the application code should not be. On a hosting account, a tidy way to do this is to put the project outside public_html (for example /home/username/myapp) and point a subdomain's document root at myapp/public (see the subdomain document root guide below). If a .env file can be opened in a browser at https://example.com/.env, fix it straight away and change every password it contains.
Running PHP scripts from the command line
Composer is only one use for command-line PHP. You can run any PHP script directly:
| Command | What it does |
|---|---|
php script.php | Runs a script. Output appears in Terminal instead of a browser. |
php script.php arg1 arg2 | Passes values to the script (read them from $argv). |
php -l script.php | Checks a file for syntax errors without running it. Great before uploading an edit. |
php -m | Lists the extensions loaded in this PHP version. |
php -i | grep memory_limit | Shows a setting's value for command-line PHP. |
php -d memory_limit=512M script.php | Runs one script with a different setting, without changing your site. |
php artisan migrate | Runs a Laravel command (from the project folder). |
Command-line PHP isn't limited by the web server's timeout, which makes it better for long imports and batch jobs. Your account's resource limits still apply, so a script that uses too much memory or runs too many processes will be stopped.
Running scripts and Composer from cron
Cron jobs don't load your aliases, so use full paths for both PHP and the script:
/opt/cpanel/ea-php83/root/usr/bin/php /home/username/myapp/artisan schedule:run >/dev/null 2>&1
Add the job in Advanced > Cron Jobs. To keep a log instead of discarding output, replace >/dev/null 2>&1 with >> /home/username/cron.log 2>&1, and check that log file now and then so it doesn't grow forever.
Keeping Composer up to date
composer self-update
This updates your composer.phar in place. If you're stuck on a PHP version older than 7.2.5 for an old application, Composer's 2.2 long-term-support branch still runs there: php ~/composer.phar self-update --2.2. Better still, plan to move that application to a supported PHP version.
Troubleshooting
"Your requirements could not be resolved" / "requires php ^8.2 but your php version (8.1.x) does not satisfy that requirement"
Composer is running on the wrong PHP version. Go back to Step 1 and use the version your project needs.
"the requested PHP extension intl is missing from your system"
The command-line PHP doesn't have that extension loaded. Check with php -m. If your site uses Select PHP Version, see whether the extension can be ticked on its Extensions tab for that version. Otherwise, open a ticket with the extension name and the PHP version you're using.
"Allowed memory size of ... bytes exhausted" or the command just prints "Killed"
Composer's dependency solver can use a lot of memory, especially during update. Run install with an existing composer.lock instead, or update just one package. You can lift PHP's own limit for Composer with COMPOSER_MEMORY_LIMIT=-1 composer update vendor/package, but your account's overall memory limit still applies. "Killed" means that account limit was reached -- run the update on your own computer and upload the lock file.
"composer: command not found"
The alias isn't loaded. Run source ~/.bashrc, or use php ~/composer.phar directly.
The site shows errors after composer install
Check the PHP versions match (Step 1), clear any framework caches (for Laravel: php artisan optimize:clear), and look at the site's error log.
Common questions
Can you install Composer server-wide for me?
Composer isn't installed server-wide on standard plans, but a copy in your home directory works exactly the same way and you control its version.
Does Composer count against my disk space?
Yes. composer.phar is small, but vendor folders and Composer's download cache (in ~/.cache/composer) can grow. Run composer clear-cache to empty the cache.
Do I need Composer for WordPress?
Usually not. Plugins and themes include their own libraries. For command-line WordPress management, use WP-CLI, which is already installed.
Related guides
- How to Change Your PHP Version in cPanel
- How to Set Up a Cron Job in cPanel
- How to Use Git Version Control in cPanel
- How to Change a Subdomain's Document Root in cPanel
- PHP "Allowed Memory Size Exhausted": What It Means and How to Fix It
- How to Use SSH, SSH Keys and Terminal in cPanel
Still stuck? Open a support ticket and the Instant Access Internet Services team will help.
