Site icon Web Niraj

Creating Symlinks to Libraries in Composer

When developing a new library in PHP, it’s sometimes useful to symlink the library into a project, rather than including the repository URL or Packagist link. That way, you can test the library out before committing the code.

Luckily, Composer provides an easy way to create a symlink in your project.

Composer Config for Git Repo

Let’s start with linking your project to your git-hosted library (useful if you haven’t published your library to Packagist). Simply add the following code to your composer.json file:

// ...
"repositories": [
      {
        "url": "git@github.com:niraj-shah/your-library.git",
        "type": "git"
    ],
// ...
"require": {
    "php": "^7.1.3",
    // ...
    "niraj-shah/your-library": "@dev",
    // ...
},
// ...

Once you’ve added the link to the repo, include the package name and version in the require block (as shown above). Once you’ve updated the config, run composer update to update the libraries in your project, and download the code from your git repo.

Composer Config for Local Folder (using Symlink)

If your library isn’t in a repo, or you rather work from a local version of the library (during development of the library), symlink is a great option to make sure the library is up to date with the dev version.

The below config creates a symlink in the vendor folder, pointing to the local version of the library. The symlink means it’s essentially a shortcut to the folder (not a copy). Any changes you make to the library will immediately be reflected in your project, making development much easier (no more having to run composer update for every change you make).

// ...
"repositories": [
  {
    "url": "/home/vagrant/code/your-library",
    "type": "path",
    "options": {
          "symlink": true
      }
  }
],
// ...
"require": {
    "php": "^7.1.3",
    // ...
    "niraj-shah/your-library": "@dev",
    // ...
},
// ...

As before, you need to remember to add the library package name to the require block. Run composer update to setup the symlink after updating the composer config – you don’t need to run the update again unless you want to update the other libraries in your project.

Exit mobile version