Magento 2 installing new php library via composer

by Iva Korlevic
July 17, 2017

Hi,
I had to install php library phpoffice/phpexcel for reading and writing excel files with php. The easiest solution seemed to be to use composer. But running the command
composer require phpoffice/phpexcel
gave me an error:

composer require phpoffice/phpexcel

[Composer\Downloader\TransportException]
Invalid credentials for 'https://repo.magento.com/packages.json', aborting.

require [--dev] [--prefer-source] [--prefer-dist] [--no-plugins] [--no-progress] [--no-update] [--update-no-dev] [--update-with-dependencies] [-ignore-platform-reqs] [--sort-packages] [-o|--optimize-autoloader] [-a|--classmap-authoritative] [--] []...

There were a couple of things I had to do to make my composer install the needed library.

Add your public and private keys

In order to use the composer with magento 2 you first need to add the authentication details to your magento. That’s easy-peasy.

  • Go to magento marketplace and sing in to your account or if you have none, then sign up and create the account.
  • Click on Access Keys in your profile. And this will take you to the area with your access keys.
  • Here you’ll see a big orange button “Create new Access Key” – click on it and it will open a modal with input form. In the input field write down you project name (like “text.com”) and click on ok. This will generate both public and private keys.
  • You will now see your access keys in table below.

Now let’s add our access keys to our magento installation.

  • Go to admin area of your store
  • There go to System -> Tools -> Web Setup Wizard
  • Then click on System Configuration and it will open the form with private and public key input.
  • Fill in the needed data by copying the access keys from magento marketplace and click save config

And voila! You should be done now and should be able to run your composer command.

Not working even though i added the keys

If you’re like me, then there will be some difficulties. If you added the access keys and it still gives you the same error then check your composer by running:
composer diagnose

In my case running this gave me this notice:

composer diagnose
Checking composer.json: FAIL
The version field is present, it is recommended to leave it out if the package is published on Packagist.
Defining autoload.psr-0 with an empty namespace prefix is a bad idea for performance
require.composer/composer : unbound version constraints (@alpha) should be avoided
Checking platform settings: OK
Checking git settings: OK
Checking http connectivity to packagist: OK
Checking https connectivity to packagist: OK
Checking github.com rate limit: OK
Checking disk free space: OK
Checking pubkeys:
Tags Public Key Fingerprint: xxxxxxxx xxxxxxxx xxxxxxx xxxxxxx
Dev Public Key Fingerprint: xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxx
OK
Checking composer version: FAIL
You are not running the latest stable version, run `composer self-update` to update (1.0.3 => 1.4.2)

It turned up that my composer version was old. So i simply run the next command:
composer self-update

And updated my composer to new stable version.
After it completed the update I again ran my line:

composer require phpoffice/phpexcel

And guess what – i worked! I finally installed the needed library.
I really hope this little post helps some one whose composer is not playing along.

Cheers!

Comments

0 thoughts on “Magento 2 installing new php library via composer”

Tell us your opinion:

Your email address will not be published. Required fields are marked *