54

I tried to install package via composer but it's keep telling me that this directory is not empty so how can I install package on non-empty folder using composer ? is there force install command ?

error message:

  [InvalidArgumentException]
  Project directory Yii-GZero-2b/ is not empty.
0

7 Answers 7

67

You currently can't. There is/was a discussion going on over on GitHub which you can read about here: https://github.com/composer/composer/issues/1135.

For now however, you'll need to create a new directory and then migrate your files over, or vice-versa.

2
  • 13
    I agree with cwallenpoole. I consider it a bug. I'm running VirtualBox using Vagrant. My vagrantfile contains the configs for the VM and needs to be in the project directory to run the VM. That means CakePHP won't install in the project directory because it is not empty with the VM configs there. This needs a solution.
    – Tanoro
    Commented Nov 15, 2018 at 21:40
  • Uh that sucks... Usually we're using DDEV to setup the project environment, e.g. for PHP 7.4 projects. Then inside DDEV we can run Composer and it will derive the right PHP version. Running Composer on the host fails, if e.g only PHP 8.1 is installed but the project dependencies requiring 7.4.
    – Moongazer
    Commented Mar 8, 2023 at 17:35
3

You can check all the . files in the folder.

$ ls -la

Delete any . file in the folder and try again. It worked for me.

1
  • Thanks! I did ls -ls which didnt show the hidden .gitignore file.
    – Chris
    Commented Jul 30, 2020 at 12:18
1

For me giving the folder write access worked very well. On windows I right clicked on the mentioned folder and unchecked the Read Only checkbox and saved it and it worked. May be you could also give it a try hope it will work for you as well.

0

This is a workaround process for someone using a docker container. The basic idea will also work for anyone on a unix/bsd os filesystem.

In my case, I have a container that maps the project directory with some existing files mapped to both php and apache containers in a docker-compose.yml file like this:

  php:
    build: './php'
    restart: always
    networks: 
      - backend
    volumes:
      - ./:/usr/local/apache2/htdocs
      - ./tmp:/usr/local/tmp

This assumes that in the project directory there's a /php directory with a Dockerfile containing something like this:

FROM php:7.4.33-fpm-alpine3.16
RUN apk update; \
    apk upgrade; \
    apk add bash; \
    apk add shadow;
RUN docker-php-ext-install mysqli opcache pdo pdo_mysql
RUN apk add freetype libpng libjpeg-turbo freetype-dev libpng-dev libjpeg-turbo-dev zlib-dev icu-dev
RUN docker-php-ext-configure gd --with-freetype=/usr/include/ --with-jpeg=/usr/include/ 
RUN docker-php-ext-install -j$(nproc) gd
RUN docker-php-ext-install intl
COPY ./conf/php.ini /usr/local/etc/php/php.ini
COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer

For my needs I am still using php 7.x, but the idea will work with the latest PHP. The important thing to note is that the PHP container has composer added from the official composer container.

Once the containers are started, I will use docker-compose ps to get the php container name, which will be something like project-php-1 in most cases.

Then use docker to exec into the container with docker exec -it project-php-1 /bin/bash

This should drop you into the container, with your project directory mapped to /usr/local/apache2/htdocs

cd /usr/local/apache2/htdocs

cd ..

Now run the composer create command exactly as you would if you were in the local directory where you would otherwise have composer create a new project, and name it whatever you would have normally. I'll just use the same project/folder name because there's no conflict in the container.

For example this would init the skeleton needed for a symphony 5.x project:

composer create-project symfony/skeleton:"^5.4" myproject

Now you'll have a directory in the container named "myproject"

cd into that and use the cp command to recursively copy all contents to the mapped /usr/local/apache2/htdocs directory (which will be your actual existing project directory).

cd myproject

cp -R * ../htdocs

You will now have all the files needed in your project to continue working.

I also have a .gitignore already copied in my project directory, so that I ignore directories I don't want in git.

It looks something like this:

vendor
tmp
var
.idea
.buildpath
.project
.env
apache-local.conf

At this point, you now have all the files and directories you need to continue with development. You can delete the directory composer made in the container as you no longer need it.

This same idea could of course be used to create the directory and copy the files and directories made by composer to an existing project, just so long as you are aware of any potential naming conflicts, and the possibility you might overwrite any existing files or directories.

-1

For Docker, I delete all docker related files after starting the container, run composer and then put files back to the folder.

-6

Instead of run the command

composer.phar create-project flarum/flarum . --stability=beta

use the specific folder name, for example

composer.phar create-project flarum/flarum /var/www/html/forum/ --stability=beta
1
  • This doesn’t change anything to the problem.
    – bfontaine
    Commented Sep 23, 2022 at 17:24
-9

Folder Must be blank, It's required because when you run create-project, composer aims to create a completely clean brand new project from scratch.

1
  • 3
    I’m adding one more downvote because this doesn’t answer the question: OP is aware that the folder must be empty and is asking how to bypass this limitation.
    – bfontaine
    Commented Sep 23, 2022 at 17:23

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