"sudo composer" command works but "composer" is not? - php

This is going to be easy one I guess. On my OS X - Yosemite I have copied composer.phar to my /usr/bin directory. I have been using it for a while but today I needed to run "composer update". It didnt work of course so I ran "sudo composer update". Then I got the message "command composer not found". Chmm
I copied composer to /usr/local/bin according to the documentation and now "sudo composer" works like charm. BUT when I run "composer" without sudo, it still uses the old one in "/usr/bin" directory. So I deleted it.
Now composer works only with sudo command. I get "Could not open input file: /usr/bin/composer.phar" otherwise. What should I do to point command "composer" to the new location in /usr/local/bin?

Another alternative to get a nice composer command instead of composer.phar:
$ curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin
$ ln -s /usr/local/bin/composer.phar /usr/local/bin/composer

Edit:
/etc/bashrc
Add this to that file:
alias composer="php /usr/local/bin/composer"
run:
source /etc/bashrc
Composer should now run without sudo.

Okay, I encountered issues of having to run composer commands with sudo as well, but in order to get it working without throwing this kind of error (in Ubuntu 15.10):
[ErrorException]
copy(/home/randomuser/.composer/cache/files/barryvdh/laravel-cors/056068736ff8f002514178e1416c7665732eaddc.zip): failed to open stream: Permission denied
What simply solved the issue for me is:
Navigating to my home directory $ cd
Changing the ownership of .composer with: sudo chown -R $USER:$USER .composer/
Then composer install works smoothly.
PS: this might be different for other situation.
Hope this helps :)

i will answer you how i solve it in my Ubuntu 16.10 and you can compare yours
my composer folder set in
/home/abdallah/.composer/
i only give this file the 777 permission so can be reached by any user group
sudo chmod -R 777 /home/abdallah/.composer/
and that is it
i hope this helpful for you

Judging from other answers it seems the solution can vary depending on your system. This is how I fixed the problem on Mac 10.12.
My composer executable in /usr/local/bin/composer had a different group than ~/.composer/ config and cache files.
/usr/local/bin/composer myusr admin
~/.composer/ myusr staff
The primary group for myusr is staff so I changed the group for /usr/local/bin/composer to staff.
/usr/local/bin myusr$ chgrp staff composer
Cache files that had been created when running composer as sudo in the past were still causing problems so I deleted those. Composer cache files are located here: ~/.composer/cache/
If updating hangs during composer update for a project check/empty cache files in the .composer directory for the project.

Related

Composer: file_put_contents(./composer.json): failed to open stream: Permission denied

