How to Install Composer and Run PHP Scripts from the Command Line

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.json file (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.

  1. Check what the command line uses now:
    php -v
    which php
    On our servers, plain php over SSH is the PHP 8.3 command-line version.
  2. Check what your website uses in Software > MultiPHP Manager (the PHP Version column).
  3. 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):
    /opt/cpanel/ea-php83/root/usr/bin/php -v
    Replace 83 with the version you want, without the dot (82 for 8.2, 84 for 8.4). To see which are installed:
    ls /opt/cpanel/ | grep ea-php
    If your site's version is set in Software > Select PHP Version instead, CloudLinux's versions are usually found at paths like /opt/alt/php83/usr/bin/php. Run the path with -v to 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

  1. Go to your home directory, outside your website folder, so Composer itself can't be reached from the web:
    cd ~
  2. 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');"
  3. Paste them into Terminal. If you didn't set up the php alias, replace php in each line with the full path to the version you chose.
  4. You should see Installer verified, then Composer (version 2.x.x) successfully installed to: /home/username/composer.phar.
  5. Test it:
    composer --version
    (or php ~/composer.phar --version without 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:

CommandWhen to use it
composer install --no-dev --optimize-autoloaderSetting up a live site. Installs the exact versions recorded in composer.lock, skipping development-only tools.
composer installSetting up a development or staging copy.
composer require vendor/packageAdding a new library to your project.
composer update vendor/packageUpdating one library to the newest version your composer.json allows.
composer updateUpdating everything. Test on a staging copy first -- this can pull in major changes.
composer check-platform-reqsChecks whether this PHP version has every extension your project needs.
composer dump-autoload -oRebuilds 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:

CommandWhat it does
php script.phpRuns a script. Output appears in Terminal instead of a browser.
php script.php arg1 arg2Passes values to the script (read them from $argv).
php -l script.phpChecks a file for syntax errors without running it. Great before uploading an edit.
php -mLists the extensions loaded in this PHP version.
php -i | grep memory_limitShows a setting's value for command-line PHP.
php -d memory_limit=512M script.phpRuns one script with a different setting, without changing your site.
php artisan migrateRuns 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

Still stuck? Open a support ticket and the Instant Access Internet Services team will help.

Ultrafast LiteSpeed hosting from InstantAccess.net: free SSL, free backups, cPanel included, no contracts. Check out our $10/month hosting.
  • install composer cpanel, composer.phar shared hosting, composer wrong php version, run php script command line, php cli version cpanel, composer install no-dev, composer memory limit, laravel composer cpanel
  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

Connecting to SSH

SSH gives you a secure command-line connection to your hosting account, for tasks like running...

How to use SSH

SSH lets you work on your hosting account from a command line -- faster than clicking through...

How to Create an SSH Key on Windows, Mac and Linux and Add It to cPanel

An SSH key lets you log in to your hosting account's command line without typing your cPanel...

How to Fix "Permission Denied (publickey)" and Other SSH Connection Errors

SSH error messages are short and cryptic, but each one points to a specific cause. This guide...

How to Transfer Files Securely with SFTP and SCP

SFTP and SCP move files between your computer and your hosting account over an encrypted SSH...