# Composerのインストール・使い方

**URL:** <https://forum.ficusonline.com/t/topic/216>\
**Category:** WEB Design\
**Created:** [2018 年 7 月 7 日午前 6:59 UTC](https://forum.ficusonline.com/t/topic/216 "2018-07-07T06:59:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tk-fuse](https://forum.ficusonline.com/user_avatar/forum.ficusonline.com/tk-fuse/32/255_2.png) [@tk-fuse](https://forum.ficusonline.com/u/tk-fuse)\
**Post date:** [2018 年 7 月 7 日午前 6:59 UTC](https://forum.ficusonline.com/t/topic/216/1 "2018-07-07T06:59:23Z")

</div>

> **[GitHub - composer/docker: Composer in Docker](https://github.com/composer/docker)**
>
> Composer in Docker

> **[Composer](https://getcomposer.org/)**
>
> A Dependency Manager for PHP

ホームフォルダ内で以下のシェルスクリプトを実行。シェルスクリプトには実行権を付与すること。

> $ sudo chmod +x composer\_install.sh

スクリプトの内容は下記サイトにて確認のこと。

> **[How do I install Composer programmatically? - Composer](https://getcomposer.org/doc/faqs/how-to-install-composer-programmatically.md)**
>
> A Dependency Manager for PHP

```auto
#!/bin/sh

EXPECTED_CHECKSUM="$(php -r 'copy("https://composer.github.io/installer.sig", "php://stdout");')"
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
ACTUAL_CHECKSUM="$(php -r "echo hash_file('sha384', 'composer-setup.php');")"

if ["$EXPECTED_CHECKSUM" != "$ACTUAL_CHECKSUM"]
then
    >&2 echo 'ERROR: Invalid installer checksum'
    rm composer-setup.php
    exit 1
fi

php composer-setup.php --quiet
RESULT=$?
rm composer-setup.php
exit $RESULT

```

スクリプトの実行

> $ sudo ./composer\_install.sh

composer.pharというファイルが作成されます。composerの実行は、

> $ php composer.phar

グローバル環境で実行させるため、/user/local/binにファイル名composerで移動します。

> $ sudo mv composer.phar /usr/local/bin/composer

環境変数を確認して実行パスに/usr/local/binが含まれているか確認します。

> $ echo $PATH

bashの設定を再読込します。

> $ source ~/.profile

任意のディレクトリで"composer"が実行できることを確認して下さい。

> $ composer

---

<div class="post-metadata">

**Author:** ![tk-fuse](https://forum.ficusonline.com/user_avatar/forum.ficusonline.com/tk-fuse/32/255_2.png) [@tk-fuse](https://forum.ficusonline.com/u/tk-fuse)\
**Post date:** [2019 年 5 月 26 日午後 1:12 UTC](https://forum.ficusonline.com/t/topic/216/2 "2019-05-26T13:12:36Z")

</div>

### Composerインストールとその使い方

Ubuntu 20.04へのインストール

> **[How To Install and Use Composer on Ubuntu | DigitalOcean](https://www.digitalocean.com/community/tutorials/how-to-install-and-use-composer-on-ubuntu-20-04)**
>
> Install Composer on Ubuntu with our step-by-step guide. Learn global vs local installation, verify installer security, and manage PHP dependencies efficientl…

### [Packagist](https://packagist.org/) _The PHP Package Repository_

> **[pimcore/pimcore - Packagist.org](https://packagist.org/packages/pimcore/pimcore#v6.9.6)**
>
> Content & Product Management Framework (CMS/PIM/E-Commerce)

### composer.json バージョン記述ルール

> **[Versions and constraints - Composer](https://getcomposer.org/doc/articles/versions.md)**
>
> A Dependency Manager for PHP

```auto
"require": {
    "vendor/package": "1.3.2", // exactly 1.3.2

    // >, <, >=, <= | specify upper / lower bounds
    "vendor/package": ">=1.3.2", // anything above or equal to 1.3.2
    "vendor/package": "<1.3.2", // anything below 1.3.2

    // * | wildcard
    "vendor/package": "1.3.*", // >=1.3.0 <1.4.0

    // ~ | allows last digit specified to go up
    "vendor/package": "~1.3.2", // >=1.3.2 <1.4.0
    "vendor/package": "~1.3", // >=1.3.0 <2.0.0

    // ^ | doesn't allow breaking changes (major version fixed - following semver)
    "vendor/package": "^1.3.2", // >=1.3.2 <2.0.0
    "vendor/package": "^0.3.2", // >=0.3.2 <0.4.0 // except if major version is 0
}

```

### Semantic Versioning

> **[Semantic Versioning 2.0.0](https://semver.org/)**
>
> Semantic Versioning spec and website

---

<div class="post-metadata">

**Author:** ![tk-fuse](https://forum.ficusonline.com/user_avatar/forum.ficusonline.com/tk-fuse/32/255_2.png) [@tk-fuse](https://forum.ficusonline.com/u/tk-fuse)\
**Post date:** [2021 年 10 月 13 日午前 10:09 UTC](https://forum.ficusonline.com/t/topic/216/3 "2021-10-13T10:09:24Z")

</div>

## Operation timed out (IPv6 issues)

> **[Operation timed out (IPv6 issues) - Troubleshooting - Composer](https://getcomposer.org/doc/articles/troubleshooting.md#operation-timed-out-ipv6-issues-)**
>
> This is a list of common pitfalls on using Composer, and how to avoid them. | A Dependency Manager for PHP

You may run into errors if IPv6 is not configured correctly. A common error is:

```auto
The "https://getcomposer.org/version" file could not be downloaded: failed to
open stream: Operation timed out

```

We recommend you fix your IPv6 setup. If that is not possible, you can try the following workarounds:

**Workaround Linux:**

On linux, it seems that running this command helps to make ipv4 traffic have a higher prio than ipv6, which is a better alternative than disabling ipv6 entirely:

```auto
sudo sh -c "echo 'precedence ::ffff:0:0/96 100' >> /etc/gai.conf"

```

---

<div class="post-metadata">

**Author:** ![tk-fuse](https://forum.ficusonline.com/user_avatar/forum.ficusonline.com/tk-fuse/32/255_2.png) [@tk-fuse](https://forum.ficusonline.com/u/tk-fuse)\
**Post date:** [2022 年 3 月 15 日午前 7:04 UTC](https://forum.ficusonline.com/t/topic/216/4 "2022-03-15T07:04:07Z")

</div>

### dockerファイルにcomposerを追加する場合

**`composer_installer.sh`**  
2022/07/23 : スクリプト内容更新

```auto
#!/bin/sh

EXPECTED_CHECKSUM="$(php -r 'copy("https://composer.github.io/installer.sig", "php://stdout");')"
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
ACTUAL_CHECKSUM="$(php -r "echo hash_file('sha384', 'composer-setup.php');")"

if ["$EXPECTED_CHECKSUM" != "$ACTUAL_CHECKSUM"]
then
    >&2 echo 'ERROR: Invalid installer checksum'
    rm composer-setup.php
    exit 1
fi

php composer-setup.php --quiet
RESULT=$?
rm composer-setup.php
exit $RESULT

```

**Prestashop** の **Docker Image** に **`Composer`** 、エディターの **`nano`** を追加

**`dockerfile`**

```auto
FROM prestashop/prestashop:1.7-fpm

# Set working directory
WORKDIR /var/www/html

RUN apt-get update && apt-get install -y nano && rm -rf /var/lib/apt/lists/*

# Installing composer
COPY composer_installer.sh ./
RUN chmod +x composer_installer.sh
RUN echo 'precedence ::ffff:0:0/96 100' >> /etc/gai.conf \
    && ./composer_installer.sh && mv composer.phar /usr/local/bin/composer

```

---

<div class="post-metadata">

**Author:** ![tk-fuse](https://forum.ficusonline.com/user_avatar/forum.ficusonline.com/tk-fuse/32/255_2.png) [@tk-fuse](https://forum.ficusonline.com/u/tk-fuse)\
**Post date:** [2023 年 4 月 2 日午前 1:05 UTC](https://forum.ficusonline.com/t/topic/216/5 "2023-04-02T01:05:17Z")

</div>

## DockerHub Composer

**`DockerHub Composer`**

> **[composer - Official Image | Docker Hub](https://hub.docker.com/_/composer)**
>
> Composer is a dependency manager written in and for PHP.

`Docker 17.05` 以降では、 **`Dockerfile`** 内で他のイメージを指定して組み入れることが可能。

**`Dockerfile`**

```auto
COPY --from=composer /usr/bin/composer /usr/bin/composer

```

> **[Multi-stage](https://docs.docker.com/build/building/multi-stage/)**
>
> Learn about multi-stage builds and how you can use them to improve your builds and get smaller images

---

<div class="post-metadata">

**Author:** ![tk-fuse](https://forum.ficusonline.com/user_avatar/forum.ficusonline.com/tk-fuse/32/255_2.png) [@tk-fuse](https://forum.ficusonline.com/u/tk-fuse)\
**Post date:** [2023 年 4 月 23 日午前 5:07 UTC](https://forum.ficusonline.com/t/topic/216/6 "2023-04-23T05:07:47Z")

</div>

## composer remove [package]

> <https://stackoverflow.com/questions/23126562/how-can-i-remove-a-package-from-laravel-using-php-composer>

```auto
# composer remove vendor/package

```

## remove / rm

[Command-line interface / Commands - Composer](https://getcomposer.org/doc/03-cli.md#remove-rm)

The `remove` command removes packages from the `composer.json` file from the current directory.

```auto
# php composer.phar remove vendor/package vendor/package2

```

Note) `php composer.phar = composer`

After removing the requirements, the modified requirements will be uninstalled.

### Options

- **–unused** Remove unused packages that are not a direct or indirect dependency (anymore)
- **–dev:** Remove packages from `require-dev`.
- **–dry-run:** Simulate the command without actually doing anything.
- **–no-progress:** Removes the progress display that can mess with some terminals or scripts which don’t handle backspace characters.
- **–no-update:** Disables the automatic update of the dependencies (implies --no-install).
- **–no-install:** Does not run the install step after updating the composer.lock file.
- **–no-audit:** Does not run the audit steps after installation is complete. Also see [COMPOSER\_NO\_AUDIT](https://getcomposer.org/doc/03-cli.md#composer-no-audit).
- **–audit-format:** Audit output format. Must be “table”, “plain”, “json”, or “summary” (default).
- **–update-no-dev:** Run the dependency update with the --no-dev option. Also see [COMPOSER\_NO\_DEV](https://getcomposer.org/doc/03-cli.md#composer-no-dev).
- **–update-with-dependencies (-w):** Also update dependencies of the removed packages. (Deprecated, is now default behavior)
- **–update-with-all-dependencies (-W):** Allows all inherited dependencies to be updated, including those that are root requirements.
- **–ignore-platform-reqs:** ignore all platform requirements (`php`, `hhvm`, `lib-*` and `ext-*`) and force the installation even if the local machine does not fulfill these. See also the [`platform`](https://getcomposer.org/doc/06-config.md#platform) config option.
- **–ignore-platform-req:** ignore a specific platform requirement(`php`, `hhvm`, `lib-*` and `ext-*`) and force the installation even if the local machine does not fulfill it. Multiple requirements can be ignored via wildcard.
- **–optimize-autoloader (-o):** Convert PSR-0/4 autoloading to classmap to get a faster autoloader. This is recommended especially for production, but can take a bit of time to run so it is currently not done by default.
- **–classmap-authoritative (-a):** Autoload classes from the classmap only. Implicitly enables `--optimize-autoloader`.
- **–apcu-autoloader:** Use APCu to cache found/not-found classes.
- **–apcu-autoloader-prefix:** Use a custom prefix for the APCu autoloader cache. Implicitly enables `--apcu-autoloader`.
