Local Laravel made simple

2016

The times of server software bundles on your dev machine are long behind us.

You‘re probably aware that back-end developers these days use virtualization tools such as Docker or Vagrant, mostly because “controlled environment”, “containerization” and “portability” are popular buzzwords these days.

While those are remarkable tools that do exactly what they promise, you might not always need them.

If you’re working on an application that only deals with native PHP API and does not rely on OS architecture or specific web server features, virtual environment might add more problems than it could solve. That’s especially true if you’re the only developer on the project.

Sometimes you just want to get going fast and crank out something that works ASAP. Or maybe you just want to check out some package. Waiting for a virtual machine to boot up can feel painfully slow when you’re getting in the flow.

What’s the alternative?

Here’s a good rule of thumb for development and life in general:

Everything should be made as simple as possible, but not simpler.

Pick the simplest tool that will do the job well and only move to something more complex when it isn’t enough anymore. Many people smarter than me have been preaching something along those lines for years: YAGNI.

The truth is, to run most small-to-medium projects locally you probably only need PHP — it provides a built-in web server since version 5.4. It has many limits and should not be used outside of development environment, but nothing can beat it‘s simplicity.

You might already have PHP installed if you’re using composer locally. If not, install it — that‘s the only dependency you’re gonna need.

What about a database?

SQLite is awesome — it works great, comes with PHP and can be used with Eloquent models.

Unless you need complex SQL features or rely on a specific DBMS behaviour, you should not notice much difference from MySQL, MariaDB or whatever you’re usually using.

You should be able to seamlessly transition from SQLite in development environment to other DBMS on staging and production servers.

Just make sure it is enabled in your php.ini file (it should be enabled by default, unless you are using Windows):

extension=php_sqlite3.dll

Now configure your .env file in Laravel project root folder:

DB_CONNECTION=sqlite
DB_DATABASE=full/path/to/file.sqlite

That’s it! It’s just a library that uses filesystem to store data, so no database servers will be running in the background.

You can put .sqlite file into /database folder in your Laravel application or anywhere else you find suitable.

Finishing up

Only one thing left to do. Open command line in your Laravel project root and run:

php artisan serve[ --host "example.dev" --port 80]

That’s it! Your development environment is live.

Do not forget to add host domain to your hosts file and point it to 127.0.0.1. If you do not specify host and port options, the default is localhost:8000.

Server will stay live as long as you keep the console open. You can run multiple servers at once — just choose different port numbers.

You can put artisan serve command into a bash (or npm) script so that you do not have to type out the hostname and port every time.

Run migrations, seeders and you’re ready to go!

And you can always move to a VM when this bare bones setup becomes too limiting.