Cannot install "global" composer requirements because "composer.json is not writeable" - php

I want to install Laravel but again and again I got the below error,
rukon#rukon-HP-Notebook:~$ composer global require laravel/installer
Changed current directory to /home/rukon/.config/composer
./composer.json is not writable. rukon#rukon-HP-Notebook:~$
I tried to make writable composer.json by
$chmod 777 ./composer.json
but it's not working. my file owner is not root

When you run any composer global command, the composer.json used is the one at your HOME directory, not at your projects directory. E.g. at ~/.composer/composer.json.
Looks like your home directory composer is owned by root.
At some point you've executed composer with root and messed up your installation.
To find out where your composer's home directory is at, you should run this (as shown here):
composer config --list --global | grep home
E.g. given your output, it seems your home directory sits at "/home/rukon/.config/composer":
So you'd need to run:
sudo chown -R $USER: ~/.config/composer
To fix your "global" composer configuration directory.
You should be able to run composer global commands after that.
If you have more permissions problems, you'd better fix your home directory so everything in there is owned by your user, and nothing by root as shown here:
sudo find ~ -type d -user root -exec sudo chown -R $USER: {} +
For a regular installation the above should be safe (all the files in your home directory belong to to your own user), but if you have anything special in there you should probably be aware and take a more surgical approach (checking each directory).

Related

Unable to change file mode on bin: Operation not permitted

I'm trying to install Composer globally on Mac OS.
I tried to move the composer.phar file by using this command in Terminal as instructed in the Getting Started section:
mv composer.phar /usr/bin/composer
I get this error:
mv: rename composer.phar to /usr/bin/composer: No such file or
directory
When I try create the directory it says the operation is not permitted:
bash-3.2# mkdir -p /usr/bin/composer mkdir: /usr/bin/composer:
Operation not permitted
I even tried to navigate to the "bin" directory and it says:
bash-3.2# mkdir composer mkdir: composer: Operation not permitted
I've enabled root successfully but still I try and alter the permissions:
bash-3.2# chmod ugo+rwx "bin" chmod: Unable to change file mode on
bin: Operation not permitted bash-3.2#
What am I doing wrong here?
Thanks in advance!
EDIT:
Thanks for the responses!
When I type the following command:
ls -leO#d /usr/local/bin
It prints:
-rwxrwxr-x 1 username staff - 2212003 27 Mar 17:00 /usr/local/bin
Background: /usr/bin is generally for "standard" binaries, not things you install yourself; those generally belong someplace like /usr/local/bin. In recent versions of macOS, this is enforced by System Integrity Protection and by keeping "system" files/directories on a separate, read-only volume.
The problem: It looks like on your system, there's a file named /usr/local/bin rather than a directory. This can happen if some installer tries to install a program to /usr/local/bin (e.g. with mv someprogram /usr/local/bin) without first making sure a directory by that name exists.
How to solve it: The first thing to do is to move/rename whatever file got installed as /usr/local/bin, and then create it as a directory:
sudo mv /usr/local/bin /usr/local/bin-moved
sudo mkdir -p /usr/local/bin
Once that's straightened out, you should be able to install composer normally. According to this (and adding sudo to get access to the directory), you'd do that with:
sudo mv composer.phar /usr/local/bin/composer
Finally, if you want to, you can try to figure out what program got installed as (rather than in) /usr/local/bin. Or just delete the /usr/local/bin-moved file and don't worry about it.
/usr/bin is a protected system folder. Do not change its permissions.
Instead install it somewhere else, e.g. into /usr/local/bin. You may still need to run the copy command with sudo though: sudo mv composer.phar /usr/local/bin/composer

Yii2 composer returns an error in installation

AM using ubuntu linux in my development
I was installing yii2 advanced template via
composer create-project nenad/yii2-advanced-template advanced
Which starts the installation upto the point when the vendor directories are being installed and it fails with the error
After some research i found out that i need to instal global asset plugin via
composer global require "fxp/composer-asset-plugin:~1.2.0"
But now am getting an error of
Changed current directory to /home/myusername/.composer
[ErrorException]
file_put_contents(./composer.json): failed to open stream: Permission denied
I understand its a permission issue and i have
sudo chomd 0777 -R /var/www/html/yii2/advanced
but this havent solved the file_put_content error
I have tried
sudo chmod 0777 -R /home/myusername/.composer
but this also fails
What else do i need to do
Using 777 as permission mask is a bad idea. I'd recommend to use chown command instead.
sudo chown -R $USER:$USER /home/$USER/.composer
This will make composer directory owned by your user and you can use 755 for directories and 644 for files.
To make it clear, what problem composer exactly stumbled upon, try running require command with -vvv option. This allows you to run composer in full verbose mode and get debug information.
composer -vvv global require "fxp/composer-asset-plugin:~1.2.0"
Look into ~/composer folder and doublecheck file permissions and owner with ls -al