I'm trying to install Prestissimo to an Ubuntu 16.04 server, but that leads to an error:
$ composer global require "hirak/prestissimo:^0.3"
Changed current directory to /home/kramer65/.composer
[ErrorException]
file_put_contents(./composer.json): failed to open stream: Permission denied
require [--dev] [--prefer-source] [--prefer-dist] [--no-progress] [--no-update] [--no-scripts] [--update-no-dev] [--update-with-dependencies] [--ignore-platform-reqs] [--prefer-stable] [--prefer-lowest] [--sort-packages] [-o|--optimize-autoloader] [-a|--classmap-authoritative] [--] [<packages>]...
I'm logged in as user kramer65, so I wouldn't know why it can't write to my home folder. My normal reaction to a permission denied is to use sudo, but composer then always says:
Do not run Composer as root/super user! See https://getcomposer.org/root for details
Any idea how I can solve this?
I had this problem to install laravel/lumen.
It can be resolved with the following command:
$ sudo chown -R "$(id -un)" "$(composer config --global home)"
What this command does: Execute as privileged user (root) the command to change file owner (not group) to the current user (by name) recursively to the COMPOSER_HOME directory.
As the user-name as well as the composer home directory can be different on each system, the following two commands are in use to obtain the ...
... username: id -un
... composer home directory: composer config --global home
This is an old question, and so a reader of the answer may want to become aware of the following:
Prestissimo was dedicated to be used with Composer 1
As of today, Composer 2 has been released which already didn't benefit from Prestissimo any longer.
But even further as of today, Composer 2.2 has been released which is a Long Term Support (LTS) version you should replace Composer 1 with immediatly if you deemed it to require Presstissimo in the past as it is in general and overall much more faster than Composer 1 ever was - with or without Prestissimo.
How to continue:
Remove Prestissmo from any of your systems.
Replace Composer 1 with Composer 2.2 on any of your systems.
Enjoy.
To resolve this, you should open up a terminal window and type this command:
sudo chown -R user ~/.composer (with user being your current user, in your case, kramer65)
After you have ran this command, you should have permission to run your composer global require command.
You may also need to remove the .composer file from the current directory, to do this open up a terminal window and type this command:
sudo rm -rf .composer
For me, in Ubuntu 18.04. I needed to chown inside ~/.config/composer/
E.g.
sudo chown -R $USER ~/.config/composer
Then global commands work.
In my case I don't have issues with ~/.composer.
So being inside Laravel app root folder, I did sudo chown -R $USER composer.lock and it was helpful.
I faced this issue as well but in my case, I was in wrong directory. Check the directory you are working
In my case, .composer was owned by root, so I did sudo rm -fr .composer and then my global require worked.
Be warned! You don't wanna use that command if you are not sure what you are doing.
In my case all the permissions were correct at all the locations manetioned in other answers here, but I was still getting this error.
Turned out there were some vendor directories that were owned by root. Composer writes composer.lock files all over the place when it's doing an update or install.
So solving my case - and this is specifically for a laravel sail container - all ownerships were switched to user sail in the project:
Enter the sail container as root:
vendor/bin/sail root-shell
Set the file ownership for all files in the project:
chown -R sail:sail /var/www/html
You may just want to do the vendor directory only as a first try:
chown -R sail:sail /var/www/html/vendor
The ownership was wrong after switching from a hand-rolled docker-compose.yaml setup to Laravel Sail, which IMO handles file ownership and permissions in a sensible way, separating root from the application user sail.
This might be super edge case, but if you are using Travis CI and taking advantage of caching, you might want to clear all cache and retry.
Fixed my issue when I was going from sudo to non sudo builds.
I was getting the same exception, but in my case I am using PowerShell to run commands
So, I fixed this with an instruction to unblock multiple files first.
PS C:\> dir C:\executable_file_Path\*PowerShell* | Unblock-File
and then use the following to load the package
& 'C:\path_to_executable\php.exe' "c:\path_to_composer_.phar_file\composer.phar "require desired/package
I was getting the same error when using it with WSL Windows 10. I used the following command to solve it:-
sudo chown -R $USER /home/<username>/.config/composer
I had same issue in windows version of composer that has installed in
C:\composer
When I was trying this command
C:\composer require aws/aws-sdk-php
then simply I got into composer installed folder and try it again
C:\composer>composer require aws/aws-sdk-php
the package installed quickly .
In my case I used sudo mkdir projectFolder to create folder. It was owned by root user and I was logged in using non root user.
So I changed the folder permission using command sudo chown mynonrootuser:mynonrootuser projectFolder and then it worked fine.
I was facing the same issue when I was running the composer require inside /var/www/html ,the default root folder of the apache web server and I was able to solve it by making the current user the owner of this html directory by
sudo chown -R $USER /var/www/html
But you definitely want to set the permissions
chmod 755 -R /var/www/html
There are 2 components to consider.
Composer wants you to run it as the logged in user. However, your webserver wants to have permissions over your application.
The solution is to add your user to the webserver group, then update the permissions of your application.
For Ubuntu running Apache webserver, use the following command to add yourself to the Apache group, replacing <username> with your username
sudo usermod -a -G www-data <username>
Now you need to update your permissions on your application. Navigate to the root folder of your application and use the following command
sudo chown -R $USER:www-data .
Composer now has the necessary permissions to pull in the packages you need and Apache has the necessary permissions to deliver your application.

