How to Install Composer in cPanel Without Root Access

How to Install Composer in cPanel Without Root Access

Composer is one of the most important tools for managing PHP project dependencies. If you are working with Laravel, Symfony, CodeIgniter, custom PHP applications, or any modern PHP package, there is a good chance you will need Composer to install or update your project libraries.

On a private server, Composer is usually installed globally by the server administrator. However, if you are using a shared hosting account or you only have access to your own cPanel account, you may not have root access. The good news is that you can still install Composer locally inside your cPanel account and use it for your PHP projects.

What This Guide Covers

This guide is specifically for users who want to install Composer inside their own cPanel account. It does not require WHM access, root access, or server-wide configuration.

By the end of this guide, you will be able to:

Install Composer inside your cPanel home directory.

Run Composer from your own cPanel account.

Use Composer inside your website or Laravel project folder.

Optionally create a shortcut so you can run Composer more easily.

Requirements

Before you begin, make sure you have the following:

Access to your cPanel account.

Access to cPanel Terminal or SSH.

PHP enabled on your hosting account.

A project that requires Composer, such as a Laravel or PHP application.

If your hosting provider has disabled Terminal access, you may need to ask them to enable it for your account. Some providers allow Terminal access by default, while others only enable it on request.

Step 1: Open Terminal in cPanel

First, log in to your cPanel account.

Look for the Terminal option inside cPanel. It is usually found under the Advanced section.

Click Terminal to open the command-line interface for your hosting account.

If you are connecting through SSH instead, log in with your cPanel username and password or SSH key.

Once you are inside the terminal, you should be in your cPanel account environment, not the root server environment.

Step 2: Go to Your Home Directory

Composer should be installed inside your cPanel home directory so it can be used by your account.

Run this command:

cd ~

The ~ symbol represents your account’s home directory. For example, if your cPanel username is exampleuser, your home directory may be something like:

/home/exampleuser

Installing Composer here keeps it local to your account and avoids the need for root access.

Step 3: Download the Composer Installer

Next, download the Composer installer using PHP.

Run:

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

This command downloads the official Composer installer and saves it as:

composer-setup.php

If the command runs successfully, it may not show any output. That is normal.

To confirm that the file exists, you can run:

ls -la composer-setup.php

Step 4: Install Composer Locally

Now install Composer inside your home directory.

Run:

php composer-setup.php --install-dir=$HOME --filename=composer

This command installs Composer as a file named:

composer

inside your home directory.

After installation, Composer should be available at:

~/composer

This is a local Composer installation. It belongs only to your cPanel account and does not affect other users on the server.

Step 5: Remove the Installer File

After Composer has been installed, you no longer need the installer file.

Remove it with:

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

This keeps your account clean and avoids leaving unnecessary installation files behind.

Step 6: Test Composer

Now test Composer to confirm that it works.

Run:

php ~/composer --version

If Composer was installed correctly, you should see an output showing the Composer version, such as:

Composer version 2.x.x

The exact version may be different depending on the current Composer release.

Step 7: Use Composer in Your Project

Once Composer is installed, you can use it inside your PHP project.

For example, if your project is inside public_html, go into that directory:

cd ~/public_html

Then run:

php ~/composer install

This will install the PHP dependencies listed in your project’s composer.json file.

For production websites, especially Laravel or other PHP frameworks, it is usually better to run:

php ~/composer install --no-dev --optimize-autoloader

This command installs only the required production dependencies and optimizes the autoloader for better performance.

Using Composer with a Specific PHP Version

On many cPanel servers, especially servers using CloudLinux or EasyApache, different PHP versions may be available.

Sometimes the default php command may not point to the PHP version your project requires.

To check the current PHP version used by your terminal, run:

php -v

You can also check the PHP path:

which php

If your project requires a specific PHP version, such as PHP 8.2 or PHP 8.3, you can run Composer with that PHP version directly.

For PHP 8.2, use:

/opt/cpanel/ea-php82/root/usr/bin/php ~/composer --version

To install project dependencies with PHP 8.2, run:

cd ~/public_html
/opt/cpanel/ea-php82/root/usr/bin/php ~/composer install

For PHP 8.3, use:

/opt/cpanel/ea-php83/root/usr/bin/php ~/composer --version

To install dependencies with PHP 8.3, run:

cd ~/public_html
/opt/cpanel/ea-php83/root/usr/bin/php ~/composer install

This is useful when your application requires a newer PHP version but the terminal default is using an older one.

