I can't install composer globally in my web host, how can I use the "composer" command? - composer-php

I hosted my website on french host Gandi, with their simple hosting plan.
I cannot move anything in the /usr/local/bin directory since it is read-only, so I used to manage with composer.phar, which works well.
I recently used a library which requires the composer executable to be present (This library executes something like "composer require xxx" and there is no fallback to composer.phar).
Is there a way to make it work ?
What I have done so far :
Tried to install composer globally (Failed because of the read-only filesystem)
Tried to install composer globally for the current user (Failed because there was no ~/.local/bin directory, and also failed after creating the directory and restarting the instance)
Tried to move the file to any directory of the $PATH variable (Failed because all of these directories are read-only)
Tried to rename composer.phar to composer, and allowed it to be executable chmod +x composer (Failed because it only works with the command ./composer and not with composer)

Ok, I finally succeeded with the following steps:
renamed composer.phar to composer mv composer.phar composer
made this new file executable chmod +x composer
added the current path (corresponding to my website's root) to the system's $PATH variable export PATH=$PATH:$PWD
I'm not fully satisfied with this since this relies on a specific website's root folder, but hey, it works! I'll try to update this answer if I find a way to create a folder available for every website I'll host there.

Related

Why is my composer command not working in subdirectories on a 1and1 webhosting server?

I'm in the process of building a TYPO3 website on a webserver hosted by 1and1 ionos. I need to install TYPO3 via composer, so I installed composer to this webserver first. It seemed to work fine and I was able to do a composer reuqire to install TYPO3 to a new directory (named: typo3-composer) within the root of the server.
But for some reason I have two problems now:
Within the root directory of my server I cannot run the "composer xx" command. It only works using the prefix "php composer xx"
In my subdirectory typo3-composer (the one I installed TYPO3 into) I cannot run ANY composer command. Terminal simply says command composer not found.
What I did so far:
1. Install composer
curl -sS https://getcomposer.org/installer | /usr/bin/php8.0-cli
2.Start composer
/usr/bin/php8.0-cli composer.phar
3.Rename file
mv composer.phar composer
4.Tried to move the file to any directory (usr/local/bin) of the $PATHvariable to make composer globally accessible. This failed because all of those directories are read-only. So i made the file executable
chmod +x composer
5.And added the current path to the system's $PATH variable
export PATH=$PATH:$PWD
I'm pretty sure this is a very basic logial mistake. Where do I have to execute the composer install command and where do I have to put the composer file so that I can use the command from every directory?
(PS: I did set the PHP Version of the Shell to php 8.0 already so I don't have to use /usr/bin/php8.0-cli all the time)
Thanks for your well formulated question which I'd like to answer the following:
If you would like to (really) execute Composer on the remote host1, you already have the answer albeit you might not be aware of it. To quote from your very own question:
It only works using the prefix "php composer xx"
This is fine and how PHP works on the command-line. However as you have noticed it depends on the directory you're in (the working direcory). Not for the command (php) but for the file you command php(1) to execute: "composer".
Some more context if it helps: The first operand of the php command you entered, "composer", is the path to the file you want php(1) to execute.
So when:
user#host:~$ php composer info
...
works and you then change the directory and it doesn't any longer:
user#host:~$ cd typo3-composer
user#host:~/typo3-composer$ php composer info
Could not open input file: composer
user#host:~/typo3-composer$ echo $?
1
then replacing composer with the actual path of it should work:
user#host:~/typo3-composer$ php ../composer info
...
before actually running php(1), you can also make use of the file(1) command to check first if a file exists and of what type it is:
Not existing file:
user#host:~/typo3-composer$ file composer
composer: cannot open `composer' (No such file or directory)
Existing "composer" file (with executable bits set):
user#host:~/typo3-composer$ file ../composer
../composer: a /usr/bin/env php script executable (binary data)
Composer phar (even with no executable bits):
user#host:~/typo3-composer$ file ../composer
../composer: a /usr/bin/env php script executable (binary data)
Composer is just a PHP file (sort-of, it is a PHP archive "phar") and the idiom to execute it under the name composer is not really necessary. The path (absolute or relative to your currend working directory "PWD"2) suffices. If you take a look in the Composer documentation for example, you find the following idiom:
php composer.phar <options>|<operands>...
TLDR: Composer works regardless how you invoke it, be it composer, php composer.phar or similar. The php(1) command only needs a pathname of the PHP file to execute, be it composer.phar or one of the typo3 command-line scripts (to give a different example).
As user Nico Haase already has pointed out, there is no inherent requirement to execute composer(1) on the remote host.
Composer interacts with the wide-area-network (WAN, also: WWW, internet) to download files and applies the result to the working directory (project directory), more specifically the vendor-dir (vendor directory, "vendor" relative to the composer.json configuration file by default).
You normally then afterwards deploy that (local) project to the remote host. There is no requirement at all to do the dependency resolution on the remote host, quite the opposite: bring the work near to yourself so that you can much faster deal with any problems that may arise and make the deployment step dead simple (e.g. a single, simple transaction). Remote machines go offline, network connection may get lost in the middle and you may also need to (or want to) fully replace the remote host occasionally. All you should need to do is to re-deploy the project, not configuring remote systems and run commands there interactively. Life is too short for that.
Now the Typo3 project might have thought otherwise (which would not be very user-friendly if you allow me the comment, but I know the typo3 folks are user-friendly for real) then you may go through additional hurdles, however those are less related to Composer (and how you invoke it) but more to Typo3 (then, if at all). But, as you don't present Typo3 as an issue in your question, I'm pretty sure you can just do the "Composer work" locally in your own shell which has the benefit that you can configure Composer and how you invoke it to your liking without the requirement to do this only short and half at best on every remote host you may come in contact with.
PWD
This variable shall represent an absolute pathname of the current working directory. It shall not contain any components that are dot or dot-dot. The value is set by the cd utility, and by the sh utility during initialization. [from]

Use composer from outside project folder

I've googled this fairly hard, but not found this question. I also looked at the documentation and had no luck:
To run composer update, the command is usually run from inside the project.
So...
cd /var/www/html/project
composer update
This works as expected.
However, as part of an update script I would like to run this in a bash script. Composer is installed globally. I would like to be able to run composer from the location of the script with a target folder.. something like:
composer update /var/www/html/project
(which I have tested and does not work)
My specific question:
Is there a way of running composer on a target folder?
I know you have solved your problem doing a "cd" and a plain "composer update" but I think that it's not the correct answer.
In composer documentation page (https://getcomposer.org/doc/03-cli.md#global-options) you can find the option --working-dir (-d) that if specified, use the given directory as working directory.
For example:
composer install --working-dir=/var/www
From personal testing it is not possible to run composer against a target folder. If you're looking to run composer from a script the best best would be something like cd /var/www/html/project && composer update
This will work from within your bash script, I have personally done this on other projects.

Different filename for global composer.json

Just started working with composer and having some issues installing apps globally. I am using this on a linux machine running ElementaryOS. When I run composer global require "laravel/installer" it gives me an error that it cannot find ./composer.json file in the current directory. First why would I need something that I am installing globally?
So if I need one, could I just create in my home directory a .composer.json and when I do a global install can I just point to that file?
So I figured it out. Instead of putting the .composer.json in the home directory, I just put it in the .composer directory. Then I just named the file composer.json as it will not pollute my home dir anymore

what is phpmd and how to use it?

I have Ubuntu 14.04 + Sublime text 3 and installed phpcs packages
additionalty I have installed phpcs and php-cs-fixer on my system
From this blog
I have found that phpmd (PHP Mess Detector) is also a required library, so installed phpmd as per given instructions on official php md page using alternative method From the github repository everything was finished.
:~/phpmd$ curl -s http://getcomposer.org/installer | php
#!/usr/bin/env php
All settings correct for using Composer
Downloading...
Composer successfully installed to: /home/keshav/phpmd/composer.phar
Use it: php composer.phar
but now when I write on terminal
phpmd /opt/lampp/htdocs/myproject, myfile.php
phpmd: command not found
There is phpmd folder on Home directory and everything without any error.
I have local project on core PHP create composer.json in project folder as per suggested on github .
Please tell me what means by
Then install Composer in your project (or download the composer.phar directly):
I think the problem is you've installed phpmd in a local directory, but you're trying to use it as if it was installed globally.
Installation instruction on the referenced sites can't really be made any clearer. Since you've already installed phpcs and php-cs-fixer, and those work for you, just follow similar instructions for phpmd. These are all PHP projects and are installed in a similar way.
Anyway, to use phpmd as a global command you have several options.
Github
Clone the github repository just like you did and add the phpmd bin directory to your PATH variable.
Global composer installation
Use the composer global command to install phpmd globally. You will also need to make sure that composer's bin directory is in the PATH. By default it's ~/.composer/vendor/bin.
composer global require phpmd/phpmd
This command will install phpmd globally, and as soon as ~/.composer/vendor/bin is in your PATH you'll be able to call it by simply invoking phpmd.
It's very well explained in composer's documentation: https://getcomposer.org/doc/03-cli.md#global
Download the phar archive
This is the simplest thing you can do. Simply go the phpmd releases, choose the latest and download the phar archive.
Put the phar file to whatever place you'd like. Just remember that it needs to be in your PATH. You can also rename it, to skip the .phar extension.
For example:
wget http://static.phpmd.org/php/2.1.3/phpmd.phar
sudo mv phpmd.phar /usr/bin/phpmd
sudo chmod +x /usr/bin/phpmd
Docker container
First, fetch the docker image with static analysis tools for PHP:
docker pull jakzal/phpqa
One of the tools provided by the image is phpmd. The command below will run phpmd in a docker container and mount the current working directory as a /project.
docker run -it --rm -v $(pwd):/project -w /project jakzal/phpqa \
phpmd src text cleancode,codesize,controversial,design,naming,unusedcode
When you use the composer-based install, it gets installed into the ./bin directory within the ./vendors directory. So for me, relative to my project's root directory, it was here:
./vendor/bin/phpmd
And I was able to run it from my project's root by running ./vendor/bin/phpmd . text codesize. (I'm not getting any useful output yet, but another issue)

PHP Composer can't find composer.json file for my project

I have successfully installed Composer in the root directory (that was the default choice) on my Linux/Apache server using their installation guide. It's all been very simple so far, except for one php.ini tweak I've had to make (detect_unicode = Off) but now I'm stuck.
I'm trying to install Ratchet using Composer, with the use of this guide:
http://socketo.me/docs/install
It says I need to "create a file called composer.json in your project folder". So I created that file (with the contents they gave on their page) using the cPanel file manager, in my application's root directory. However, when I run Composer using:
php composer.phar install
PuTTy gives the following error message:
Composer could not find a composer.json file in /root
To initialize a project, please create a composer.json file as described in the http://getcomposer.org/ "Getting Started" section
But this doesn't seem to make sense, why would I place the JSON file in the server's root if the documentation says to place it in the project folder? What am I missing?
I just found on other topic that you can use :
php composer.phar --working-dir=/home/user/folder/ update
It looks like you're executing php composer.phar install in /root path.

Resources