Composer global require Laravel/Installer not working on Ubuntu
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
# Composer Global Require Fails on Ubuntu: Solving Permission Denied Errors
As a senior developer, I’ve seen countless frustrating roadblocks when setting up command-line tools, especially on Linux distributions like Ubuntu. The error you are encountering—`file_put_contents(./composer.json): failed to open stream: Permission denied`—is a classic symptom of insufficient file system permissions, not necessarily a bug in Composer itself.
This issue usually arises when attempting to install global packages or access configuration files in directories owned by the system root, which is common when using commands like `sudo`.
Let’s dive into why this happens and how we can correctly set up your environment to successfully install tools like the Laravel Installer.
## Understanding the Permission Denied Error
When you execute a command that tries to write a file (like Composer installing a package definition in `composer.json`), the operating system checks if the current user has the necessary write permissions for that directory.
In your case, even though you installed Composer using `sudo php -- --install-dir=/usr/local/bin`, subsequent global operations might run under a restricted context, or the specific directory where Composer attempts to store its global data is locked down by default system configurations. The attempt to use `chmod -R 777` often fails because changing permissions on core system directories requires elevated privileges that might be blocked even when using `sudo` in certain execution contexts.
## Solutions for Global Installation Issues
Since simply changing directory permissions didn't resolve the issue, we need a more targeted approach focused on ownership and installation methods. Here are the most reliable ways to fix this:
### Method 1: Using `sudo` Correctly (The Quick Fix)
The most straightforward way to bypass permission errors for system-wide installations is to ensure *all* operations run with root privileges. While running global commands with `sudo` is common, it can sometimes lead to ownership issues later on if you are trying to manage those files as a standard user.
Try executing the command directly with `sudo`:
```bash
sudo composer global require laravel/installer
```
If this still fails, it confirms that the issue isn't just about the final installation step, but how Composer is trying to initialize its environment globally on your specific Ubuntu setup.
### Method 2: Setting Up User-Specific Global Directories (The Best Practice)
For long-term stability and better permission management—especially when working within a development environment—it is best practice to configure Composer to store global packages in a directory owned by your user account, rather than forcing everything into system directories.
You can configure Composer to use a user-specific path. This ensures that your user owns the files, eliminating future "Permission denied" errors related to writing configuration files like `composer.json`.
1. **Create a dedicated directory for global packages:**
```bash
mkdir -p ~/.composer/vendor
```
2. **Configure Composer to use this path (if necessary, though often handled by environment variables):**
Ensure your shell environment variables are correctly set up to point to user-writable locations. For many modern setups, simply ensuring you are running commands as your standard user (`$USER`) resolves the issue when using global installs, aligning with best practices advocated by platforms like [Laravel](https://laravelcompany.com).
### Method 3: Reinstalling Composer for Cleanliness
If permissions remain stubbornly locked down, a clean reinstallation can often reset the ownership structure correctly. You can remove the existing installation and reinstall it following official documentation, ensuring all necessary environment variables are set up properly.
For detailed setup guidance on setting up your project dependencies and tools, always refer to resources provided by [Laravel](https://laravelcompany.com). This approach ensures that when you install tools like the Laravel Installer, they integrate smoothly with your operating system permissions.
## Conclusion
The "Permission denied" error during global Composer operations is almost always a file ownership issue masquerading as a permission problem. By shifting focus from simply changing file modes (`chmod`) to correctly managing directory ownership (using `mkdir` and ensuring correct execution context), you can resolve this frustrating hurdle. Always prioritize user-owned directories when dealing with application tooling on Linux systems.