{"id":1088,"date":"2018-10-21T05:51:24","date_gmt":"2018-10-21T05:51:24","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw92\/?p=1088"},"modified":"2018-10-23T00:49:02","modified_gmt":"2018-10-23T00:49:02","slug":"how-to-install-and-use-composer-on-debian-9","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/10\/21\/how-to-install-and-use-composer-on-debian-9\/","title":{"rendered":"How To Install and Use Composer on Debian 9"},"content":{"rendered":"<p><a href=\"https:\/\/getcomposer.org\/\">Composer<\/a> is a dependency manager for PHP (similar to <a href=\"\/post\/how-to-install-node-js-on-debian-9\/\">npm for Node.js<\/a> or <a href=\"\/post\/how-to-install-pip-on-debian-9\/\">pip for Python<\/a>). Composer will pull in all the required PHP packages your project depends on and manage them for you.<\/p>\n<p>This tutorial provides the steps necessary to install Composer on Debian 9 systems. We will also cover how to use Composer to create and manage PHP projects.<\/p>\n<h2>Prerequisites<\/h2>\n<p>Before continuing with this tutorial, make sure you are logged in as a <a href=\"\/post\/how-to-create-a-sudo-user-on-debian\/\">user with sudo privileges<\/a> and you have <a href=\"\/post\/how-to-install-php-on-debian-9\/\">PHP installed on your Debian 9<\/a> system.<\/p>\n<h2>Installing Composer<\/h2>\n<p>Follow the steps below to install Composer on Debian systems:<\/p>\n<ol>\n<li>First update the packages index and install the necessary packages with the following commands:\n<p>sudo apt update<br \/>\nsudo apt install php-cli php-zip wget unzip<\/li>\n<li>Once the dependencies are installed, use the php cli toll to download the Composer installation script:\n<p>php -r &#8220;copy(&#8216;https:\/\/getcomposer.org\/installer&#8217;, &#8216;composer-setup.php&#8217;);&#8221;<\/p>\n<p>The command above will download the composer-setup.php file in the current working directory.<\/li>\n<li>We\u2019ll verify the script data integrity by comparing the script SHA-384 hash with the latest installer hash found on the <a href=\"https:\/\/composer.github.io\/pubkeys.html\">Composer Public Keys \/ Signatures<\/a> page.\n<p>Run the following <a href=\"\/post\/wget-command-examples\/\">wget command<\/a> to download the expected signature of the latest Composer installer from the Composer\u2019s Github page and store it in a variable named HASH:<\/p>\n<p>HASH=&#8221;$(wget -q -O &#8211; https:\/\/composer.github.io\/installer.sig)&#8221;<\/p>\n<p>To verify that the installation script is not corrupted copy and paste the following code into your console:<\/p>\n<p>php -r &#8220;if (hash_file(&#8216;SHA384&#8217;, &#8216;composer-setup.php&#8217;) === &#8216;$HASH&#8217;) { echo &#8216;Installer verified&#8217;; } else { echo &#8216;Installer corrupt&#8217;; unlink(&#8216;composer-setup.php&#8217;); } echo PHP_EOL;&#8221;<\/p>\n<p>If the hashes match, you\u2019ll see the following output:<\/p>\n<p>If the hashes doesn\u2019t match you will see Installer corrupt. In this case you will need to redownload the Composer installation script and double check the value of the $HASH variable with echo $HASH. Once the installer is verified, you can continue with the next step.<\/li>\n<li>The following command will install Composer in the \/usr\/local\/bin directory:\n<p>sudo php composer-setup.php &#8211;install-dir=\/usr\/local\/bin &#8211;filename=composerAll settings correct for using Composer<br \/>\nDownloading&#8230;<\/p>\n<p>Composer (version 1.7.2) successfully installed to: \/usr\/local\/bin\/composer<br \/>\nUse it: php \/usr\/local\/bin\/composer<\/p>\n<p>At this point you have Composer installed on your Debian system. It is installed as a system-wide command and it will be available for all users.<\/li>\n<li>To verify the installation simply type:\n<p>The command above will print the Composer\u2019s version, commands and arguments.<\/p>\n<p>______<br \/>\n\/ ____\/___ ____ ___ ____ ____ ________ _____<br \/>\n\/ \/ \/ __ \/ __ `__ \/ __ \/ __ \/ ___\/ _ \/ ___\/<br \/>\n\/ \/___\/ \/_\/ \/ \/ \/ \/ \/ \/ \/_\/ \/ \/_\/ (__ ) __\/ \/<br \/>\n____\/____\/_\/ \/_\/ \/_\/ .___\/____\/____\/___\/_\/<br \/>\n\/_\/<br \/>\nComposer version 1.7.2 2018-08-16 16:57:12<\/p>\n<p>Usage:<br \/>\ncommand [options] [arguments]<\/li>\n<\/ol>\n<p>If you want to use a per-project Composer installation run the following command php composer-setup.php which will create a file named composer.phar in your current working directory. You can use the composer by running .\/composer.phar &lt;command&gt;.<\/p>\n<h2>Getting Started with Composer<\/h2>\n<p>Now that you have Composer is installed on your Debian system we will show you how to create a PHP project with Composer.<\/p>\n<p>Start by creating a a directory which will be the project root and hold the composer.json file. This file describes your PHP project including the PHP dependencies and other metadata.<\/p>\n<p>Create the project directory and switch to it with:<\/p>\n<p>mkdir ~\/my-first-composer-project<br \/>\ncd ~\/my-first-composer-project<\/p>\n<p>Next, we\u2019ll initialize a new composer.json file using the composer require &lt;package name&gt; command and specify the package we want to download. In this example we will create a sample application that will print the current time using a package called <a href=\"https:\/\/carbon.nesbot.com\/docs\/\">carbon<\/a>.<\/p>\n<p>Run the following command to initialize a new composer.json file and install the carbon package:<\/p>\n<p>composer require nesbot\/carbonUsing version ^1.34 for nesbot\/carbon<br \/>\n.\/composer.json has been created<br \/>\nLoading composer repositories with package information<br \/>\nUpdating dependencies (including require-dev)<br \/>\n&#8211; Installing symfony\/polyfill-mbstring (v1.9.0): Downloading (100%)<br \/>\n&#8211; Installing symfony\/translation (v3.4.17): Downloading (100%)<br \/>\n&#8211; Installing nesbot\/carbon (1.34.0): Downloading (100%)<br \/>\nsymfony\/polyfill-mbstring suggests installing ext-mbstring (For best performance)<br \/>\nsymfony\/translation suggests installing symfony\/config<br \/>\nsymfony\/translation suggests installing symfony\/yaml<br \/>\nsymfony\/translation suggests installing psr\/log-implementation (To use logging capability in translator)<br \/>\nWriting lock file<br \/>\nGenerating autoload files<\/p>\n<p>Composer will create a composer.json file and download and install carbon and all its dependencies.<\/p>\n<p>If you list your project\u2019s directory, you will see that it contains two files composer.json and composer.lock, and a vendor directory.<\/p>\n<p>-rw-r&#8211;r&#8211; 1 linuxize users 60 Oct 16 21:02 composer.json<br \/>\n-rw-r&#8211;r&#8211; 1 linuxize users 6851 Oct 16 21:02 composer.lock<br \/>\ndrwxr-xr-x 5 linuxize users 4096 Oct 16 21:02 vendor<\/p>\n<ul>\n<li>vendor &#8211; the directory where the project dependencies are stored.<\/li>\n<li>composer.lock &#8211; file containing a list of all installed packages including the exact packages versions.<\/li>\n<li>composer.json &#8211; file describing the PHP project and all PHP dependencies.<\/li>\n<\/ul>\n<p>You can search the Composer repository <a href=\"https:\/\/packagist.org\/\">Packagist<\/a> for PHP packages.<\/p>\n<p>Composer provides autoload capabilities which allows us to use PHP classes without the need to require or include the files.<\/p>\n<p>Create a file named testing.php and paste the following code:<\/p>\n<p>&lt;?php<\/p>\n<p>require __DIR__ . &#8216;\/vendor\/autoload.php&#8217;;<\/p>\n<p>use CarbonCarbon;<\/p>\n<p>printf(&#8220;Now: %s&#8221;, Carbon::now());<\/p>\n<p>Let\u2019s analyze the code line by line.<\/p>\n<p>In the first line after the opening php tag we are including the vendor\/autoload.php file that was automatically generated by Composer. This file will autoload all the required libraries.<\/p>\n<p>Next we are aliasing CarbonCarbon as Carbon and with the last line we are printing the current time using the Carbon now method.<\/p>\n<p>Run the script by typing:<\/p>\n<p>The output should look something like below:<\/p>\n<h3>3. Updating Packages with Composer<\/h3>\n<p>To update the PHP packages, simply run:<\/p>\n<p>The command above will check for newer versions of the installed packages and if a newer version is found and the <a href=\"https:\/\/getcomposer.org\/doc\/articles\/versions.md\">version constraint<\/a> match with the one specified in the composer.json, Composer will update the package.<\/p>\n<h2>Conclusion<\/h2>\n<p>You have learned how to install Composer on your Debian 9 machine. We have also shown you how to use Composer to create a basic PHP project. To find more information about Composer visit the official <a href=\"https:\/\/getcomposer.org\/doc\/\">Composer documentation<\/a> page.<\/p>\n<p>If you have any question, please leave a comment below.<\/p>\n<p><a href=\"http:\/\/lxer.com\/module\/newswire\/ext_link.php?rid=261826\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Composer is a dependency manager for PHP (similar to npm for Node.js or pip for Python). Composer will pull in all the required PHP packages your project depends on and manage them for you. This tutorial provides the steps necessary to install Composer on Debian 9 systems. We will also cover how to use Composer &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/10\/21\/how-to-install-and-use-composer-on-debian-9\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How To Install and Use Composer on Debian 9&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1088","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/1088","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=1088"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/1088\/revisions"}],"predecessor-version":[{"id":1352,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/1088\/revisions\/1352"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=1088"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=1088"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=1088"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}