{"id":166201,"date":"2021-01-29T17:00:57","date_gmt":"2021-01-29T14:00:57","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-install-and-use-composer-phps-dependency-manager-cloudsavvy-it\/"},"modified":"2021-01-29T17:00:57","modified_gmt":"2021-01-29T14:00:57","slug":"how-to-install-and-use-composer-phps-dependency-manager-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-install-and-use-composer-phps-dependency-manager-cloudsavvy-it\/","title":{"rendered":"#How to Install and Use Composer, PHP\u2019s Dependency Manager \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a23b0e8c39cf\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a23b0e8c39cf\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-install-and-use-composer-phps-dependency-manager-cloudsavvy-it\/#Installing_Composer\" >Installing Composer<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-install-and-use-composer-phps-dependency-manager-cloudsavvy-it\/#Preparing_Your_Project\" >Preparing Your Project<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-install-and-use-composer-phps-dependency-manager-cloudsavvy-it\/#Installing_Packages\" >Installing Packages<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-install-and-use-composer-phps-dependency-manager-cloudsavvy-it\/#Updating_Packages\" >Updating Packages<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-install-and-use-composer-phps-dependency-manager-cloudsavvy-it\/#The_Composer_Autoloader\" >The Composer Autoloader<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-install-and-use-composer-phps-dependency-manager-cloudsavvy-it\/#Summary\" >Summary<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Install and Use Composer, PHP\u2019s Dependency Manager \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4047\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/b835749967babe94be45e3edd2508d9c\/p\/uploads\/2017\/08\/1ac500da.png\" alt=\"PHP Logo\" width=\"1400\" height=\"600\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/getcomposer.org\/\">Composer<\/a> is the PHP community\u2019s go-to dependency manager. It simplifies installing, updating and using third-party packages. Packages can be hosted by public and private repositories, with most popular projects publishing to <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/packagist.org\/\">Packagist<\/a>.<\/p>\n<h2 id=\"installing-composer\"><span class=\"ez-toc-section\" id=\"Installing_Composer\"><\/span>Installing Composer<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Composer is a community effort which isn\u2019t bundled with PHP. It\u2019s distributed as a PHP PHAR archive from <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/getcomposer.org\/\">getcomposer.org<\/a>. Some Linux distributions do include Composer in their software repositories but installing in this way usually delivers an outdated version.<\/p>\n<p>Make sure you\u2019ve got PHP installed before continuing. PHP 5.3 is the oldest supported version at the time of writing. You\u2019ll also need <code>git<\/code> and <code>unzip<\/code> on your system if you want to install packages from source.<\/p>\n<p>Composer provides an automated setup script. Begin by <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">download<\/a>ing the installer to your working directory:<\/p>\n<pre>curl https:\/\/getcomposer.org\/installer -o composer-setup.php<\/pre>\n<p>You should now verify the installer\u2019s hash to check it\u2019s not been tampered with. Refer to the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/getcomposer.org\/download\/\">Composer website<\/a> to check the latest hash and obtain sample verification code.<\/p>\n<p>Next, use the setup script to install Composer:<\/p>\n<pre>php composer-setup.php --install-dir=\/usr\/local\/bin --filename=composer<\/pre>\n<p>This will download Composer into <code>\/usr\/local\/bin<\/code>, allowing it to sit in your path alongside your other executables. Try running <code>composer<\/code> in your shell to check that\u2019s everything working. The Composer version should be displayed, followed by a list of the available Composer commands.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-9128\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/813bd15a0329f57c17cce61ab7f95710\/p\/uploads\/2021\/01\/9bf67b6a.jpg\" alt=\"Screenshot of Composer's help output\" width=\"944\" height=\"468\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>To update Composer in the future, run <code>composer self-update<\/code>. This will automatically replace your Composer binary with the latest release. You won\u2019t need to use the <code>composer-setup.php<\/code> script again so you can delete it now.<\/p>\n<h2 id=\"preparing-your-project\"><span class=\"ez-toc-section\" id=\"Preparing_Your_Project\"><\/span>Preparing Your Project<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>You\u2019ll need to create a <code>composer.json<\/code> file in your project\u2019s working directory before you can start using Composer. Run <code>composer init<\/code> to create one interactively. This will get you setup with a basic configuration.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-9132\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/90d08bfe7da90ca72283db9f6e2ed4dd\/p\/uploads\/2021\/01\/5021795f-1.jpeg\" alt=\"Screenshot of the Composer init command\" width=\"940\" height=\"536\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Follow the command line prompts to supply information about your project, such as its name, description and author details. Package names use the <code>vendor\/package<\/code> syntax to avoid conflicts between authors in public repositories. You should use your Packagist username as the <code>vendor<\/code> component.<\/p>\n<p>Most keys in <code>composer.json<\/code> are optional unless you intend to publish your codebase to Packagist. You can find a complete description of the file\u2019s structure on the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/getcomposer.org\/doc\/04-schema.md\">Composer documentation<\/a> site.<\/p>\n<h2 id=\"installing-packages\"><span class=\"ez-toc-section\" id=\"Installing_Packages\"><\/span>Installing Packages<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>You can add packages to your project using the <code>composer require<\/code> command:<\/p>\n<pre>composer require vendor\/package<\/pre>\n<p>Search for packages to install using the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/packagist.org\/\">Packagist website<\/a>. They\u2019ll be added to the <code>require<\/code> section of your project\u2019s <code>composer.json<\/code> file. Once installed, the package\u2019s source goes into the <code>vendor<\/code> folder within your project.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-9130\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/21a501682df4ea353e647c5b8f9f8732\/p\/uploads\/2021\/01\/fee3321d.jpeg\" alt=\"Screenshot of a Composer.json file\" width=\"622\" height=\"292\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Composer relies on semantic versioning to handle package updates. The exact version of each package you\u2019ve installed is written to <code>composer.lock<\/code> in your project\u2019s directory. This enables Composer to identify the specific package to install when <code>composer.json<\/code> indicates a range of versions is acceptable.<\/p>\n<p>You should commit both <code>composer.json<\/code> and <code>composer.lock<\/code> to your source control. Other developers working on your project can then run <code>composer install<\/code> to acquire all the dependencies you\u2019ve defined.<\/p>\n<p>Packages such as test runners can be marked as development dependencies by supplying the <code>--dev<\/code> flag to the <code>require<\/code> command. They\u2019ll be separated into a <code>require-dev<\/code> section within <code>composer.json<\/code>. When installing your packages, use <code>composer install --no-dev<\/code> to exclude development dependencies. This is useful when using Composer within deployment scripts and CI systems.<\/p>\n<h2 id=\"updating-packages\"><span class=\"ez-toc-section\" id=\"Updating_Packages\"><\/span>Updating Packages<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>You should try to keep your packages updated so you\u2019re not missing out on security and bug fixes. Run the <code>composer outdated<\/code> command to see a list of dependencies in `composer.json` that have new versions available.<\/p>\n<p>To apply the updates, run <code>composer update<\/code>. This will respect semantic versioning and pull down the newest version of each package, within the version constraints specified by your <code>composer.json<\/code>. A package marked as <code>^1.2<\/code> will update to 1.2.x or 1.3.x but not 2.0. The <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/getcomposer.org\/doc\/articles\/versions.md\">Composer docs<\/a> include detailed information on how the tool resolves different forms of version constraint.<\/p>\n<p>Updating a package will automatically rewrite your <code>composer.lock<\/code> file to specify the new version. Other developers working on your project can rerun <code>composer install<\/code> to obtain the exact packages you\u2019re using.<\/p>\n<h2 id=\"the-composer-autoloader\"><span class=\"ez-toc-section\" id=\"The_Composer_Autoloader\"><\/span>The Composer Autoloader<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Autoloading is the preferred mechanism to discover source files within PHP. Composer has first-class support for autoloading; most of the time, its autoloader will be the only file you need to <code>require_once()<\/code> within your project.<\/p>\n<p>As you install dependencies, Composer will automatically write an autoloader to <code>vendor\/autoload.php<\/code>. Packages specify how they should be autoloaded using the <code>autoload<\/code> field in <code>composer.json<\/code>. You should set this up for your own project so Composer can autoload its files too:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"json\">\n<pre class=\"de1\">{&#13;\n    \"autoload\": {&#13;\n        \"psr-4\": {&#13;\n            \"ExampleProject\\\": \"src\/\"&#13;\n        }&#13;\n    }&#13;\n}<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>The above snippet will configure your project\u2019s autoloading using <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.php-fig.org\/psr\/psr-4\/\">the PSR-4 standard<\/a>. Codebase resources within the <code>ExampleProject<\/code> namespace will be mapped to files within the <code>src<\/code> directory \u2013 for example, <code>use ExampleProjectExampleClassesMyClass<\/code> will automatically <code>require_once(\"src\/ExampleProject\/ExampleClasses\/MyClass.php\")<\/code>.<\/p>\n<p>The only file you\u2019ll need to manually <code>require_once()<\/code> is the autoloader itself:<\/p>\n<pre class=\"php\">require_once(__DIR__ . \"\/vendor\/autoload.php\");<\/pre>\n<p>You should add the line as early as possible in your application. This will ensure autoloading is enabled before you start consuming classes and interfaces within your codebase.<\/p>\n<p>Sometimes you might need to force regeneration of the autoloader. This will often be because you\u2019ve updated your project\u2019s <code>autoload<\/code> configuration. You can run <code>composer dump-autoload<\/code> to write a new autoloader on-demand.<\/p>\n<h2 id=\"summary\"><span class=\"ez-toc-section\" id=\"Summary\"><\/span>Summary<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Composer simplifies PHP development by providing the dependency manager missing from the core language. Using Composer you can easily incorporate third-party code into your projects, without having to manually download source files and keep them up to date.<\/p>\n<p>Composer\u2019s built-in autoloader lets you access installed packages without any extra work on your part. The tool also <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/getcomposer.org\/doc\/articles\/scripts.md\">includes a script runner<\/a> which allows you to execute tasks within your codebase by adding commands to the <code>scripts<\/code> block in <code>composer.json<\/code>. Use <code>composer run my-script<\/code> to run the script.\n<\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/9125\/how-to-install-and-use-composer-phps-dependency-manager\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Install and Use Composer, PHP\u2019s Dependency Manager \u2013 CloudSavvy IT&#8221; Composer is the PHP community\u2019s go-to dependency manager. It simplifies installing, updating and using third-party packages. Packages can be hosted by public and private repositories, with most popular projects publishing to Packagist. Installing Composer Composer is a community effort which isn\u2019t bundled with&#8230;<\/p>\n","protected":false},"author":1,"featured_media":166202,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2017\/08\/1ac500da.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-166201","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/166201","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=166201"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/166201\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/166202"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=166201"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=166201"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=166201"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}