Running composer in a different directory than current - php

I don't know if this question has been asked, because searching finds results mostly about moving the libraries installation directory.
I have a globally installed composer command. Is there a way to run, for example, composer install in a different directory than current, i.e. to specify the directory in which I would like tu run the command?
E.g. being in /home/someuser, I would like to acquire the same result as in running composer install it inside /home/someuser/myproject. Of course, one way would be to simply change the current directory, run composer and go back.

Try composer install -h. There you'll find an option --working-dir (or -d). And that's what you're looking for.
Then run:
composer install --working-dir=/home/someuser/myproject
You can find more in composer docs.
Depending on your operating system, the = might need to be removed:
composer install --working-dir /home/someuser/myproject

In addition to the above answer from Tomáš Votruba i had to append the = charachter on OSX. So the full command would be:
composer install -d=/home/someuser/myproject
My first post on SO so was unable to simply add this as a comment.

This works for me, PHP 7.3 on ubuntu 18.04
Install
composer install --working-dir=/your_composer_dir
Update
composer update --working-dir=/your_composer_dir

I tried what others said, but it was giving me: Invalid working directory specified 'PATH' does not exist. Although it was my working dir that contained composer.json!
I don't know why anyway, but this worked for me (only for gnu/linux users):
composer --working-dir=$(pwd)
And by the way, if you had run composer -h, it would've told you the solution:
-d, --working-dir=WORKING-DIR If specified, use the given directory as working directory.

I am using a Windows machine with PHPStorm (terminal) and this worked for me.
composer install --working-dir /home/someuser/myproject
My Linux OS machines require me to use
composer install --working-dir=/home/someuser/myproject
Note: You may be able to substitute ~/ for /home/someuser/ if your path is super long.

Run:
cd /home/mysites/google.com
Then run:
composer require facebook/graph-sdk
Above steps will open up the directory named (google.com) and install facebook Graph SDK there.

Related

composer - vendor folder empty

I got a website which I need to maintain and after looking at the files and code, I thought there are some missing files in project/vendor folder.
After talking to the current maintainer, he told me I need to use composer in order to see those files. I have installed composer but I don't know how to "fill" the folder with the files.
From reading online I understood I need to extract and install dependencies using the composer.json file but even after searching the web for more then an hour I didn't find how to do it.
Go to the root of you project and run
composer install
after that composer will download all package that are in the composer.json file in the require and require-dev section
First, install the composer, take a look here composer, after this try to run composer install, in some cases I do update with composer update too.
Remember to run the command composer install on the same path where composer.json
Apparently I had to install php7.0-curl using the sudo apt-get install php7.0-curl.
After that I just used composer install again and it's good now

Tell Composer to use Different PHP Version