Optional: Create a Composer Shortcut

By default, you need to run Composer like this:

php ~/composer

If you want to run Composer more easily by typing only:

composer

you can create a shortcut inside your account.

First, create a bin directory inside your home folder:

mkdir -p ~/bin

Now create a Composer shortcut file:

cat > ~/bin/composer <<'EOF'
#!/bin/bash
php ~/composer "$@"
EOF

Make the file executable:

chmod +x ~/bin/composer

Now add your bin directory to your account’s PATH:

echo 'export PATH="$HOME/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc

Test it:

composer --version

If everything is correct, Composer should now run directly with the composer command.

Installing Composer Dependencies for Laravel in cPanel

If you are uploading a Laravel project to cPanel, you will usually need Composer to install the project dependencies.

Go to your Laravel project directory:

cd ~/public_html

Then run:

php ~/composer install --no-dev --optimize-autoloader

If your Laravel project is not directly inside public_html, go to the actual project directory instead.

For example:

cd ~/my-laravel-project
php ~/composer install --no-dev --optimize-autoloader

After that, you may also need to run Laravel-specific commands, depending on your project setup:

php artisan key:generate
php artisan config:cache
php artisan route:cache
php artisan view:cache

Only run these commands if your Laravel application requires them and your .env file has already been configured correctly.

Common Composer Commands

Here are some useful Composer commands you may need after installation.

To install dependencies:

php ~/composer install

To install dependencies for production:

php ~/composer install --no-dev --optimize-autoloader

To update dependencies:

php ~/composer update

To require a new package:

php ~/composer require vendor/package-name

To check the Composer version:

php ~/composer --version

To diagnose Composer issues:

php ~/composer diagnose

Common Issues and Fixes

1. Composer Command Not Found

If you run:

composer --version

and get an error like:

composer: command not found

it means Composer is not available globally in your account’s PATH.

Use this instead:

php ~/composer --version

Or create the Composer shortcut explained earlier in this guide.

2. Wrong PHP Version

If Composer complains that your PHP version is too old, check your PHP version:

php -v

If the version is not suitable for your project, use a specific cPanel PHP binary.

Example for PHP 8.2:

/opt/cpanel/ea-php82/root/usr/bin/php ~/composer install

Example for PHP 8.3:

/opt/cpanel/ea-php83/root/usr/bin/php ~/composer install

3. Memory Limit Error

Sometimes Composer may fail because of PHP memory limits.

You may see an error related to memory exhaustion.

You can try running Composer with no memory limit:

php -d memory_limit=-1 ~/composer install

For production installs, use:

php -d memory_limit=-1 ~/composer install --no-dev --optimize-autoloader

If the server has strict resource limits, you may still need to contact your hosting provider.

4. Permission Issues

If you see permission errors, make sure you are running Composer from your own cPanel account and not trying to write into a directory owned by another user.

You can check your current directory with:

pwd

You can check file ownership with:

ls -la

In most cases, files inside your own home directory should be writable by your cPanel user.

5. Missing PHP Extensions

Some packages require specific PHP extensions such as mbstring, openssl, curl, intl, fileinfo, or zip.

If Composer reports a missing extension, check your PHP extensions in cPanel.

In many cPanel environments, you can manage extensions from:

Select PHP Version

PHP Extensions

MultiPHP Manager

The exact option depends on how your hosting provider configured the server.

If you cannot enable the required extension yourself, contact your hosting provider and ask them to enable it for your account.

Should Composer Be Installed Globally or Locally?

If you are the server administrator with root access, Composer can be installed globally on the server.

However, for normal cPanel users, local installation is usually the best option. It does not require root access, it is safer for shared hosting environments, and it gives each account control over its own Composer usage.

A local Composer installation is enough for most PHP projects hosted inside cPanel.

Final Thoughts

Installing Composer inside a cPanel account is straightforward and does not require root access. You only need Terminal or SSH access, PHP, and a few commands.

The safest method is to install Composer in your home directory and run it using:

php ~/composer

For most production PHP projects, especially Laravel applications, you can install dependencies with:

php ~/composer install --no-dev --optimize-autoloader

If your project requires a specific PHP version, run Composer using the correct cPanel PHP binary, such as PHP 8.2 or PHP 8.3.

With Composer installed locally, you can manage your PHP project dependencies directly from your cPanel account without needing root access or server-wide installation.

Comments

No comments yet. Why don’t you start the discussion?

    Leave a Reply