When I run commands as superuser I get the warning not to run from the superuser account but I can successfully run them.

When I try to run the same commands via a non superuser account I get the following error:

phpenv: composer: command not found

I believe this is something to do with the fact the account is not superuser or does not have correct permissions.

How can I fix this please so that I can run from the other account rather than superuser.

I'm on Ubuntu 20.04.6 LTS and using PuTTY for SSH.

Comments

arup_cispl’s picture

This happens because Composer/phpenv aren’t in your non-root user’s PATH.

1. Install Composer system-wide

curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
sudo chmod +x /usr/local/bin/composer

2. Add phpenv to PATH

echo 'export PATH="$HOME/.phpenv/bin:$PATH"' >> ~/.bashrcecho 'eval "$(phpenv init -)"' >> ~/.bashrcsource ~/.bashrc

3. Test

phpenv versions
composer --versionwhich composer

 You can now run Composer and phpenv without sudo.


Why it happens: Root has a different PATH. phpenv installs shims in your home dir, but they aren’t available to your user by default. Composer also warns against running as root for security reasons.

Option A: Configure phpenv
Add this to ~/.bashrc or ~/.profile:

export PATH="$HOME/.phpenv/bin:$PATH"eval "$(phpenv init -)"

Reload and run phpenv rehash.

Option B: User-only Composer install (no sudo)

curl -sS https://getcomposer.org/installer | phpmkdir -p ~/.local/binmv composer.phar ~/.local/bin/composerchmod +x ~/.local/bin/composerecho 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrcsource ~/.bashrc

Troubleshooting

echo $PATH which composerwhich phpenvls -la ~/.phpenv/ls -la /usr/local/bin/composer

Once PATH is set correctly, you’ll be able to use Composer and phpenv as a regular user on Ubuntu 20.04 (PuTTY is just the SSH client, not the cause).

thismarkjohnson’s picture

The issue is that composer is installed only for the superuser and its path isn’t in your non-root user’s $PATH. To fix:

  1. Make sure Composer is installed globally for all users:

sudo apt update sudo apt install composer

or use the official installer for your user.

  1. Ensure your non-root user’s PATH includes Composer:

export PATH="$HOME/.config/composer/vendor/bin:$PATH"

Add that line to ~/.bashrc or ~/.profile to make it permanent.

After this, composer should work without sudo.