Uploading laravel project to cPanel

I'm trying to upload my laravel project to cPanel. But till now, it doesn't seems to be working.
I have moved following directories and files from my project into the directory 'lavravel' in root.
app/
bootstrap/
config/
database/
resources/
storage/
artisan
composer.json
composer.lock
.env
And moved files from my public folder to public_html
Now my directory looks like this.
I have made changes in following files.
laravel/bootstrap/app.php
/*
|--------------------------------------------------------------------------
| Public Path
|--------------------------------------------------------------------------
|
| Set the path to the Laravel "public" directory so that any files/packages
| that use app('path.public') to get the "public" path find the right path.
| DO NOT include a trailing slash on the path.
|
*/
$app->bind('path.public', function ()
{
return __DIR__.'/../../public_html';
});
public_html/index.php
require __DIR__.'/../laravel/bootstrap/autoload.php';
$app = require_once __DIR__.'/../laravel/bootstrap/app.php';
But, it doesn't seems to work. It displays a blank page. As you can see via this URL. nepalride.com
You should take a look at the Laravel
After uploading the files, make sure you set the correct permissions on the storage and bootstrap/cache folders.
Also, you need to run composer install to install the vendor packages required by Laravel. Your uploaded laravel folder doesn't have a vendors folder.
Look through the error logs and you will see all the issues you're encountering.
The easiest way to do it if you have ssh access is create a laravel directory with your app in. Then:
rm -rf public_html
ln -s laravel/public public_html
This deletes the current public_html (make sure there is nothing inside it you need before running it) and creates a symbolic link from public_html to your apps public folder.
manual path configuration of index.php will lead to problems down the road and so the proper way of configuring it would be to have ssh or terminal access. First make sure you have shell or terminal access(not usually accessible in shared hosting accounts).
Open terminal and check for php version
$ php -v
make sure php version is above the required version of your laravel app. if not change it to the latest available version.
Configure shell environment to use composer
$ echo 'alias composer="php -d allow_url_fopen=On /home/username/composer.phar"' >> ~/.bashrc
$ source ~/.bashrc
after that download and install composer on your cpanel account
$ cd ~
$ curl -k -O https://getcomposer.org/installer
$ php -d allow_url_fopen=On installer
make sure you have installed it
$ composer -v
go to the uploaded project either using ftp or git and run
$ composer install
to install dependencies
make sure your .env files are properly configured
run
$ php artisan key:generate
run migrations
$php artisan migrate
make sure you have proper access
$ chmod -R 775 storage
optimize your app
$ php artisan optimize
make app accessible to public by running
$ mv public_html public_html_old
create the symlink
$ ln -s /home/user/app/public /home/user/public_html
and all should be good and app should be running.

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.

Access composer.json /composer.phar

I have installed composer.phar in the following directory:
/public_html/composer
I have already added on package to the composer.json as below:
{
"require": {
"stripe/stripe-php": "2.*"
}
}
That was some time ago. I would now like to install another package, using composer. However, I cannot access it. I am trying to do the following in SSH/Terminal:
root#host [~]# composer.phar
-bash: composer.phar: command not found
Then I try to:
root#host [~]# cd public_html
root#host [~/public_html]# cd composer
-bash: cd: composer: No such file or directory
root#host [~/public_html]#
What am I doing wrong? In FTP I can see the composer is located in the /public_html/ folder, but I can't acccess it?
EDIT:
ls -l composer
gives me:
/bin/ls: Can't access composer: No such file or directory
Using ls -a I get:
./ .bwusage.sqlite error_log perl5/ .spamassassin/
../ .composer/ .forward php.ini.new .ssh/
.accesshash .cpanel/ .gnupg/ php.ini.orig support/
.bash_history cpanel3-skel/ .HttpRequest/ .pki/ .tcshrc
.bash_logout .cpanm/ .lesshst public_ftp/ tmp/
.bash_profile .cpobjcache/ .MirrorSearch/ public_html/ .trustwavereqs
.bashrc .cshrc .my.cnf .rnd .viminfo
The problem I believe is that /public_html/composer is under the root / and there is also a public_html under /root (home directory for root user) directory
use pwd to check where you are at the moment and use ls /public_html/composer to see if composer.phar is in this directory.
You can run: php /public_hmtl/composer/composer.phar.
In order to be able to run it as composer you must mv the composer.phar to a directory that is included in your PATH variable.
For example:
mv /public_html/composer/composer.phar /usr/bin/composer
So your structure is: public_html/composer/composer.phar
If so, then either navigate into the composer folder (cd public_html/composer), then issue the ./composer.phar or php ./composer.phar command.
You can excute it using relative/absolute paths (./public_html/composer/composer.phar or /srv/www/mysite/whatever/public_html/composer/composer.phar)
Please note, that you have to execute composer from the folder where your composer.json file is located.
In all cases you have to have access to the folder and the file itself.

Categories