NativePHP: Developing Cross-Platform Desktop Apps with PHP

PHP isn't just for websites anymore! While we often use it for websites, APIs and command-line tools, there's a cool new thing called NativePHP. It lets us create desktop apps for Mac, Windows, and Linux using PHP. How cool is that?
The Emergence of NativePHP
Have you heard of Tinkerwell? It's a fantastic tool, but it's limited to macOS. But what if we want our apps to run on various platforms? Enter Marcel Ponciot's brainwave: Let's utilize PHP to craft apps that work on all these systems. And that's how NativePHP came into existence. The journey began in April 2023 (on Twitter), and in July, an alpha version was ready, with rapid development!
What if you could use @laravelphp within a native desktop app - shipping a static PHP binary?
— Marcel Pociot 🧪 (@marcelpociot) April 5, 2023
What if you could then use @laravelphp service providers to define native application behavior, such as menus in this app?
Check this out 🤯
Cooking up something good with @simonhamp pic.twitter.com/FECjn90LaY
Get started with NativePHP
Enough talk, let's get practical with NativePHP:
Please note that the examples provided are demonstrated on a Linux system (like Ubuntu), but the steps are nearly identical across platforms. However, it's worth mentioning that the experience might be even smoother on MacOS.
Before you begin, make sure you have the following prerequisites in place:
- PHP >=8.1 (& Composer)
- Laravel 10 or higher
- Node.js/NPM
- You can check additional requirements here:
Setting Up PHP
Depending on your operating system, you have various options for setting up PHP and the web server:
- Linux Users: Valet Linux is a suitable choice.
- MacOS Users: You can opt for tools like Herd or Laravel Valet.
- Windows Users: Consider using Laragon or Windows Subsystem for Linux (WSL).
Note: If you're using Valet, ensure that the DNS over HTTPS option is disabled in your browser to access your app smoothly.
Setting Up Node.js
For a seamless workflow, setting up Node.js is essential:
- Install the Latest Node.js version directly.
- Use Node Version Manager (NVM) for better version control.
Remember, these are just suggestions. Feel free to explore other tools and enjoy the process!
Create Blank Laravel Project
# Init your Laravel project, you can also check the official documentation: https://laravel.com/docs
composer create-project laravel/laravel app-with-native-php;
cd app-with-native-php;
# Install your dependencies
composer install && npm install;
# Install NativePHP package in your project (Electron version), you look in further details here
composer require nativephp/electron;
# Run the NativePHP installer
php artisan native:install;
To launch your app, use the following command:
php artisan native:serve
You can explore native features across macOS, Windows, and Linux, including custom windows, application menus, and notifications. Keep in mind, the NativePHP commands might evolve in the future, so consult the official documentation for any updates.
Please note that since NativePHP is in alpha version, certain features might not function as anticipated, depending on the operating system you're using.
Compagnon project
I've initiated an open-source project called Compagnon, which uses NativePHP. This app aims to centralize tools that aid developers in their daily tasks, with potential for further expansion.
You need some requirements to use this project:
- PHP 8.2
- Node.js 18.17 / NPM (nvm can be used)
# clone the repository
git clone https://github.com/DevCompanion/compagnon.git compagnon
# Initialize the application
make init.app
# Install project dependencies
make composer_ci && make npm_ci
# Generate the Laravel project key
php artisan key:generate
# Build the application assets
npm run build
# Launch the app with NativePHP
php artisan native:serve
You might see a screen that looks like the example below:
The source code is available on Github.
You can stay updated on my journey by following Twitter / X.
For existing projects
Requirements:
- Ensure you have Laravel 10 or higher installed
- PHP >=8.1 (& Composer)
- Check the prerequisites
# Install NativePHP in your project (Electron version), you look in further details here:
composer require nativephp/electron;
# Run the NativePHP installer
php artisan native:install;
# Launch the app with NativePHP
php artisan native:serve
On Linux (with no official support at the moment), you might encounter issues. If you encounter an error like 'vendor/nativephp/electron/resources/js/resources/php/php: 1: Syntax error: '(' unexpected,'
you can find solutions in this GitHub issue.
If you run into difficulties, make sure to check the NativePHP GitHub issues page. There, you can find help and solutions to common problems.
Contribution and the Future of NativePHP
The journey of NativePHP doesn't end with its release. The community is invited to contribute to this groundbreaking project. Every contribution, regardless of size, is valued. Future plans involve enhancing integration with Electron APIs and ongoing development for Tauri support, led by Simon.
Conclusion: Towards New Horizons
With NativePHP, PHP developers are changing the game. We're crafting applications that seamlessly function across diverse platforms. Marcel Ponciot has truly brought something remarkable here. For more details, explore the official NativePHP website.
For an extensive collection of resources, including projects, videos, and tutorials related to NativePHP, be sure to visit: Awesome NativePHP Repository.
References
Marcel Pociot
Simon Hamp
Official NativePHP Website
NativePHP (Github)
Marcel Pociot "Native PHP" - Laracon US 2023 Nashville (Youtube)
Compagnon (Github)