Composer:file_put_contents(./ composer.json):无法打开流:权限被拒绝

时间:2016-12-18 20:06:29

标签: php linux laravel composer-php sudo

我正在尝试将Prestissimo安装到Ubuntu 16.04服务器上,但这会导致错误:

$ 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>]...

我以用户kramer65登录,因此我不知道为什么它无法写入我的主文件夹。我对permission denied的正常反应是使用sudo,但作曲家总是说:

  

不要以超级用户身份运行Composer!有关详细信息,请参阅https://getcomposer.org/root

知道如何解决这个问题吗?

11 个答案:

答案 0 :(得分:139)

我有这个问题要安装laravel / lumen。

我可以使用以下命令解决:

$ sudo chown -R $USER ~/.composer/

答案 1 :(得分:11)

要解决此问题,您应该打开一个终端窗口并输入以下命令:

sudo chown -R user ~/.composeruser是您当前的用户,在您的情况下,kramer65

运行此命令后,您应该有权运行composer global require命令。

您可能还需要从当前目录中删除.composer文件,为此打开一个终端窗口并输入以下命令:

sudo rm -rf .composer

答案 2 :(得分:1)

我也遇到过这个问题,但就我而言,我的目录错误。检查您正在使用的目录

答案 3 :(得分:0)

在我的情况下,.composer由root拥有,所以我做了sudo rm -fr .composer然后我的全局要求有效。

警告!如果您不确定自己在做什么,就不想使用该命令。

答案 4 :(得分:0)

这可能是超级边缘情况,但如果您使用Travis CI并利用缓存,则可能需要清除所有缓存并重试。

解决了我从sudo到非sudo版本时的问题。

答案 5 :(得分:0)

就我而言,~/.composer没有问题。
因此,进入Laravel应用根目录后,我做了sudo chown -R $USER composer.lock,它很有帮助。

答案 6 :(得分:0)

在您的PC中,只需在docker所在的目录中键入以下命令:

  

chown -R 1000:1000 / var / www

如果您将项目存储在其他文件夹中,则

/ var / www 可以更改。

例如:

  

chown -R 1000:1000 / home / myuser / my_projects

答案 7 :(得分:0)

我遇到了相同的异常,但就我而言,我正在使用PowerShell运行命令 因此,我通过一条指令来解决此问题,该指令首先要取消解锁多个文件。 { “statusCode”: 500, “error”: { “type”: “SERVER_ERROR”, “description”: “ERROR: Module ‘sqlite3’ already loaded on line 0 in file Unknown.” } } 然后使用以下内容加载程序包 PS C:\> dir C:\executable_file_Path\*PowerShell* | Unblock-File

答案 8 :(得分:0)

就我而言,我使用sudo mkdir projectFolder创建文件夹。它归root用户所有,我是使用非root用户登录的。

因此,我使用命令sudo chown mynonrootuser:mynonrootuser projectFolder更改了文件夹权限,然后工作正常。

答案 9 :(得分:0)

对我来说,在Ubuntu 18.04中。我需要塞进~/.config/composer/

例如

sudo chown -R $USER ~/.config/composer

然后全局命令起作用。

答案 10 :(得分:0)

我在 WSL Windows 10 中使用它时遇到了同样的错误。我使用以下命令来解决它:-

// In your component.spec file, 

@Injectable()
class MockService extends RealService {
  yourOriginalServiceMethod() {
    return of(mockData);
    // Here mockData can be any mocked-data. It should be of whatever the type your 
       original method in the service returns. Like an object 
   // 'of' is the RXJS operator. It will turn your mockData to an Observable so that when you run the test case, it will be subscribed without any issue. 
  }
}
  
beforeEach(() => {
  fixture = TestBed.createComponent(AppComponent);
  component = fixture.componentInstance;

  realService = TestBed.get(RealService); // this is important to test the subscription error scenario
});

describe('AppComponent', () => { // 2
  beforeEach(async(() => { // 3
    TestBed.configureTestingModule({
      declarations: [
        AppComponent
      ],
      providers: [
        {
           provide: MockService,
           useClass: RealService
        }
      ]
    }).compileComponents();
  }));

// Now your test case,  

it("component #yourComponentMethod() method for successful subscription",() => {
  spyOn(component, "yourComponentMethod").and.callThrough();
  component.yourComponentMethod();
  expect(component.yourComponentMethod).toHaveBeenCalled();

  // THis method will clear the successful subscription scenario
});


it("component #yourComponentMethod() method for failed subscription",() => {

  // This line will call the service and instead of returning mockData, it will fail it.
  spyOn(realService, 'yourMethodName').and.returnValue(throwError({status: 500}));
  
  // Rest is the same
  spyOn(component, "yourComponentMethod").and.callThrough();
  component.yourComponentMethod();
  expect(component.yourComponentMethod).toHaveBeenCalled();

  // THis method will clear the failed subscription scenario
});
相关问题