
5 minutes read
Use Bun as your package manager in any PHP project
Table of contents
- → Introduction
- → Why switch from npm, pnpm, or Yarn to Bun
- → Install Bun on macOS
- → Install Bun on Linux and WSL
- → Install Bun on Windows
- → Migrate your project: replace existing lockfiles and run bun install
- → Add and remove packages with Bun
- → Run your package.json scripts with Bun
- → CI and troubleshooting tips
- → Conclusion
Introduction
Bun is a fast JavaScript all‑in‑one toolkit you can use as a package manager in PHP projects. I use it daily in Laravel and Symfony builds to install dependencies and run scripts faster. If you are used to npm, pnpm, or Yarn, Bun feels familiar but quicker.
Why switch from npm, pnpm, or Yarn to Bun
Most PHP developers only use Node.js to build front‑end assets. Switching tools can feel like a chore, but Bun gives you real gains:
- bun install can be up to 25x faster than npm install. See the bun install docs.
- bun run has much lower overhead than npm run, roughly 6 ms vs 170 ms in the example (about 28x). See the quickstart.
- Simple script startup on Linux is shown around 5.2 ms with Bun vs 25.1 ms with Node.js (about 4–5x). See the bun run docs.
In short:
- Your front‑end dependencies will install faster.
- Your assets will compile faster.
- CI runs will be faster.
Install Bun on macOS
Use Homebrew. Either of these works (pick one):
brew install oven-sh/bun/bun # or brew tap oven-sh/bun brew install bun
See the official installation guide.
Install Bun on Linux and WSL
Use the installer script:
curl -fsSL https://bun.com/install | bash
Before you start, make sure unzip is installed. On Linux, the minimum kernel is 5.1, and 5.6 or higher is recommended. Details are in the installation guide.
Install Bun on Windows
Bun has first‑class Windows support (Windows 10 version 1809 or later). You can install it in several ways:
PowerShell:
powershell -c "irm bun.sh/install.ps1 | iex"
Scoop:
scoop install bun
Chocolatey:
choco install bun
See Install Bun on Windows and the Windows section on the Get started page.
Migrate your project: replace existing lockfiles and run bun install
You can switch from npm, pnpm, or Yarn with a small cleanup, then one install:
- Remove other lockfiles so Bun can create its own lockfile:
- package-lock.json (npm)
- pnpm-lock.yaml (pnpm)
- yarn.lock (Yarn)
- Run:
bun install
As of Bun v1.2, the default lockfile is text‑based bun.lock. Projects that already use bun.lockb still work, and Bun continues to support that format. See the lockfile docs.
For reproducible builds in CI, commit bun.lock and use:
bun install --frozen-lockfile # or bun ci
Add and remove packages with Bun
To install your current dependencies (from package.json), run:
bun install
If you want to troubleshoot network or cache issues, you can disable the cache:
bun install --no-cache
For details and flags, see the bun install command.
Add packages with bun add. Here is an example with three packages:
bun add tailwindcss autoprefixer postcss
See bun add for options like –dev and –exact.
Remove a package with bun remove:
bun remove axios
Since the Fetch API is built in, many apps do not need Axios. Both Bun and Node.js 18+ provide fetch globally. See bun remove.
Run your package.json scripts with Bun
Bun plugs into your current workflow. Run the scripts defined in package.json like you do with npm:
bun run dev
bun run is fast and works well for PHP front‑end build pipelines. It also offers much lower startup overhead than npm run. See the bun run docs.
On Windows, bun run uses Bun shell by default. If a script expects your system shell, set this in bunfig.toml:
[run] shell = "system"
Learn more in bunfig.toml.
CI and troubleshooting tips
- Reproducible installs: use bun ci or bun install –frozen-lockfile, and commit bun.lock. See bun install.
- Speed and caching: Bun uses a global cache and hardlinks/copy‑on‑write to keep disk usage low. See global cache.
- No cache runs: bun install –no-cache can help when debugging registry issues.
- Security note: Bun does not run dependency lifecycle scripts (like postinstall) unless you list the package in trustedDependencies. See lifecycle scripts and trustedDependencies.
- Windows shell gotcha: bun run defaults to Bun shell on Windows. If a script fails due to shell syntax, set run.shell to system.
Conclusion
Bun is a drop‑in package manager that makes PHP front‑end builds feel fast. It installs quickly on macOS, Linux, and Windows, it writes a readable bun.lock by default, and it speeds up installs and scripts. I recommend trying it on a small Laravel or Symfony project and measuring the difference.
Related reading: What’s the fuss around Bun’s package manager abilities?
Did you like this article? Then, keep learning:
- Get practical tips to fix common Laravel session errors potentially related to frontend interactions
- Learn to add Alpine.js to Laravel projects to enhance frontend interactivity
- Explore Bun's usage tailored for Laravel projects, complementing PHP integration
- Understand why Bun outperforms NPM, Yarn, and pnpm in speed and efficiency
- Discover fast and effective Laravel database management practices for optimized app performance
- Learn how to add Vue.js to Laravel projects, an alternative frontend framework with Bun
- A detailed guide on adding and using Tailwind CSS in Laravel projects
- Get a comprehensive guide to Tailwind CSS, a popular frontend framework compatible with Bun
- Understand how to run and manage Laravel application commands with Artisan CLI
0 comments