Can't execute composer.phar in shared hosting - composer-php

I've installed Composer on a shared-host (hostgator) following this guide:
https://laravel.io/forum/02-13-2014-how-to-install-laravel-on-a-hostgator-shared-server
It was installed using this command:
curl -sS https://getcomposer.org/installer | php
When I try to run composer I get the message "/home2/georger/composer.phar: no such file or directory" although the file does exist, I can open it. Other questions that deal with this suggest moving it to /usr/local/bin but that's not an option for me.

As outlined in the installation instructions, you will need to pass it to the php interpreter:
$ php composer.phar
Alternatively, you might want to reconsider whether you really want to execute composer in a production environment or rather execute it during the deployment and then deploy the artifacts. In other words, you probably want to run composer install on the machine from which you want to deploy to the production system, and then synchronize files from there to the production system, for example, using rsync.
For reference, see https://getcomposer.org/doc/00-intro.md.

Related

Why is my composer command not working in subdirectories on a 1and1 webhosting server?

I'm in the process of building a TYPO3 website on a webserver hosted by 1and1 ionos. I need to install TYPO3 via composer, so I installed composer to this webserver first. It seemed to work fine and I was able to do a composer reuqire to install TYPO3 to a new directory (named: typo3-composer) within the root of the server.
But for some reason I have two problems now:
Within the root directory of my server I cannot run the "composer xx" command. It only works using the prefix "php composer xx"
In my subdirectory typo3-composer (the one I installed TYPO3 into) I cannot run ANY composer command. Terminal simply says command composer not found.
What I did so far:
1. Install composer
curl -sS https://getcomposer.org/installer | /usr/bin/php8.0-cli
2.Start composer
/usr/bin/php8.0-cli composer.phar
3.Rename file
mv composer.phar composer
4.Tried to move the file to any directory (usr/local/bin) of the $PATHvariable to make composer globally accessible. This failed because all of those directories are read-only. So i made the file executable
chmod +x composer
5.And added the current path to the system's $PATH variable
export PATH=$PATH:$PWD
I'm pretty sure this is a very basic logial mistake. Where do I have to execute the composer install command and where do I have to put the composer file so that I can use the command from every directory?
(PS: I did set the PHP Version of the Shell to php 8.0 already so I don't have to use /usr/bin/php8.0-cli all the time)
Thanks for your well formulated question which I'd like to answer the following:
If you would like to (really) execute Composer on the remote host1, you already have the answer albeit you might not be aware of it. To quote from your very own question:
It only works using the prefix "php composer xx"
This is fine and how PHP works on the command-line. However as you have noticed it depends on the directory you're in (the working direcory). Not for the command (php) but for the file you command php(1) to execute: "composer".
Some more context if it helps: The first operand of the php command you entered, "composer", is the path to the file you want php(1) to execute.
So when:
user#host:~$ php composer info
...
works and you then change the directory and it doesn't any longer:
user#host:~$ cd typo3-composer
user#host:~/typo3-composer$ php composer info
Could not open input file: composer
user#host:~/typo3-composer$ echo $?
1
then replacing composer with the actual path of it should work:
user#host:~/typo3-composer$ php ../composer info
...
before actually running php(1), you can also make use of the file(1) command to check first if a file exists and of what type it is:
Not existing file:
user#host:~/typo3-composer$ file composer
composer: cannot open `composer' (No such file or directory)
Existing "composer" file (with executable bits set):
user#host:~/typo3-composer$ file ../composer
../composer: a /usr/bin/env php script executable (binary data)
Composer phar (even with no executable bits):
user#host:~/typo3-composer$ file ../composer
../composer: a /usr/bin/env php script executable (binary data)
Composer is just a PHP file (sort-of, it is a PHP archive "phar") and the idiom to execute it under the name composer is not really necessary. The path (absolute or relative to your currend working directory "PWD"2) suffices. If you take a look in the Composer documentation for example, you find the following idiom:
php composer.phar <options>|<operands>...
TLDR: Composer works regardless how you invoke it, be it composer, php composer.phar or similar. The php(1) command only needs a pathname of the PHP file to execute, be it composer.phar or one of the typo3 command-line scripts (to give a different example).
As user Nico Haase already has pointed out, there is no inherent requirement to execute composer(1) on the remote host.
Composer interacts with the wide-area-network (WAN, also: WWW, internet) to download files and applies the result to the working directory (project directory), more specifically the vendor-dir (vendor directory, "vendor" relative to the composer.json configuration file by default).
You normally then afterwards deploy that (local) project to the remote host. There is no requirement at all to do the dependency resolution on the remote host, quite the opposite: bring the work near to yourself so that you can much faster deal with any problems that may arise and make the deployment step dead simple (e.g. a single, simple transaction). Remote machines go offline, network connection may get lost in the middle and you may also need to (or want to) fully replace the remote host occasionally. All you should need to do is to re-deploy the project, not configuring remote systems and run commands there interactively. Life is too short for that.
Now the Typo3 project might have thought otherwise (which would not be very user-friendly if you allow me the comment, but I know the typo3 folks are user-friendly for real) then you may go through additional hurdles, however those are less related to Composer (and how you invoke it) but more to Typo3 (then, if at all). But, as you don't present Typo3 as an issue in your question, I'm pretty sure you can just do the "Composer work" locally in your own shell which has the benefit that you can configure Composer and how you invoke it to your liking without the requirement to do this only short and half at best on every remote host you may come in contact with.
PWD
This variable shall represent an absolute pathname of the current working directory. It shall not contain any components that are dot or dot-dot. The value is set by the cd utility, and by the sh utility during initialization. [from]

Laravel package installation on live server

How to install package in laravel on live server, whenever I run "command composer require monolog/monolog". It gives error Could not open input file: /home/root/composer.phar
First of all, you should not run composer require on live server. You should only run composer install so you should require it locally, test if everything is working fine and then on live server just install library that is tested in your application.
About error:
Could not open input file: /home/root/composer.phar
make sure this file really exists (if not you can download it from Composer site) and make sure it has correct permissions (it should have execute permission) - use chmod +x composer.phar to make it executable
Please try like this.
$ curl -sS https://getcomposer.org/installer | php
$ mv composer.phar /usr/local/bin/composer
$ php composer.phar update monolog/monolog
You can install package without any problem.

How to cache package manager downloads for docker builds?

If I run composer install from my host, I hit my local composer cache:
- Installing deft/iso3166-utility (1.0.0)
Loading from cache
Yet when building a container having in its Dockerfile:
RUN composer install -n -o --no-dev
I download all the things, e.g.:
- Installing deft/iso3166-utility (1.0.0)
Downloading: 100%
It's expected, yet I like to avoid it. As even on a rebuilt, it would also download everything again.
I would like to have a universal cache for composer that I could also reshare for other docker projects.
I looked into this and found the approach to define a volume in the Dockerfile:
ENV COMPOSER_HOME=/var/composer
VOLUME /var/composer
I added that to my Dockerfile, and expected to only download the files once, and hit the cache afterwards.
Yet when I modify my composer, e.g. remove the -o flag, and rerun docker build ., I expected to hit the cache on build, yet I still download the vendors again.
How are volumes supposed to work to have a data cache inside a docker container?
Use the experimental feature : Docker buildkit (Supported Since docker 18.09, docker-compose 1.25.4)
In your dockerfile
# syntax=docker/dockerfile:experimental
FROM ....
# ......
RUN --mount=type=cache,target=/var/composer composer install -n -o --no-dev
Now before building, make sure the env var is exported:
export DOCKER_BUILDKIT=1
docker build ....
If you are using docker-compose, make sure to export also COMPOSE_DOCKER_CLI_BUILD :
export COMPOSE_DOCKER_CLI_BUILD=1 DOCKER_BUILDKIT=1
docker-compose build ...
If it does not work with docker-compose, make sure your docker-compose version is above 1.25.4
docker-compose version
I found two ways of dealing with this problem, yet none deal with composer volumes anymore.
Fasten composer download process: Use hirak/prestissimo
composer global require "hirak/prestissimo:^0.3"
💡 With Composer 2.0, the above step is no longer required for faster downloads. In fact, it won't install on Composer 2.0 environments.
Force docker to use a cached composer install. Docker uses a cache on a RUN if the added files didn't change. If you only do COPY . /your-php-app, docker build will refresh all the cashes and re-run composer install even if only one unrelated file in the source tree changed. In order to make docker build to run composer install only install on package changes, one has to add composer.json and composer.lock file before adding the source files. Since one also needs the source files anyway, one has to use different folders for composer install and rsync the content back to the then added folder; furthermore one then has to run the post-install scripts manually. It should look something like this (untested):
WORKDIR /tmp/
COPY composer.json composer.lock ./
RUN composer install -n -o --no-dev --no-scripts
WORKDIR /your-php-app/
COPY . /your-php-app/
RUN rsync -ah /tmp/* /your/php-app/
RUN composer run-script post-install-cmd
or combine the two =)
I would like to have a universal cache for composer that I could also reshare for other docker projects.
Using a shared volume for the Composer cache works great when working with containers. If you want to go broader than just containers, and use a shared cache for e.g. local development as well, I've developed a solution for that called Velocita - how it works.
Basically, you use one global Composer plugin for local projects and inside and build containers. This not only speeds up downloads tremendously, it also helps with 3rd party outage for example.
I would consider utilizing the $HOME/.composer/cache/files directory. This is where composer reads/write to when using composer install.
If you are able to mount it from your host to your container that would work. Also you could just tar it up after each time your run composer install and then drop that in before you run composer install the next time.
This is loosely how Travis CI recommends doing this.
Also, consider using the --prefer-dist flag with your composer install command.
Info on that can be found here: https://getcomposer.org/doc/03-cli.md#install
--prefer-dist: Reverse of --prefer-source, composer will install from dist if possible. This can speed up installs substantially on build servers and other use cases where you typically do not run updates of the vendors. It is also a way to circumvent problems with git if you do not have a proper setup.
Some references on utilizing the composer cache for you:
https://blog.wyrihaximus.net/2015/07/composer-cache-on-travis/
https://github.com/travis-ci/travis-ci/issues/4579

what is phpmd and how to use it?

I have Ubuntu 14.04 + Sublime text 3 and installed phpcs packages
additionalty I have installed phpcs and php-cs-fixer on my system
From this blog
I have found that phpmd (PHP Mess Detector) is also a required library, so installed phpmd as per given instructions on official php md page using alternative method From the github repository everything was finished.
:~/phpmd$ curl -s http://getcomposer.org/installer | php
#!/usr/bin/env php
All settings correct for using Composer
Downloading...
Composer successfully installed to: /home/keshav/phpmd/composer.phar
Use it: php composer.phar
but now when I write on terminal
phpmd /opt/lampp/htdocs/myproject, myfile.php
phpmd: command not found
There is phpmd folder on Home directory and everything without any error.
I have local project on core PHP create composer.json in project folder as per suggested on github .
Please tell me what means by
Then install Composer in your project (or download the composer.phar directly):
I think the problem is you've installed phpmd in a local directory, but you're trying to use it as if it was installed globally.
Installation instruction on the referenced sites can't really be made any clearer. Since you've already installed phpcs and php-cs-fixer, and those work for you, just follow similar instructions for phpmd. These are all PHP projects and are installed in a similar way.
Anyway, to use phpmd as a global command you have several options.
Github
Clone the github repository just like you did and add the phpmd bin directory to your PATH variable.
Global composer installation
Use the composer global command to install phpmd globally. You will also need to make sure that composer's bin directory is in the PATH. By default it's ~/.composer/vendor/bin.
composer global require phpmd/phpmd
This command will install phpmd globally, and as soon as ~/.composer/vendor/bin is in your PATH you'll be able to call it by simply invoking phpmd.
It's very well explained in composer's documentation: https://getcomposer.org/doc/03-cli.md#global
Download the phar archive
This is the simplest thing you can do. Simply go the phpmd releases, choose the latest and download the phar archive.
Put the phar file to whatever place you'd like. Just remember that it needs to be in your PATH. You can also rename it, to skip the .phar extension.
For example:
wget http://static.phpmd.org/php/2.1.3/phpmd.phar
sudo mv phpmd.phar /usr/bin/phpmd
sudo chmod +x /usr/bin/phpmd
Docker container
First, fetch the docker image with static analysis tools for PHP:
docker pull jakzal/phpqa
One of the tools provided by the image is phpmd. The command below will run phpmd in a docker container and mount the current working directory as a /project.
docker run -it --rm -v $(pwd):/project -w /project jakzal/phpqa \
phpmd src text cleancode,codesize,controversial,design,naming,unusedcode
When you use the composer-based install, it gets installed into the ./bin directory within the ./vendors directory. So for me, relative to my project's root directory, it was here:
./vendor/bin/phpmd
And I was able to run it from my project's root by running ./vendor/bin/phpmd . text codesize. (I'm not getting any useful output yet, but another issue)

Cant' get MongoDB PHP Extension to work

I've been following this tutorial to try to install the PHP MongoDB extension but unfortunately haven't been able to get it working.
http://www.marcwitteveen.com/tech/mac/installing-mongodb-on-a-mac-so-you-can-access-it-with-php/
I can access the collections in the terminal, the database is properly installed but I didn't manage to get the extension working.
I have the following problem:
Sinan-MBP:mongo-php-driver-master Sinan$ cd
Sinan-MBP:~ Sinan$ sudo pecl install mongo
pecl/mongo is already installed and is the same as the released version 1.4.1
install failed
I've been trying several times so that's probably why I get this message. However, even after adding "extension=mongo.so" to the php.ini, I can't use MongoDB in my PHP code.
Thank you for your help.
This is likely a case of modifying the wrong php.ini for the environment, or PHP not being able to find the mongo.so in its directory. If you have separate php.ini files for CLI and web environments, PECL might have installed the compiled extension to a different directory.
If you run through the installation docs, there are instructions for grepping extension_dir from php -i and ensuring that mongo.so is present in that directory. If your web environment has a separate php.ini file, then grepping output from php -i likely won't help. You may need to view phpinfo(); output through your web server and check for the extension directory there.
One additional test that PECL installation succeeded would be attempting to view reflection information for one of the driver classes from the CLI environment. You can do so with php --rc MongoClient, which should print info on the methods of the MongoClient class.
I don't know why it was not working but I found a workaround late last night.
I uninstalled the php extension and then installed it again and made sure the extension was loaded in the php.ini
sudo pecl uninstall mongo
sudo pecl install mongo

Resources