How to remove globally a package from Composer? - php

I ran this command to install globally PHPUnit:
composer global require 'phpunit/phpunit=3.7.*'
Now I want to uninstall globally PHPUnit.
Any ideas?

To remove a globally installed package run:
composer global remove phpunit/phpunit
global command lets you to run many commands like install, require or update as if you were running them from the COMPOSER_HOME directory.
Read the related documentation here: http://getcomposer.org/doc/03-cli.md#global
COMPOSER_HOME depends on your system (on Linux it's ~/.composer), see http://getcomposer.org/doc/03-cli.md#composer-home for more details.

Also you can use another way
cd $HOME/.config/composer
And in composer.json file remove some require positions
After all execute composer update
This is a long way, but more clear

Related

Running commands after Composer package installed globally

I have created a Composer package which is designed to be installed globally, it's basically a package to spin up Docker environments for projects that I use a lot for local development. I'd like to share my package but would like to make it more user friendly.
Currently when the package is installed (with composer global require me/my-package-name), the user needs to take some extra steps:
cd into the install location and run composer install (inside the root of my package in their vendor dir)
On MacOS, users also need to run cd docker && chmod +x phpenv
Add above docker folder to their PATH
I'm trying to make this less of a pain, so I attempted to add a post package install command - composer.json:
"scripts": {
"post-package-install": [
"DannyXCII\\Environment\\Install\\Installer::postPackageInstall"
]
},
public static function postPackageInstall(): void
{
$path = dirname(__DIR__, 3);
exec("cd $path && composer install && composer update && composer dump-autoload && cd docker && chmod +x phpenv");
}
After trying and failing to get this to work when installing this globally I realise now that this script won't get called when this package is installed, only when packages are required and composer install is ran inside this package, as:
Note: Only scripts defined in the root package's composer.json are executed. If a dependency of the root package specifies its own scripts, Composer does not execute those additional scripts.
So, I believe in this case, the solution would be to add this script to the root project - so I'd need to update this script and add to the composer.json file in C:\Users\Danny\AppData\Roaming\Composer(?), as would anyone else wanting to use this - so this doesn't solve my issues regarding usability.
I have been searching and attempting to resolve this for a while but found this quite a difficult topic to search on.
Essentially, my question is: how can I run commands such as composer install inside my package after it is globally required without the user having to do so manually?
Ok so I've now solved this - so, you know how you can run composer global require laravel/installer and immediately type laravel and you'll see some help text from the command? This is the same level of convenience I wanted to achieve with my package so I decided where better to look to see how it's done?
I was thinking about this incorrectly; I don't want a vendor directory inside my globally installed package and I shouldn't ever expect commands like composer install inside my package.
I was able to remove the two blocks from above. Inside my phpenv script, where I'm looking for the autoloader from within my package, I also added an additional check, so that this will use the autoloader at C:/Users/Danny/AppData/Roaming/Composer/vendor/autoload.php if it exists - this removes the need to run composer install (one step down):
if (file_exists(dirname(__DIR__, 3) . '/autoload.php')) {
require dirname(__DIR__, 3) . '/autoload.php';
} else {
require dirname(__DIR__, 1) . '/vendor/autoload.php';
}
Assuming you have your Composer bin directory added to your path (something like C:/Users/Danny/AppData/Roaming/Composer/bin), I was able to remove the need to add the path to this package specifically by making sure that the script to execute my commands was mapped to the above bin directory. I moved my phpenv script to a bin directory in my package and then added the following to my composer.json - this removes the need to add the path to the script directory to my environment variables path:
"bin": [
"bin/phpenv"
],

Finding out which composer packages are installed globally

I am working on a project where I need to determine which composer packages are installed globally and what version. What is the best way to do that on Mac?
I was hoping there was some kind of terminal command for that and I can't seem to find it.
This is what I tried:
php /usr/local/bin/composer.phar show
Tried this
php global composer.phar show
The order of the parameters is wrong. global is not a valid parameter for the PHP interpreter, but for the composer application.
To see the packages installed globally on your system run:
php composer.phar global show
Or, if composer itself is installed globally:
composer global show

Running composer in a different directory than current

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.

Composer - Is it possible to install packages in "require-dev" globally with one command?

quick question, and please let me know if anything about my idea is silly in general / if there is a better approach to this:
For the purpose of creating a Continuous Delivery pipeline, I would like to declare some packages needed for various types of tests in Composer with "require-dev". However, I am wondering if it was possible to run the install command in a way that it would install all packages listed under "require-dev" globally with one command?
It would be nice, since it would allow me to keep the test environments up-to-date with ease and allow for global access of all PHP Testing solutions I need. I know it is not much work and I would already be done with it if I just went through all of them manually, but I was curious to know if there was a nice way of doing this, since I think downloading and everything by hand, giving it execution rights and then moving it to the bin/ directory for global access is kind of a tedious solution.
According to the composer's help using the global keyword with the composer allow running commands in the global composer dir ($COMPOSER_HOME).
This means when you run composer global require phpunit/phpunit, composer will update its global directory instead of the directory you are in at that moment.
so in my case, $COMPOSER_HOME is in my home directory /Home/.composer, running the above command will;
update the /Home/.composer/composer.json file.
Download the latest phpunit package to the /Home/.composer/vender
Add a symlink to the phpunit executable file into the /Home/.composer/vendor/bin directory
so at this point if i have the bin folder included in my paths i would be able to run phpunit within my system regardless of which directory i'm running the command in.
Now if you prefer the dependency to be installed as part of the development requirement all you need to do is to add --dev parameter to the install (or update) command as well e.g. composer global require phpunit/phpunit --dev
What is that global install supposed to do?
The usual thing to do would be to install Phpunit. Now it wouldn't help you if you have two projects, one using the old version 3.7, and the newer using 4.6, to install any version globally.
Don't install dependencies globally that are used by a certain package explicitly.
However, tools that are not required, or are not stated to be required in a specific version, like PHP Codesniffer, can easily be installed centrally with
composer global require squizlabs/php_codesniffer
Then put the resulting path of ~/.composer/vendor/bin into the PATH environment variable. Note that ~ does not resolve there, you have to do that yourself.

Running PHPUnit in Laravel's Homestead

I'm using Homestead to serve my Laravel application. I'm trying to run PHPUnit. According to the docs:
An example test file is provided in the app/tests directory. After
installing a new Laravel application, simply run phpunit on the
command line to run your tests.
Well, when I'm "simply running" phpunit in my project root (inside the Homestead environment) I get this:
The program 'phpunit' is currently not installed.
Do I need to install PHPUnit separately then? The documentation does not mention it. What am I doing wrong?
You can install it globally on the system using.
composer global require phpunit/phpunit
However, if you need different versions for different projects this can cause issues.
The alternative option is to use the version installed as part of your dependencies by referencing the path to your vendor directory.
./vendor/bin/phpunit
You could even add an alias to your aliases file in your ~/Homestead directory. That way you're always using the phpunit version that is installed with your project dependencies.
alias phpunit=./vendor/bin/phpunit
You'll need to restart the homestead box to make use of the alias.
You can install it globally with:
$ composer global require "phpunit/phpunit=4.4.*"
# then use
$ phpunit
or you can use it with your local composer:
$ composer require "phpunit/phpunit=4.4.*"
# then
$ vendor/bin/phpunit
Since it's a package required for development, Laravel provide PHPunit(require-dev section in composer), you should find it in vendor's folder :
$ your_app/vendor/bin/
You can run the command from the root of your app folder by typing :
$ vendor/bin/phpunit
I hope it will help !

Categories