How to use a specific version of composer? - php

I am using a shared host which comes with an old version of composer. Unfortunately, it's an old version its not able to install something properly. My shared host provider said I can simply install a new version to my shared root directory.
So I did that by adding a php.ini file and then running the following command
php -r "readfile('https://getcomposer.org/installer');" | php -c php.ini
That seemed to work and when I run the command to check the version it only shows me the new version when I run php composer.phar -V. But when I run composer -V, it shows me the old version.
How can I override the main composer for my shared directory? And by the way, I did this in my root directory as I have multiple domains so was hoping I could get this to work in all domains.

I managed to find a solution. I just needed to create an alias and it seems to be working now
echo alias composer="composer.phar" >> .bashrc

Related

How to change PHP version used by composer

I want to use another php version on my machine than the one already installed using WAMP (2 PHP version installed).
The composer installed uses PHP 5.6
A new project requires PHP7.0.
Whenever I choose PHP 7 from the control panel of WAMP and then run php -v it still printing PHP5.6 (CLI)... instead of PHP7.
How can I use PHP7 without reinstalling the composer again?
You can change php version of composer without uninstalling it, follow these steps :
Search for system environment variables in cortana.
Click on the button "Environment variables".
Under "System variables" select path and click on edit, you will see one entry like this "C:\wamp\bin\php\php5.6.13".
Just change this to the folder name of the php located at your wamp/bin/php7.1.9, here php7.1.9 is folder name.
Replace php5.6.13 with bin7.1.9, it will look like these "C:\wamp\bin\php\php7.1.9", just click ok on all the boxes.
You are done.
To verify, first close all the cmd windows, than open cmd and type php -v, press enter and you should see php7.1.9.
If you don't see change in php version than just restart your pc and run php -v again in cmd , it will work.
I'm assuming Windows if you're using WAMP. Composer likely is just using the PHP set in your path: How to access PHP with the Command Line on Windows?
You should be able to change the path to PHP using the same instructions.
Otherwise, composer is just a PHAR file, you can download the PHAR and execute it using any PHP:
C:\full\path\to\php.exe C:\full\path\to\composer.phar install
If anyone is still having trouble, remember you can run composer with any php version that you have installed e.g. $ php7.3 -f /usr/local/bin/composer update
Use which composer command to help locate the composer executable.
I found a very easy way to switch php versions:
Search for system environment variable
Click on "Environment variables"
Under "System variables" select path and click on edit
Move the PHP version folder you want to use before the other one.
So for example: php 7.0 will now be used:
Close all windows by clicking "OK"
Close all the cmd windows, than open cmd and type php -v
You will see the correct php version loaded now:
Another possibility to make composer think you're using the correct version of PHP is to add to the config section of a composer.json file a platform option, like this:
"config": {
"platform": {
"php": "<ver>"
}
},
Where <ver> is the PHP version of your choice.
Snippet from the docs:
Lets you fake platform packages (PHP and extensions) so that you can emulate a production env or define your target platform in the config. Example: {"php": "7.0.3", "ext-something": "4.0.3"}.
This is what happens in my case. I hope this may help to someone have same situation. I'm using macOS Monterey with MAMP.
I linked the php 7.4 using ~/.profile file. So the terminal it says I'm using php 7.4. However, still my composer giving an error saying i'm using php 7.3.
So I check the php path using
which php
This gives me the /usr/local/bin/php as my php cli location. So I remove the file and made a symlink to my php7.4 and now working perfectly.
sudo rm -rf /usr/local/bin/php
sudo ln -s /Applications/MAMP/bin/php/php7.4.21/bin/php /usr/local/bin/php
I found out that composer runs with the php-version /usr/bin/env finds first in $PATH, which is 7.1.33 in my case on MacOs.
So shifting mamp's php to the beginning helped me here.
PHPVER=$(/usr/libexec/PlistBuddy -c "print phpVersion" ~/Library/Preferences/de.appsolute.mamppro.plist)
export PATH=/Applications/MAMP/bin/php/php${PHPVER}/bin:$PATH
Old question I know, but just to add some additional information:
WAMP is used only on Microsoft Windows Operating Systems.
Changing the version of PHP used through the left-click -> PHP -> Version menu changes the version used by Apache to server your site.
Changing the version of PHP used through the right-click -> Tools -> Change PHP CLI Version menu changes the version used by WAMP's PHP CLI.
Note: It is important to understand that the "PHP CLI Version" is used by WAMP's own internal PHP scripts. This "PHP CLI Version" has nothing to do with the version you wish to use for your scripts, Composer or anything else.
For your scripts to work with the version you require, you need to add it's path to the Users Environmental Path. You could add it to the Systems environmental Path but the Users Path is the recommended option.
From WAMP v3.1.2, it would display an error when it detect reference to a PHP path in the System or User Environmental Path. This was to stop confusion such as you were experiencing. Since v3.1.7 the display of this error can now be optionally displayed through a selection in the WampSettings menu.
As indicated in previous answers, adding an installed PHP path (such as "C:\wamp64\bin\php\php7.2.30") to the Users Environmental Path is the correct approach. PS: As the value of the Users Environmental Path is a string, all paths added must be separated with a semi-colon (;)
After experiencing the exact same problem (IE: Choosing which version of PHP I wanted Composer to use), I created a script which could easily and rapidly switch between PHP CLI Versions depending on what project I was working on.
The Windows batch script "WampServer-PHP-CLI-Version-Changer" can be found at https://github.com/custom-dev-tools/WampServer-PHP-CLI-Version-Changer
I hope this helps others.
Good luck.
After a long search on the internet and finding many unrelated answers / ones that did not work for me, Here is what worked for me.
Those who are in shared hosting know that bin directory is write-protected and running sudo commands or any system-wide command is not allowed.
There's two ways of solving this:
Run the command directly on your project folder selecting the appropriate PHP version you need.
ea-php80 /opt/cpanel/composer/bin/composer update
To get available PHP on your server type ea-php and hit TAB to see a list.
make an alias to composer
Run this command to edit/make this file nano ~/.bashrc
Inside that file, put alias composer="ea-php80 /opt/cpanel/composer/bin/composer"
This gives you the flexibility to run composer commands as usual without those long trailing strings
If you are using Windows, all you have to do is change the path to php.exe in the composer.bat file located in: "C:\ProgramData\ComposerSetup\bin".
In my case I include paths to all php versions, whenever I need to run a project on a specific php version, I just move the required path to the top (using these buttons in the right) and then close all the terminals and restart my wampp server.
The path with listing in the will be selected as your php version by windows
This is the simplest solution I think.
If you still facing the problem after changing Environment variables in windows, try to delete directory or just rename directory of your old php.
I've done it and it's work.
I will assume that you need this because a requirement to have multiple php versions installed to handle multiple projects.
If this is the case a prefer to run directly the php desired bin pointing to the executable script of composer, for example, in my case I have php 8.1 and 7.4, my main php version configured for CLI is 8.1, but I want to run composer with 7.4 in some projects, so I run this command:
php7.4 -f /usr/local/bin/composer install
Where php7.4 is the bin installed and my global composer script is in /usr/local/bin/composer
From there, you can make an alias like this to facilitate things: alias composer7.4='php7.4 -f /usr/local/bin/composer ' so next time you need to run composer with php#7.4 you only need to run: composer7.4 install
Came here by the title, but the question specifies WAMP; which this may not easily apply to. So, in my case - using a Mac.. so more like a MAMP - if you have brew and the below versions installed, this could help - and composer picks it up.
brew link --overwrite --force php#8.1
php -v
#PHP 8.1...
brew link --overwrite --force php#7.4
php -v
#PHP 7.4...