I've been stuck at this for a few days. I'm using 1and1 hosting, and they have their PHP set up a bit weird.
If I use just php composer.phar install, then I'm using PHP 4.4.6, which is horribly insufficient. However, I can run php5.5 composer.phar install, get a little bit further, but it still fails because somewhere along the line, PHP is called again, but it fails, as it's using 4.4.6.
Is there any way to tell Composer to use the php5.5 command? Has anyone successfully gotten Composer configured on 1and1 hosting?
I'm trying to get Laravel up and running (which uses Composer). I've been able to do it on my GoDaddy domain, but not 1and1.
Ubuntu 18.04 case ... this run for me.
/usr/bin/php7.1 /usr/local/bin/composer update
Maybe this can't solve exactly your issue but probably it will help others who comes here from web search.
Just run the command to add below code to your composer.json file to set different PHP version:
$ composer config platform.php 8.0.7
"config": {
"platform": {
"php": "8.0.7"
}
}
On xubuntu I had php 7.2 as default. But needed to run composer with php 5.6.
So this worked for me:
php5.6 /usr/bin/composer
You always can use that way.
In project folder where you has composer.json file. Run the command
php7.4 /usr/local/bin/composer install
or
php7.4 /usr/local/bin/composer update
where php7.4 your specific version can be(php7.0, php5.5, php7.3 etc...)
where /usr/local/bin/composer path for system folder with composer
!!! you should have php7.4-cli
sudo apt-get install -y php7.4-cli
That way for linux/ubuntu user
Cheers ;-)
If you just need to get composer to ignore the version requirements, you can bypass using:
composer install --ignore-platform-reqs
composer is a PHP binary script and it's using Unix Shebang to define which interpreter for executing.
root#home:~# head /usr/local/bin/composer -n 5
#!/usr/bin/env php
<?php
/*
* This file is part of Composer.
*
/usr/bin/php7.1 /usr/local/bin/composer update
So if you're running composer without any specific option, it will use your PHP version at /usr/bin/env php (which is your default PHP)
We can have many versions of PHP inside your Linux and you can flexible use which PHP version for executing as you want like this way:
root#home:~# /usr/bin/php7.1 /usr/local/bin/composer update
Or just
root#home:~# php7.1 /usr/local/bin/composer
root#home:~# php7.2 /usr/local/bin/composer
root#home:~# php7.3 composer
...
To check & find all installed PHP packages, goto /usr/bin/
root#home:~# ls /usr/bin/ | grep php
php
php5.6
php7.1
php7.2
php7.3
php-config
php-config7.1
phpdbg
phpdbg5.6
phpize
phpize7.1
I too have a shared hosting account on 1&1 (ionos) and here's what I have had to do:
if you login as the root ssh account, you can create a ~/.bash_profile and add
alias php="php7.1"
alias composer="php7.1 composer.phar"
to it so that you can now use the commands you would normally use and it just works. (put composer.phar in your project folder root)
Then, make sure your laravel composer.lock file from your dev machine gets up to your project folder on 1and1 and run
composer install
Using this tip from #tobymackenzie: on shared hosts you really should just run composer install (not update!) from a composer.lock file you created on your own machine. That way the memory usage remains very low.
I still had STDIN issues with artisan commands so make sure you change the .env file to
APP_ENV=local
because having it set to production throws infinite STDIN warnings as it waits for you to type yes. At least on my account it does.
Hope this helps somebody.
In my case, Composer detected PHP version 7.2.6 but my actual PHP Version was 8.0.2 .So , I did the following steps and It was the solution for me.
Step 01:- Add below code to your composer.json file
"config": {
"platform": {
"php": "7.2.6"
}
}
Step 02:- Then, run the below command.
C:\xampp\htdocs\your project name>composer update
Try this approach
ea-php72 /opt/cpanel/composer/bin/composer
The existing answers partly do not answer the question, give suggestions that do not work or give bad advice.
The question stated:
However, I can run php5.5 composer.phar install, get a little bit further, but it still fails because somewhere along the line, PHP is called again, but it fails, as it's using 4.4.6.
Instead, I am here showing a way to test and verify the suggestions.
I wish I had a better answer myself but I am unsure what is the best solution. Personally I would go with a solution which make sure PHP is called with the correct version on the host (e.g. via an alias or setting the PHP environment correctly, running the entire operation inside a container or some other solution), regardless of Composer and add a platform php constraint.
If you don't run any scripts and just need the requirements to resolve the dependencies correctly config:platform:php would probably be your friend.
Let's say our default php version is 7.4 but our project requires 7.2.
Check your default PHP version
command line:
# shows version of default PHP
php --version
# show path of default PHP
which php
# show version of /usr/bin/env php
# /usr/bin/env php --version
Create a test script in your composer.json
"scripts": {
"php-version": "php --version"
}
Now you can test what version of PHP is used for the commands Composer
calls, e.g.
composer php-version
/usr/bin/php7.2 /usr/bin/composer php-version
In my case, both of these use my default php script (which is version
7.4). So, while the 7.2 version is used to run Composer, the scripts
called by Composer use the default command (unless you use #php instead of php1).
Now let's add the platform setting as suggested in some of the answers
"config": {
"platform": {
"php": "7.2.0"
}
}
Does not change anything for the script execution:
/usr/bin/php7.2 /usr/bin/composer php-version
# result is still 7.4
Actually, what the platform option is important for is resolving the requirements, so you should add it (in your project composer.json). It just won't solve the above problem of composer using PHP with the correct version. See also Composer docs for platform.
If you use #php instead of php, then Composer uses the PHP_BINARY2 path which is the php binary path that Composer tried to infer being executed with. See Executing PHP scripts. In this example commonly $(command -v php7.4) different to $(command -v php7.2), here $(command -v php).
PHP_BINARY is an environment variable Composer sets for its own environment (ref), similar to the predefined PHP_BINARY constant in PHP.
I had the same issue and this is how I was able to figure out how to run composer using a different PHP version on a shared hosting without breaking the system :)
Luckily, solving this issue with composer using different PHP version is dead simple.
Findings:
Default PHP was not run from /usr/local/bin or similar folder. Using the
which php
or
whereis php
would not give you the best answer.
Follow the short guide below:
First find the actual path to your composer binary itself.
On my shared hosting server, it was at
/opt/cpanel/composer/bin/composer
I believe most hosting providers using cPanel would have such path to composer.
Next, create an alias to the PHP version you want to use.
In my project, I needed php 7.4 or newer.
As mentioned earlier, the host was using cPanel and the actual default PHP version that the server uses is in the path /opt/cpanel/
You should see different versions of php in the /opt/cpanel/ (prefixed by ea-phpxx) for example, for php 7.4, it would be a folder called ea-php74 and for PHP 8.0, it would be a folder called ea-php80.
Now, be sure that php exists in a path similar to this:
/opt/cpanel/ea-php80/root/usr/bin
If you find php in the folder, then you can now alias it as folows:
alias php='/opt/cpanel/ea-php80/root/usr/bin/php'
Test that the alias is working by running
php --version
and check the php version as shown in the screenshot below:
Now, run composer as normal. For example
php /opt/cpanel/composer/bin/composer update
php /opt/cpanel/composer/bin/composer install
php /opt/cpanel/composer/bin/composer install
Remember how we found the composer binary at /opt/cpanel/composer/bin/composer. If this does not match the path to the compser binary on your server, use the appropriate one.
I hope this helps.
Happy Coding!
You could change your PATH to override the php version.
PATH="/home/user/bin:$PATH"
In /home/user/bin (or any other directory really) make a symlink named php to the php5 binary.
Another option, use a shell alias:
alias php="/path/to/php5"
I'm on a Hostgator shared account, the following worked for me:
First, find out where your desired version of PHP lives (you can start typing ea-php and hit TAB to list all available executables starting with ea-php):
which ea-php73
/usr/local/bin/ea-php73
Then, find out where composer lives:
which composer
/opt/cpanel/composer/bin/composer
Then use those to run your composer command:
/usr/local/bin/ea-php73 /opt/cpanel/composer/bin/composer install
For people using Plesk, you need the different pieces:
Get the PHP version required from /opt/plesk/php/<version/bin/php, eg /opt/plesk/php/7.4/bin/php
Get the real path of composer since /usr/local/bin/composer is a custom Plesk script by vieweing the script contents ( cat /usr/local/bin/composer ). In my case, it was /usr/local/psa/var/modules/composer/composer.phar
Put it all together:
# /opt/plesk/php/7.4/bin/php /usr/local/psa/var/modules/composer/composer.phar install
... or to make it simpler, just create a new file ( I like composer74 ) in /usr/local/bin:
cat > /usr/local/bin/composer74 to create the file
paste the following:
#!/bin/bash
/opt/plesk/php/7.4/bin/php /usr/local/psa/var/modules/composer/composer.phar
insto the terminal
Hit CTRL+D to save the file
chmod +x /usr/local/bin/composer74 to make it executable
Use it anywhere:
# composer74 install
Know this question is a bit old... but if you pull down composer into your app root:
https://getcomposer.org/download/
Instead of relying on global composer, then you can run:
php56 composer.phar {your command}
or I believe newer homstead versions would be like:
php7.1 composer.phar {your command}
https://laracasts.com/discuss/channels/laravel/run-composer-commands-with-different-php-versions
Ubuntu 18.04 case ... this works for me. Here, Composer picks the required PHP version automatically.
/opt/lampp/bin/php /usr/bin/composer install
OR
/opt/lampp/bin/php /usr/bin/composer update
php5.5 composer.phar install fails because your composer.json is telling it to run php and not php5.5, edit the file composer.json replace the php scripts references to php5.5. Example:
From:
"post-root-package-install": [
"php -r \"copy('.env.example', '.env');\""
],
To:
"post-root-package-install": [
"php5.5 -r \"copy('.env.example', '.env');\""
],
Even when doing this, 1and1 has a memory limit for scripts execution so php5.5 composer.phar install won't fail but it won't complete its execution either. You can still run the scripts post install manually.
I have deployed Laravel webapps to 1and1 without commit vendor directory, following this guide: Deploy Laravel Webapp to 1and1
I had no luck with any of above answers, I had auto scripts in composer.json so just ignoring or faking platforms just caused failed scripts etc. Setting php version in command just didn't work!
I did notice however that although running which php and php -v returned correct version which /usr/bin/env php returned the problematic version of php that composer was using. So found a very good answer here and outlined below:
Issue:
/usr/bin/env php looks for an executable named php in the current $PATH; it pays no attention to aliases, shell functions, etc. If you want it to execute php v5.3, you have to put that version in a directory under the name php, and put that directory in your PATH somewhere before the directory that contains the problematic version.
Fix:
Create new php executable folder like below with your specific php executable linked inside:
mkdir /usr/local/bin/php-overrides
ln -s /usr/local/bin/php7 /usr/local/bin/php-overrides/php
Add below to your ~/.bash_profile (or other appropriate startup script):
PATH="/usr/local/bin/php-overrides:$PATH"
https://serverfault.com/questions/579956/alias-doesnt-work-in-script-with-usr-bin-env-php
There are two ways to do it.
Tell the composer to ignore the platform requirements while running the command in the terminal.
composer update --ignore-platform-reqs
Tell the composer to ignore the platform requirements in the composer.json file in the project root directory.
"config": {
"platform-check": false,
},
We can tell Composer, what version of PHP we are supporting with our app by using the platform configuration in our composer.json file by adding the following configuration,
{
"config": {
"platform": {
"php": "5.5"
}
},
"require": {
...
}
}
Or from the command-line
composer config platform.php 5.5
i have a method to slove this problem.
export PATH=/usr/local/Cellar/php/8.0.12/bin:$PATH && composer -vvv
Temporarily change environment variables and use composer
With PHP 8 and 8.1 coming out, I have to work with a couple of versions regularly in the CLI, and the best way I found to do this was to create an alias.
Assuming you installed using brew, or already know where your version of PHP is.
Add the following to your bash profile.
alias php7='/usr/local/opt/php#7.4/bin/php'
alias php8='/usr/local/opt/php#8.0/bin/php'
If you want you can install PHP 8.1 as well with brew install php#8.1 and add another alias.
This will allow you to run php8 composer, you can do a quick test by creating an index.php file and adding phpinfo() to it.
php7 index.php | grep "PHP Version"
php8 index.php | grep "PHP Version"
While trying to install a Laravel application on my SiteGround shared hosting using SSH, I had to specify the adequate PHP bin constant prior to executing composer :
PHP_BIN=php81 composer install
Other solutions here would not work.
Must add in two places in your composer.json file to set different php version:
"config": {
"platform": {
"php": "7.4"
}
}
"require": {
"php": "^7.4",
}
You could try and copy the php and php-config files into /usr/local/bin instead. That path has a higher order of precedence and should override the /usr/bin folder without needing to disable SIP. That is what I did.
On AlmaLinux with php74 my default PHP version, php81 installed to /usr/bin/php81 and composer in location /usr/bin/composer, the following works one time: running composer using php81.
/usr/bin/php81 /usr/bin/composer install
Composer flags can be added as following:
/usr/bin/php81 /usr/bin/composer install --no-dev --no-interaction --ignore-platform-reqs
Notice these commands need to be executed from you working folder.

Is there a place to get the laravel `vendor` folder in one spot?

I realize that this is what composer is used for, but I don't particularly like it. It makes sense, but it annoys me that laravel5's github doesn't work out of the box because it's vendor directory is somewhat large and isn't necessarily laravel5, although laravel does require it.
It also puzzles me why composer doesn't get it itself.
I would expect to be able to
git clone the_laravel5_github_url
composer install
and be able to run as it seemed like you were able to do at one time, but for some reason now, the vendor directory isn't there. I feel like this makes laravel harder to set up as it isn't so obvious.
Is there a place where someone can get an recent version of this? I find it disappointing that there is no mention of it on their readme and that you were able to do it before and still can't despite using composer install.
Install Composer on your OS using this command
curl -sS https://getcomposer.org/installer | php.
Move the composer.phar file to /usr/local/bin/ with this command
mv composer.phar /usr/local/bin/composer. This will enable you to access composer globally.
git clone your project.
Make a vendors folder in the root of your project.
cd to root of your project and run composer update. This command will look for vendors folder in the root and will install all the packages required by your project in it.
Happy Coding
Thank you :)
Use composer install in the same folder of your app. Try composer update. And install composer globally in your OS.

Can't run phpunit tests from command line

I am trying to run unit tests in a new laravel 5 application, using the phpunit framework. In the root path of my laravel application I ru the following command:
./vendor/bin/phpunit /tests/ExampleTest.php
And then I get the following message:
You need to set up the project dependencies using the following commands:
wget http://getcomposer.org/composer,phar
php composer.phar install
I already have composer installed in may system and I install Laravel 5 using composer. Isn't phpunit installed when I install a new laravel 5 application? If not, how can I install it in a existent laravel 5 application?
I known that I can also install phpunit globaly and resolve the problem. But maybe it will be a duplication since I have all the phpunit code already in may laravel application.
I had the same problem and it had a specific solution that may apply to other people. I store my files in Dropbox, and the vendor/bin/phpunit file should be an symlink like this
$ ls -lo vendor/bin/phpunit
lrwxr-xr-x vendor/bin/phpunit -> ../phpunit/phpunit/phpunit
However, Dropbox will occasionally replace symlinks with the original file, and this will cause the error above. The behaviour is inconsistent, and with relative symlinks seems to break when 2 machines are accessing Dropbox at the same time. Fixing the symlink worked for me or you could make a new symlink directly to the vendor/phpunit/phpunit/phpunit outside of Dropbox and run that.
Edit: Nowadays I exclude Vendor and node_modules from Dropbox - and simply run composer install when necessary. This works really well, and also deals with the hassle of syncing so many files on Dropbox. What you can do is go into the folder and delete all the files. Wait for Dropbox to sync. Then mark the folder as excluded. Finally, run composer install and you get the contents as you need. (Delete + composer install often solves other issues too).
Running composer install did nothing in my case. However, removing vendor folder and then calling composer install fixed it.
You need to have Composer installed and run composer install or composer update in your application to install the different packages listed in your composer.json.
When you install your Laravel application it doesn't install the packages right away.
You can verify the packages are installed by looking in the vendor directory of your application and check that phpunit is in there.
did you install phpunit globally? I recommend you do it.
just type in your laravel's root directory (e.g. /var/www)
cd /var/www
phpunit
if you what to test just one file, you can do something like this:
phpunit tests/ExampleTest.php
Unit Test:
D:\xampp\htdocs\Samplemed\vendor\bin>
phpunit ../../tests/Unit/Company/CompanyUnitTest
Feature Test:
D:\xampp\htdocs\Samplemed\vendor\bin>phpunit
../../tests/Feature/Company/CompanyFeatureTest
Please try this. its working fine.

Using Composer PHP on Shared Space

I'm using Bluehost and do have access to SSH, thankfully. I've also set my PHP to 5.4 in the Control Panel settings. Now, here's the two big questions that I can't seem to grasp: how do I install Composer and furthermore, after installing Composer, how do I get the dependencies included?
In this case, I am attempting to use the official Tumblr PHP Library, which has dependencies -- all of which can be found on Packagist.
I've referred to this question in which the OP is using HostGator. I have attempted to install Composer in a similar fashion and have done so with seemingly successful results. The issue, however, is that I don't know where to install it on Bluehost so I now have Composer installed in several random places on the server simply because I don't know how to navigate to find where to put it in this shared space.
I know this is the issue (i.e. it's installed in the wrong place) because when I use the composer phar files and syntax to install the packages, I get errors saying the commands don't exist.
I really hate asking for hand-holding assistance, but if someone could walk me through the proper installation of Composer on a shared space and the proper installation of a Composer package from Packagist on the same shared space, it would be much appreciated. I've dug through the Composer documentation and can't seem to find a proper guide -- if one can even exist -- for this case. At this point, I'm wondering if it's going to be different for every web host.
Once you install composer as mentioned by KLVTZ. In the bluehost environment, use the php-cli instead of php.
php-cli composer.phar install
When installing composer, the biggest problem I ran into was that Bluehost's command line php was 5.2 and composer needs 5.3+.
Fortunately, Bluehost does give you access to 5.4. You can find it at
/ramdisk/php/54/bin/php54-cli
I ended up setting up an alias to call composer.phar using the above php command and it's worked great. You can see more details on how to install composer on bluehost.
You should probably not use Composer on the remote host, but instead install it on the local machine that is used to upload your project to Bluehost. There you can manage all the stuff that is needed to fetch the dependencies, which might include having the ZIP extension enabled or having a ZIP program available, having GIT or Mercurial installed if you have to grab a version from a branch, and so on.
All these dependencies of using Composer might not be available on that remote host, and frankly: You really do not want to install these development tools on a production machine anyway, I think. Any software that potentially helps an attacker shouldn't be present if it can be avoided.
The issue, however, is that I don't know where to install it on Bluehost...
In order to fix this problem, you need to figure out where exactly your project folder is. Composer needs to be installed in that folder. If you have access to a UNIX shell, I recommend that you change your directory to that project folder
cd $_SERVER['DOCUMENT_ROOT']/project_folder/
Generally, your remote server will follow the same file directory tree as that on your local machine. However, if you are unsure as to how your file structure may be organized, you can always print working directory of your local machine:
pwd
After you have successfully located the correct file path on your shared space, simply install composer:
curl -sS https://getcomposer.org/installer | php
This will report back something like:
All settings correct for using Composer
Downloading...
Composer successfully installed to: /var/www/my_project/composer.phar
Use it: php composer.phar
You now have access to your composer.phar in your project. Let me know if you need any further help.
One problem on Bluehost is that the php command on Bluehost does not execute php-cli. So I "bypass" that by having my own php script earlier in the PATH and then forwarding the arguments to php-cli. In the same process I made script named composer which calls php-cli composer.phar directly.
So, this setup has worked for me on Bluehost:
In ~/bin I have the composer.phar and two bash scripts called php and composer. In ~/.bash_profile I have prepended ~/bin to my PATH.
~/bin/php looks as follows:
#!/bin/bash
php-cli "$#"
~/bin/composerlooks as follows:
#!/bin/bash
php-cli ~/bin/composer.phar "$#"
And the additions to ~/.bash_profile are these:
PATH=$HOME/bin:$PATH
PATH=$PATH:$HOME/.composer/vendor/bin
export PATH
(I have also added ~/.composer/vendor/bin as can be seen, this is handy for global access for some composed applications (for me that is laravel)).
Remember to source ~/.bash_profile. (Or reconnect)
I think you should do something like this.
In console
echo $PATH
Use one of these pathes
mv composer.phar {selected path}/composer
chmod +x {selected path}/composer
Now you can simply call composer everywhere
If you have composer installed in your root directory (under public_html), might be something related to PHP version. As Bluehost when chosing PHP version "Be aware that this only modifies your ~/public_html/php.ini file. If you choose to use PHP 5.4, you'll want to make sure your crons use "/usr/php/54/usr/bin/php" instead, as otherwise it will use PHP 5.2."
So you can try with the command
/usr/php/54/usr/bin/php-cli composer.phar install

Categories