What are the differences between composer update and composer global update in yii2?
It's not anything to do with Yii directly.
Composer allows you to install dependencies globally or per-project (the default).
https://getcomposer.org/doc/03-cli.md#global
This is merely a helper to manage a project stored in a central location that can hold CLI tools or Composer plugins that you want to have available everywhere.
You might want to install something like phpunit or phpcs globally (so it's available for every project) whereas installing a library or framework that you need for your project should be a per-project installation.
Related
I have a hard time figuring this out; I am also puzzled of the connection - if any - with the create-project command.
As far as I can tell, the only difference between install and create-project is the execution of post-root-package-install and post-create-project-cmd hooks...
Can someone shed some more light on this?
My goal is to set up a composer infrastructure where I run create-project and this sets up a project skeleton (creates and sets directory permissions, creates default configuration files, creates blank data stores)
What exactly is the difference between project and library types in composer?
Practically, there is none. It doesn't affect how composer gets executed. You can use both install and create-project with both types of package types.
This metadata is meant to inform plugins, IDE, or even packagist.org when parsing composer.json, but on a vainilla installation, there is no practical difference in using one or the other.
(Docs on package types)
As far as I can tell, the only difference between install and create-project is the execution of post-root-package-install and post-create-project-cmd hooks.
The docs are your friends:
You can use Composer to create new projects from an existing package. This is the equivalent of doing a git clone/svn checkout followed by a composer install of the vendors.
Any time you do create-project, install is executed as well. Which means that the install hooks are executed as well.
First it clones the whole package via the appropriate CVS (git, usually), and then immediately executes composer install. By default, it removes CVS information (e.g. the .git directory), unless one uses the --keep-vcs option.
create-project is useful to bootstrap applications, so the app's directory is setup beyond what downloading composer`s dependencies would do. You can create a skeleton directory structure, etc.
Usually one would have a package proper (that could be required into an application), and a "application-skeleton" package, that would include the directory structure and would depend on the original package.
I'm posting a more succint answer to my questions, based on experimentation:
What is the difference between project and library types?
Absolutely none as far as composer is concerned. Some plugins might implement logic to treat the two package types differently though.
How does that relate to require, install, create-project commands?
In no way whatsoever. The project in package type has nothing to do with project in create-project.
How does create-project work?
Let's say we are talking about a single <package>. We have 2 workflows:
composer init <project_path> && composer require <package>
composer create-project <package> <project_path>
The first workflow will create a blank root package and add <package> as a requirement to it.
The second workflow will "clone" <package> into <project_path> as the root package.
NB: If you are working with local path type repos for development, create-project will actually create <project_path> as a symlink to <package>'s source dir. This is the default behavior of path repos, and probably a miss for composer in the need to treat create-project differently. This can create a heap of confusion (as you might be inadvertently changing and adding to <package>s sources while thinking you are just editing project_path). So for local development and testing, you are better off with cp -A rather than composer create-project.
I found there are two options to install PHP package globally in Linux (Ubuntu 16.04):
Using composer:
composer global require symfony/finder
The package will be located at ~/.config/composer/vendor/
Using apt-get:
apt-get install php-symfony-finder
The package will be located at /usr/share/php/
This directory /usr/share/php/ is also in default PHPs include_path (I have PHP 7.2)
There are several questions I have:
Why would I want to install package globally ?
I know it's useful to install php tools globally, like phpunit - It has binary file and it allows you to run tests everywhere, so you don't have to install it in every project.
But what about symfony/finder for example ? What is particular use of this package installed globally ?
What is the difference between 1 and 2 option ?
Does it have any different use cases or different effects ?
Why would I want to install package globally ?
Normally, these are dependencies you want to use in almost every project, because they are available at a system level you can use them without duplicating their dependencies in every application you create.
For example, in my case I have php_md, php_cs for code formatting, phpunit for testing.
What is the difference between 1 and 2 option ?
Both are package managers, they make sure every package installed has the correct dependencies, so their core functionality is similar.
Now, they have several differences:
Their focus in the packages they manage, composer is specific for php based packages but apt-get is for Linux and more system level oriented.
Their package database, composer uses packagist and apt-get uses a selection of repositories and ppas (you can find them in /var/lib/apt/lists/).
The package selection, since composer is specialized in php you can expect a wider variety in anything php related.
In conclusion, you can clearly make it work with both, but I would recommend you to keep everything php related on composer, unifying them under the same manager.
Any other difference or correction I've overlooked is welcome.
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.
I see I can add the following to my composer.json file:
{
"require-dev": {
"phpunit/phpunit": "4.2.*"
}
}
But I have PHPUnit installed, I can run it from the command line without the above. Why would I install it as a dependency? Does that mean I don't need to install it?
Also, the following let's me install it globally - composer global require "phpunit/phpunit=4.2.*" - where is this set? If I install it globally, can I unrequire it later if I choose to install on a project by project basis. I'm really just finding my way around the framework and don't want to set anything I can't reverse.
http://phpunit.de/manual/current/en/installation.html
You don't have to use a require-dev section for your development dependencies, if they are already on your development machines. If PHPUnit is already globally installed, there is no need to install it in your project.
When your project has a lot of developers, you can't pretend that all of them have PHPUnit installed globally. Also, the PHPUnit version used in your project might matter. By using the require-dev section you explicitly say, that this specific version is a development dependency. When working with Continuous Integration servers, some of them have PHPUnit installed globally, some of them, like Jenkins would need additional steps (like a global installation) then it becomes handy to install the dependency on a per-project level.
The command composer global require "phpunit/phpunit=4.2.*" will install PHPUnit and all its dependencies into the ~/.composer/vendor/ directory and the CLI tools into the bin folder ~/.composer/vendor/bin/. As you can see this is a per user installation (~).
You can remove it by editing the ~/.composer/composer.json file, removing the PHPUnit dependency and then running composer global update.
I created a project with Laravel and downloaded from git via this command:
git clone -b develop git://github.com/laravel/laravel.git
The file size was about 21MB,
I want to know should I download Laravel for every project with this command?
What you have done is cloned the framework itself, which you should only do if you're going to fork and develop the Laravel core.
What you should do instead is use Composer to install your Laravel projects. You'll also be using Composer for other dependency-related actions in said projects (including autoload). This is the proper way of installing a fresh Laravel framework for developing a website:
composer create-project laravel/laravel --prefer-dist
http://laravel.com/docs/installation
Then, any future Laravel projects you create will be loaded from your Composer cache without needing to re-download.
The Composer package also sets up all your vendor-related .gitignore information and includes several other really useful management features. This is important, because you only want to keep your application-specific code under git version control, not the framework itself or any other dependencies. (Otherwise, your diffs and commits will get polluted with the dependencies' development changes.)
Once you've created a repository for your project, and installed Laravel with Composer, and created your first few commits (with some migrations, models, and controllers, for instance), cloning your project usually works something like this:
cd /clone-here
git clone /myproject # Location of current project
# /clone-here now has only the application-specific files from /myproject. It is
# still missing the framework itself and other dependencies.
composer install # Composer now looks at the dependencies in
# /clone-here/composer.json and installs them into /clone-here/vendor
# including the Laravel framework.
# Now the framework and other dependencies are good to go.
php artisan migrate # Laravel makes all your DB schemas from your migrations
php artisan db:seed # Seed your lovely new DB tables
It's really elegant and fun once you get used to it.
Edit:
See Sheikh's answer to save some time in the Composer install process!
Already Leng gave a nice answer.
Installing Laravel since version-4.1* via Laravel Installer is faster than composer
First, download the Laravel installer PHAR archive. For convenience,
rename the file to laravel and move it to /usr/local/bin. Once
installed, the simple laravel new command will create a fresh Laravel
installation in the directory you specify. For instance, laravel new
blog would create a directory named blog containing a fresh Laravel
installation with all dependencies installed. This method of
installation is much faster than installing via Composer.