Composer requires sudo for simple -V command

I'm trying to set up Composer for use with Heroku, but am running into problems. I've followed the download steps here which generates the composer.phar file. I then moved it to /usr/local/bin/composer per the instructions here. Now when running composer from CLI I get the error
Could not open input file: /Users/myusername/.composer/composer.phar
sudo composer -V gets me the version number,but with the warning
Do not run Composer as root/super user! See https://getcomposer.org/root for details
The link seems unrelated to a simple -V command, which I can't imagine I should need to sudo to run anyway. I've done this installation pretty much by the book, but it seems like this isn't right.
I suspect non-root users do not have execute permissions.. From a shell on my system, I get...
ls -al /usr/local/bin/composer
-rwxr-xr-x 1 root user 1201562 Dec 8 2015 /usr/local/bin/composer
eg; Full perms for root, read & execute for everyone else. You may need to ..
sudo chmod 755 /usr/local/bin/composer

Composer global require Laravel/Installer not working on Ubuntu

I'm trying to run composer global require "laravel/installer" on my Ubuntu machine (Trenta OS Distro) and for some reason I can't get it to install.
file_put_contents(./composer.json): failed to open stream: Permission denied
I get the error above on every attempt. I've been unable to find any help on google so I'm guessing this isn't a very common problem. I tried whereis command on that file and it gives me a location
/usr/local/bin/composer
I tried running chmod -R 777 on that file but it did nothing. I don't know what else to do.
Composer was installed on my machine with the following command
curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer
I thought composer.json only comes with projects, but I shouldn't need an existing project in order to simply set up the Laravel Installer right?
I had the same problem and after reading this answer I solved it updating the .composer folder permissions:
sudo chown -R $USER:$USER /home/$USER/.composer
Where $USER is your machine's username.
Update: As #Lucas Bustamante pointed, you don't need to change $USER for your username, its an environment variable already.
As Ian Warner commented out, is not a good idea to run composer as sudo user, because it can lead to security issues.
usr/local/bin/composer composer global require "laravel/installer"
usr/local/bin/composer composer create-project --prefer-dist laravel/laravel myblog
I think this may solve your problem as it did for me.
I made a bash file for the whole process of global installation of Laravel.
download it from here, open a terminal in the bash file directory and run the command below:
bash Laravel_Global_Installer.sh
This bash file manages installations of:
Latest version of PHP and its extensions
Latest version of Composer (Global)
Latest version of Laravel (Global)
This will help you install Composer Globally and without sudo. Therefore you will be able to install Laravel Globally.
first run whereis composer then
navigate to the that directory and run
sudo chown -R $USER:$USER / composer
use sudo before the command,
for e.g.: sudo composer global require "laravel/installer"

How do I move Composer?

