Configure Composer to use Nexus

NHAILA Achraf
3 min readJun 30, 2021
Nexus Repository OSS ( open source repository)

Nexus OSS is free also can publish and retrieve versioned apps and their dependencies by using central repositories that are accessible from other environments

Nexus has large community support and supports many types of repositories ( today we’re gonna use composer repo).

To make our work easier, we’re going to build a docker image nexus.

Table Of Contents

1 INSTALL NEXUS USING DOCKER

2 USING COMPOSER WITH NEXUS

1- INSTALL NEXUS USING DOCKER

Clone & Build with Docker

git clone https://github.com/sonatype-nexus-community/nexus-repository-composer.gitdocker build -t nexus-repository-composer .

Run as a Docker container

docker run -d -p 8081:8081 --name nexus-repository-composer nexus-repository-composer

The application will now be available from your browser at http://localhost:8081

As of Nexus Repository Manager Version 3.17, the default admin password is randomly generated. If running in a Docker container, you will need to view the generated password file (/nexus-data/admin.password) in order to login to Nexus. The command below will open a bash shell in the container named nexus-repository-composer:

docker exec -it nexus-repository-composer /bin/bash $ cat /nexus-data/admin.password

Once logged into the application UI as admin using the generated password, you should also turn on "Enable anonymous access" when prompted by the setup wizard.

we proceed with a creation of 2 composer Repositories

1–1 Proxying Composer Repositories

The current spike implementation makes certain assumptions about the layout of the upstream repository and is only intended for use with the main Composer repository at https://packagist.org.

To proxy a Composer repository, you simply create a new ‘composer (proxy)’ as documented in Repository Management in detail. Minimal configuration steps are:

  • Define ‘Name’
  • Define URL for ‘Remote storage’ e.g. https://packagist.org/
  • Select a ‘Blob store’ for ‘Storage’

1–2 Hosting Composer Repositories

Creating a Composer hosted repository allows you to register packages in the repository manager. The hosted repository acts as an authoritative location for these components.

To add a hosted Composer repository, create a new repository with the recipe ‘composer (hosted)’ as documented in Repository Management.

Minimal configuration steps are:

  • Define ‘Name’ — e.g. composer-hosted
  • Select ‘Blob store’ for ‘Storage'

Example :

2- Using Composer With Nexus3

to configure composer with nexus we must proceed with 2 steps :

#2–1 update composer.json

#2–2 create auth.json

2–1 Add your Nexus URL Repo use the following command in your project :

update composer.json :

composer config repo.myrepo composer https://localhost:8081/repository/packagist/

where myrepo is just an indexed name.

If you want do disable Packagist for your project issue this command composer config repo.packagist false

Example config composer.json

{
"name": "href/myrepo",
"require-dev": {
"predis/predis": "^1.1"
},
"authors": [
{
"name": "NHAILA Achraf",
"email": "achraf.nhaila@gmail.com"
}
],
"require": {
"twbs/bootstrap": "5.0.2"
},
"repositories": {
"repo-php": {
"type": "composer",
"url": "https://localhost:8280/repository/COMPOSER-Repo/"
},
"packagist": false
}
}

2–2 create auth.json

{
"http-basic": {
"nexus.machine.com": {
"username": "username",
"password": "password"
}
}
}

Now, when you run composer install, composer will try to get their dependencies from Nexus, and if Nexus does not have them, it will look for them on the Internet.

If you have any questions, please use comments.

Goodbye.

--

--