Change Composer PHP Version Path Plesk

RESOLVED :
For plesk users you need to make nano .bash_profile when you are in the file add this line :
PATH=/opt/plesk/php/7.1/bin:$PATH
it will work for ssh users !!
for root ssh user you can do :
export PATH=/opt/plesk/php/7.1/bin:$PATH;
Hope it will help
I try to say to composer that now he needs to look on the php version 7.1 wish is installed on my plesk serveur.
how i could do this ? i am not a pro in commands line.. i think i need to add this line
export PATH=/opt/plesk/php/7.1/bin:$PATH; in my .bashrc file but i really don't know how to access to this and how to add the line.
hope someone could help me for this, thanks a lot in advance.
Actually i need to do manualy this to update composer :
export PATH=/opt/plesk/php/7.1/bin:$PATH; php composer.phar update
I had the same problem with both Composer and WP CLI in a Plesk Onyx environment
Problem 1
- This package requires php ^7.2 but your PHP version (5.4.16) does not satisfy that requirement.
And something similar when trying to run WP CLI
php -v
PHP 7.2.23 (cli) (built: Sep 27 2019 11:40:34) ( NTS )
Solved it with a symlink like so,
after loggin in on the right domain with ssh:
cd /usr/bin
mv php php_OLD
ln -s /opt/plesk/php/7.2/bin/php php
and after testing cleaning up:
rm php_OLD
I discoverd only very slowly that the php binary which both applications use is in /usr/bin
So what the code does is:
go to that directory
rename php for backup purpose
create a symlink to the correct path of the right plesk php binary
worked instantly
hope this helps someone to solve this puzzle quicker than I did...
A slightly alternative version if it may help anyone:
Create an alias for composer and store it in /etc/profile.d (so it is available to all users)
cd /etc/profile.d
nano composer7.sh
In your file put this (adjusting for version & composer location):
alias composer7='/opt/plesk/php/7.0/bin/php /usr/local/bin/composer.phar'
Save the file and login again.
Your script can now be used from command line even if your installation of Plesk is running a different version of php. Now you can execute it like:
composer7 update

Laravel - Installing packages that were installed with composer on live server

