Getting Started with phpkg
Welcome to phpkg
phpkg is a lightweight PHP package manager that simplifies your workflow. Forget Composer’s complexity—phpkg autoloads namespaced functions and classes from Git repositories directly, with no central registry or bloated vendor/ dirs. Whether you’re building apps or running scripts, phpkg empowers both object-oriented and functional programming, getting you coding faster.
- Why phpkg? No setup overload, instant Git integration, and the freedom to write lean functions without class wrappers.
- Quick Start: Try this now:
bash -c "$(curl -fsSL https://raw.githubusercontent.com/php-repos/phpkg-installation/master/install.sh)" phpkg run https://github.com/php-repos/chuck-norris
See a Chuck Norris joke in your terminal—no config needed.
Requirements
To use phpkg, ensure you have:
- PHP >= 8.1: With
php-mbstring(for string handling),php-zip(for unpacking archives) andphp-curl(for Git downloads). - Git: To clone repositories directly.
- unzip: To extract package files.
Works on Unix/Linux/macOS and Windows. Windows users need PHP 8.5 or later (PHP 8.5 is fully tested; later versions may need additional testing).
Installation
Unix/Linux/macOS
Install phpkg with one command:
bash -c "$(curl -fsSL https://raw.githubusercontent.com/php-repos/phpkg-installation/master/install.sh)"
This downloads and sets up phpkg in your home directory. Open a new terminal to use it (path updated).
Windows
Run this command in PowerShell to install phpkg:
powershell -ExecutionPolicy Bypass -Command "Invoke-WebRequest -Uri 'https://raw.githubusercontent.com/php-repos/phpkg-installation/master/install.ps1' -OutFile install.ps1 -UseBasicParsing; powershell -ExecutionPolicy Bypass -File install.ps1"
This downloads and sets up phpkg in your home directory. Open a new terminal to use it (path updated).
Usage
phpkg excels at managing packages and running standalone scripts. Here’s how to get started.
Set Up Your Project
-
Initialize: Create a new project:
mkdir my-app && cd my-app phpkg init- Adds
Packages/for dependencies,phpkg.config.jsonfor settings, andphpkg.config-lock.jsonfor metadata.
- Adds
-
Configure: Map your namespaces in
phpkg.config.json:{ "map": {"App": "src", "Tests": "tests"}, "entry-points": ["public/index.php"], "packages": [] }App\→src/,Tests\→tests/.
-
Add a Package: Install from GitHub:
phpkg add php-repos/observer- Clones
observertoPackages/, updates configs with version and hash. - You can use simplified formats:
owner/repofor GitHub packages (e.g.,php-repos/observer) or justrepofor php-repos packages (e.g.,observer). - Full Git URLs also work:
https://github.com/php-repos/observer.gitorgit@github.com:php-repos/observer.git.
No central repo—just Git URLs. Works with GitHub now, GitLab and others soon.
- Clones
-
Build: Build your project:
phpkg build- Creates
build/directory with production-ready files, autoloads dependencies into entry points (e.g.,public/index.php).
Watch changes live with
phpkg watchto automatically rebuild on file changes. - Creates
Add GitHub Token (Optional) For private repos, or when you have many dependencies, add a GitHub token:
phpkg credential github.com <your-token>
- Generate at GitHub
- Skip if
GITHUB_TOKENenv var is set.
Run Standalone Packages Execute scripts directly:
phpkg run php-repos/weather
- Outputs weather forecast in your terminal—no setup.
- You can use simplified formats:
owner/repofor GitHub packages or justrepofor php-repos packages.
Example: Build a Simple App
- Init:
phpkg init - Add Code:
// src/Hello.php namespace App; function greet($name) { return "Hello, $name!"; }// public/index.php require 'build/phpkg.imports.php'; echo \App\greet('World'); - Build:
phpkg build - Run:
cd build php public/index.phpOutput: “Hello, World!”
phpkg autoloads namespaced functions like App\greet—no classes needed for simple tasks.
Why phpkg?
- Function-First Autoloading:
phpkgautoloads namespaced functions—not just classes—letting you write simple, reusable functions instead of wrapping everything in objects. Unlike Composer’s class-only PSR-4, this unlocks functional programming in PHP, making your code cleaner and more expressive. - Speed: Installs in seconds, builds without bloat—no heavy
vendor/dirs or lock files slowing you down. - Freedom: Use any Git repo directly—no central registry or middleman locking you in.
- Simplicity: Minimal setup, maximum control—focus on coding, not configuring.
Common Workflows
Starting a New Project
# 1. Initialize
phpkg init
# 2. Add dependencies
phpkg add php-repos/observer
phpkg add owner/package-name
# 3. Build
phpkg build
# 4. Develop (with auto-rebuild)
phpkg watch
Running Standalone Tools
# Run a CLI tool
phpkg run php-repos/weather
# Serve a web app
phpkg serve php-repos/daily-routine
Updating Dependencies
# Update to latest
phpkg update php-repos/observer
# Update to specific version
phpkg update php-repos/observer v2.0.0
# or
phpkg update php-repos/observer --version=v2.0.0
# Update all (install after changes)
phpkg install
Key Concepts
Simplified Package Formats
phpkg supports simplified package identifiers:
- GitHub packages:
owner/repo(e.g.,php-repos/observer) - php-repos packages: Just
repo(e.g.,observer) - Full URLs:
https://github.com/owner/repo.gitorgit@github.com:owner/repo.git
Function-First Autoloading
Unlike Composer, phpkg autoloads both classes and functions:
namespace App\Utils;
// This function is autoloaded!
function format($text) {
return strtoupper($text);
}
// So is this class
class Formatter {
// ...
}
Build System
- Build:
phpkg build→build/directory - Auto-rebuild:
phpkg watchmonitors changes and rebuilds automatically
For deeper understanding, see Concepts.
Related Documentation
- Installation - Install phpkg on your system
- Concepts - Understand how phpkg works under the hood
- Init Command - Initialize a new phpkg project
- Add Command - Add packages to your project
- Build Command - Build your project
- Best Practices - Recommended workflows and patterns
- Command Comparison - When to use which command
- FAQ - Frequently asked questions