I've seen some things similar ( Can't move composer ) to this but I have yet to figure it out how to fix this problem.
I'm installing composer on linux(fedora 22) following this guide: https://ask.fedoraproject.org/en/question/53409/does-fedora-has-composer-in-the-repo/?answer=53445#post-id-53445
The end result I am seeking is using laravel with docker for a group project but so far I'm stuck here with this error with composer.
I enter: sudo mv composer.phar /usr/local/bin/composer in the command line and it kicks back this message: mv: cannot stat ‘composer.phar’: No such file or directory
I'm uncertain of whats happening here, is this expecting that there is already a composer file in /usr/local/bin/ , because there is not. It's obviously not creating one either.
Perhaps this is completely over my head, that said can someone explain what is going on here to me and help me reach a solution for installing composer?
Download the composer.phar manualy.
cd to the directory where you have the downloaded composer.phar and run the below command:
sudo mv composer.phar /usr/local/bin/composer.phar
to move the composer.phar to the bin directory /usr/local/bin. Now restart the terminal and it should work. Note that it is a composer executable for Linux (didn't test it but it is in the docs).
I would adjust the move command above slightly to:
sudo mv composer.phar /usr/local/bin/composer
As per the instructions at https://getcomposer.org/download/
That way, you can actually run composer with just,
php composer
As it's located in your /user/local/bin as just composer. If you move it as composer.phar things get strange.
Note, moving the composer.phar won't relocate the composer.json and composer.lock files that will be the original directory where composer was installed, but the composer command will run fine.

permission denied for composer in /usr/local/bin/

I followed the documentation on https://getcomposer.org/doc/00-intro.md#globally to install composer globally on arch linux. When I do composer self-update, I get this message:
[ErrorException]
rename(/home/hannes/.composer/cache/composer-temp.phar,/usr/local/bin/composer): Permission denied
The permissions in /usr/local/bin/ (I changed them to 777, but it did not help):
-rwxrwxrwx 1 hannes users 1104202 30. Mai 18:07 composer
In my home directory I did this:
sudo chmod -R 777 .composer/
In /etc/php/php.ini, the open_basedir looks so:
open_basedir = /srv/http/:/home/:/tmp/:/usr/share/pear/:/usr/share/webapps/:/usr/local/bin/
I also tried sudo composer self-update but it did not work as well and is possibly not the right way. (?). What else could I try to make this work?
On Ubuntu server >= 16.04
FIRST
sudo rm /usr/local/bin/composer
AND
cd ~/.cache/composer
chmod 755 composer-temp.phar
sudo mv composer-temp.phar /usr/local/bin/composer
this might be the case if you have downloaded composer.phar directly ,
but not by running php composer-setup.php
make composer.phar executable with following command before moving it to /usr/local/bin/composer or after moving
sudo chmod 755 composer.phar
composer-setup.php will make this change for us by default
You should check the permissions of the directory /usr/local/bin/, not just the file within. The process has to write both, the file and into the directory which both must be granted.
Apart from that, a general hint: do not always set everything to 777. There is no reason for that and it makes your system vulnerable.
According to the line you posted in the last comment the directory is currently writeable only for the root user himself. That would explain the error you get. You should not make your own user account the owner, Linux systems are multi user environments. Instead think about one of these approaches:
add the account that is meant to execute the composer to the group root (a user account can belong to several groups) and make the directory group writeable
change the groups ownership of the directory to a group that account is a member of and make the directory group writeable
use the sudo utility to install and update the composer utility
The last option is the typically chosen and preferred one. It leaves permissions as they are (conservative) and only uses raised privileges for system maintenance jobs like installation and upgrade.
Use sudo command for any command which writes to root files of folder. It worked for me.
use sudo "your command"
Even after moving the file via sudo mv composer.phar /usr/local/bin/composer I was getting a permission error when trying to run the composer command. sudo chmod 755 /usr/local/bin/composer fixed things for me.
You could temporarily add the rights to your working user, then update composer w/o errors and then get back the rights.
sudo chmod 777 /usr/bin/
composer self-update
sudo chmod 755 /usr/bin/
Now, there is a package for composer in arch linux which works for me:
sudo pacman -S composer
This did the job for me on Centos 7
chown -R apache:apache path/to/composer
chmod 755 path/to/composer
make sure composer already at /usr/local/bin and then do following things
chmod 755 composer
if permission denied, add sudo
For those who are using shared server here are the steps.
Let's first download composer file.
cd ~
curl -sS https://getcomposer.org/installer | php
Now we can access composer by:
~/composer.phar
Assuming you have /public_html in root directory, here are the steps to use latest composer.
cd public_html
~/composer.phar update
In future, if you want to update composer:
~/composer.phar self-update
Hope this is helpful.
My solution on macOs was the following:
First delete the directory yo created. Somehing like '/usr/local/bin/composer' using this command: rmdir /usr/local/bin/composer.
Now try to install composer again using Homebrew :
Step 1: brew install composer.
Step 2: brew link composer.
Now you can run composer in terminal.
For composer 2
sudo composer self-update --2
For composer 1
sudo composer self-update --1
if permission denied you should use the command with sudo like:
sudo composer self-update

Categories