Use composer from outside project folder - bash

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.

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]

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

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.

Can't find /root/.composer/vendor/bin/laravel: No such file or directory

I have installed composer on Centos 7 virtualbox with PHP 5.6.27. I will show the commands that I have used and the issues that I am now having. I should first say that all of these commands have worked on previous installations.
Okay so for starters here is what I used to install composer.
curl -sS https://getcomposer.org/installer | php
Followed by
mv composer.phar /usr/local/bin/composer
Now I am trying to install laravel via the commands listed below.
composer global require "acacha/adminlte-laravel-installer=~3.0"
Up to this point all of the commands listed above have successfully worked.
Now here is the current problem. When I try to run the following command I get an error.
laravel new laravel-with-admin-lte
-bash: /root/.composer/vendor/bin/laravel: No such file or directory.
I have tried a whole set of remedies for this message but it cannot find the .composer directory. Can anyone tell me where the correct .composer directory is and how I can modify it so that it looks to the proper location to finalize this installation?
I appreciate any solutions that you can provide me. I have struggled with this for two days straight and have googled everything under the sun. Hopefully, someone on here has had a similar solution with a valid resolution.
Put an alias to the freshly installed Laravel installer in your user configuration .bashrc:
nano ~/.bashrc
And place this inside the file:
alias laravel='~/.config/composer/vendor/bin/laravel'
And run the following to make sure your bashrc profile is reloaded:
source ~/.bashrc
laravel new project
download as superuser
download installer
sudo composer global require "laravel/installer=~1.1"
setting up path
export PATH="~/.composer/vendor/bin:$PATH"
check laravel command
laravel
I had the same problem, so I changed the directory.
This is the directory where your bash was created: /root/.config/composer
enter image description here
but when excute the laravel command, the system search en the directory : bash: /root/.composer/vendor/bin/laravel:
enter image description here
So, just you need to change the address /root/.config/composer/vendor to /root/.composer/vendor.
PD. sorry I am learning english, so it is a little bad.

How to Require Package Into Laravel Project with Composer

I am new to Laravel and Composer, and am following some guides including LaraCasts.com . One of the things that need to be done is use Composer to require packages to use in the Laravel project. However, this doesn't work for me.
I installed Laravel using the composer create-project method (http://laravel.com/docs/5.0), and so far everything was fine. However, any package I "require" now actually goes into the C:\Users\user\vendor folder, and the composer.json file is in C:\Users\user.
I understand that Composer is supposed to separate between projects as opposed to being global like PEAR. So how do I specify that I'm working on a specific project and require everything into that project? Also, what is the actual path where I'm supposed to put packages in Laravel, in case I want to do it manually?
I am using Windows 7 and Xampp.
When you run composer require {pacakage}, make sure you use the cd command (Change Directory - I think) into the project you want to install the package for. So if you're in Xampp it could be something like cd C:\xampp\htdocs\my-laravel-project\ or whatever the path to your project is, then run your composer require {pacakage}.
I believe it's installing it in C:\Users\user\vendor as when you open your terminal/command prompt the default location is C:\Users\{username}\.
Please make sure you ran the composer create-project in your web root, for Xampp I think it's C:\xampp\htdocs\ but it could be different depending on how you set up Xampp when you installed it.

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)

Resources