I have a laravel project on namecheap shared server, I have set up everything and it is working there. But after that, I have added locally on my machine to the project imagecache intervention package with composer. I don't know how to install it on the server though, I have tried with running composer commands through ssh:
php composer require intervention/imagecache
But I got an error:
Could not open input file: composer
Since, their support told me that they actually have composer uploaded in the folder public_html and not where my project is and that is public_html/myProject if that is the problem. Since I have no idea, how the composer is installed and setup there, I wonder how can I install this package. I have also tried by just uploading the whole directory of the package from my local vendor folder in the project to the production server, but that didn't work either, images were still not visible when I was using imagecache route. How can I fix that?
Run this: php -c php.ini composer.phar install
First You have to create php.ini file with this info.
max_execution_time = 300
max_input_time = 300
memory_limit = 512M
suhosin.executor.include.whitelist = phar
detect_unicode = Off
If you have ssh then you can create a folder where ever you wan't.
It is better if you put it in the same level that your public folder and then install composer inside this folder:
with curl
curl -sS https://getcomposer.org/installer | php
if you don't have curl or the server has restrictions you can use:
php -r "readfile('https://getcomposer.org/installer');" | php
or you can "Manual Download" the composer.phar and upload it trough ftp
https://getcomposer.org/download/
Then you need to give execution rights to the composer.phar file and you
chmod a+x composer.phar
can also create an alias to run composer from your public folder
alias composer="path/to/composer/composer.phar"
This alias will work until you close your console. In some shared servers you will see a file call: .bashrc where you can put the alias

Blank page with php artisan serve for laravel 5 in mamp php 5.4.10

I am trying to install Laravel for first time. I installed it on my desktop with the following composer command
composer create-project laravel/laravel laravel-app
The command completed successfully without any error but with some messages like
symfony/var-dumper suggests installing ext-symfony_debug ()
symfony/translation suggests installing symfony/config ()
I now did chmod -R 777 larvel-app to make whole folder writable by everyone. I now started php server with following command
cd laravel-app
php artisan serve
and it gave following message
Laravel development server started on http://localhost:8000
When viewed from web browser in address http://localhost:8000, there is nothing, only blank page. When I browse this address, the server seems to quit.
I have following environment:
OSX Mavericks;
MAMP with php 5.4.10 and apache2;
following extensions in php.ini file
extension=imap.so
extension=yaz.so
extension=mcrypt.so
extension=gettext.so
extension=pgsql.so
extension=pdo_pgsql.so
extension=pdo_mysql.so
;;;;;Following are added by me ;;;;no error when starting server
extension=openssl.so
extension=mbstring.so
When I run which php from command line, I get following.
/Applications/MAMP/bin/php/php5.4.10/bin/php
When I tried to run this in mamp ie localhost:8888, it gave 500 internal server error.
Please suggest me, where I might have made mistake.
As a OSX user i strongly recommend you to use Laravel Homestead to develop your applications.
http://laravel.com/docs/5.0/homestead
You only need to install:
Virtualbox
Vagrant
I have my projects in ~/code folder, so everytime i create a new project, i just:
Add project test domain to my local host file (my-project.app pointing to localhost 127.0.0.1)
ssh into the homestead virtualmachine (homestead ssh)
run built in serve command (serve my-project.app /path/to/project/public)
VoilĂ 
The most common reason for this is that the web server process does not have write access to the storage folder and its subfolders.
Set the permissions on the storage folder so that the web server can create files.
You don't have to use php serve, mamp does that for you. Make sure you point your directory to public folder. You can also use custom domain for each of your projects.
Also for Mac there is laravel valet. Easy to get started with many php projects.
I think you should move your Laravel Application to
/Applications/MAMP/htdocs/
and then you can access it on
http://localhost:8888/laravel-app

composer and php installation conflict for laravel 4 artisan

hi i start working Laravel 4, i have finished the installation process with composer, but when try to use artisan it always give a message
Laravel requires the Mcrypt PHP extension
i have checked with php_info() it shows me Mcrypt PHP extension is enabled, after searching on web i have found some solutions, if i apply those then composer stops working viceversa . i am using XAMPP for development if i use which php command in terminal it show me path /usr/bin/php but my development php is in /Applications/XAMPP/xamppfiles/bin, i have extend the $PATH using .bash_profile
export XAMPP_PHP=/Applications/XAMPP/xamppfiles/bin
export PATH="$XAMPP_PHP:$PATH"
after this artisan start work great but composer stops its working if i type composer command in terminal it show ???? output, now i want composer, php and artisan work perfect just looking for good settings
I solved this problem with a workaround. Instead of changing the $PATH variable, on /usr/bin/ I created a symbolic link to XAMPP's PHP:
cd /usr/bin/
ln -s /Applications/XAMPP/xamppfiles/bin/php phpx
'phpx' is the name I gave to run php on XAMPP (pick the name you prefer - obviously it needs to be different than 'php'). This way I can run artisan using 'phpx' (XAMPP version) which has mcrypt. For example:
phpx artisan key:generate
And if you need to run composer, you run php (mac version):
php composer.phar install
It's a workaround, I'm not proud of it, but it's working.
The best solution would be to install mcrypt on your mac (use this tutorial), mysql and use mac's php. But as a lot of developers (including me) still rely on XAMPP, this workaround may be enough for testing.
If it outputs ???? it most likely means you have detect_unicode = On in your php.ini. This must be off because it breaks phar files.

Categories