4

After having had some problems while trying to upgrade Magento 2, or when installing new modules, I often find the suggestion of "using Composer" to do it.

However, as a designer (not really a developer) when I search about it on the internet, I find articles that describe advanced stuff, all sort of details assuming I know what it's all about, which unfortunately is not the case.

Is Composer installed on my local machine? Or in the server? Will it help upgrading / installing new modules?

I do not fear the command lines but I just don't get the general picture yet. Do I need to dig deep in the server to have it working?

Thanks,

2

2 Answers 2

4

Composer is an application package manager. It is an application that needs to be installed on your local machine. When used, it gathers, installs, and autoloads into the framework all the various dependencies required to operate the site (such as libraries, the Magento core codebase, and other frameworks required to operate a Magento 2 site). You can also use it to manage your own modules/themes as packages if desired.

Once installed, it can be run in the command-line in any directory that contains a valid composer.json or composer.lock file. In the Magento 2 codebase these files are located in docroot.

There are two composer commands in common use. composer install and composer update. The command composer update will read the composer.json file and go through each item in it and check the various remote packages (you'll need some keys to access repo.magento.com (the place online where magento 2 core code packages are hosted), these can be obtained via a free Magento account).

If everything checks out, the composer update command will create a new file composer.lock. This file contains a snapshot of all the various packages along with their version numbers that have been installed by composer update. It it recommended to submit the lock file into version control.

If a composer.lock file is present, you can run composer install and it'll just read the lock file and install from there. This can help as if minor updates are issues to packages, developers can be using slightly different versions of packages depending on when they ran composer update. If a lock file is present and each developer runs composer install they'll be using the same package versions.

In summary, go to Magento 2 docroot and look for a composer.lock file. If it's there just run composer install (and enter keys if/when prompted). If it's not there then run composer update and then don't forget to submit the new composer.lock file to your version control system.

Once composer has downloaded and installed all the packages, you will have a vendor folder in your docroot. This contains all framework dependencies and the core Magento 2 codebase. If you're using composer, your app/code folder should only contain new modules not part of the Magento 2 core framework.

Now when it comes time to upgrade Magento, because everything is managed by composer it's a matter of changing the version number in the composer.json file and running composer update to have composer manage all the required packages, pull them down from their remote repositories, install them, and create a new composer.lock file that records all the new installed packages.

6
  • I'd advise against editing the composer.json file because you may introduce a syntax error - just use the composer commands instead (Run php vendor/bin/composer in your Magento docroot to list the most common ones). Aug 2, 2018 at 8:26
  • Interesting and good point that you can use composer commands to add dependencies (ie, composer can edit the JSON to help reduce potential of errors). I'm personally okay editing composer.json as my IDE runs code inspections for valid JSON, and the resulting file goes through a testing process to ensure code submissions are sane. But point taken that it can be easier/safer in some situations to just use composer commands to manage things. Aug 2, 2018 at 10:33
  • Great explanation thanks, so let's say that I have my staging site online (in a remote server). Then I install composer locally in my machine (not the server, but my computer), then I go to a local copy of Magento and work from there? How does it synch with the remote server's staging site then?
    – Peanuts
    Aug 2, 2018 at 17:12
  • 1
    @Peanuts you need the composer app installed on each environment. When you run composer install or composer update it'll download required packages from online repositories. I'd recommend against running it on a production server without testing on a local environment as it's likely that you'll need to change the codebase slightly to get things working. Aug 2, 2018 at 17:28
  • 1
    Yeah, you'll need SSH (secure shell) access to run the composer commands. Aug 2, 2018 at 18:01
2

I would just add to @ByteCreation answer:

If you want to install a specific version you just need to change the last number of this command:

composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition=2.2.5 /installation/directory/path

And answering about using composer for installing modules; it is the easiest way but it modifies vendor and that in some cases is not proper. It depends if you only have 1 store and you don't have any problem with it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.