PHP - - By Claudio Ribeiro

The Android Elephpant – Laravel on your Android Phone?

It was not that long ago that Christopher Pitt wrote an excellent article about writing and running PHP code on an iPad. After reading it, I thought to myself “It would be really cool to do the same on Android”: being able to write and edit code on the fly, for example while traveling, and not having to take the laptop everywhere. So I’ve decided to do some research and see what I could come up with.

Android Elephpant

For this article, you can use any type of Android device. I’ve done it on my phone, but an Android tablet with a Bluetooth keyboard would probably be the ideal setup.

There are a couple of different shell apps for Android. For this tutorial we will use one called Termux.

Termux

Termux combines both a powerful terminal emulation and an extensive Linux package collection. It is also completely free and easy to use.

After installing Termux from the Play Store, the first thing to do is to run the apt update command. As per the documentation: “This command needs to be run initially directly after installation and regularly afterwards to receive updates.”

Now for the fun part. The first two commands I want to talk about are the apt list and apt list --installed commands. The first one will list all the available packages for Termux. We can see that it has support for a lot of different programming languages, text editors and has some useful utility packages like zip, tar and so on. The second command will list all the installed packages. As we can see Termux already comes with some packages like apt and bash pre-installed.

My goal when testing Termux was to see if I could assemble a proper* PHP development environment, so I started by installing a text editor. I prefer Vim, but there are some more options available, like Emacs and Nano. Vim has a bit of a learning curve to it, but it gets very comfortable when you get past the basics of it. You can get Vim with the apt install vim command.

If you want to learn more about vim, there’s this very good article or, alternatively, after installing it, type vimtutor to use the built-in tutorial.

If you are testing this on your Android phone, running vim will bring the first set of problems. How can I hit the Escape button? Termux has a large list of shortcuts that be used to simulate the buttons that are not available on the Android keyboards:

Command Key
Volume Up+E Escape key
Volume Up+T Tab key
Volume Up+1 F1 (and Volume Up+2 → F2, etc)
Volume Up+0 F10
Volume Up+B Alt+B, back a word when using readline
Volume Up+F Alt+F, forward a word when using readline
Volume Up+X Alt+X
Volume Up+W Up arrow key
Volume Up+A Left arrow key
Volume Up+S Down arrow key
Volume Up+D Right arrow key
Volume Up+L (the pipe character)
Volume Up+U _ (underscore)
Volume Up+P Page Up
Volume Up+N Page Down
Volume Up+. Ctrl+\ (SIGQUIT)
Volume Up+V Show the volume control

Now that we have our editor up and running, it’s time to install the packages we’ll need: PHP, Git and Composer.

apt install php
apt install git

This will install the latest PHP and Git packages.

Packages-1

For Composer, we need to do a little bit of extra work. We need to go to the Composer download page and use the instructions for the command line installation:

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('SHA384', 'composer-setup.php') === '55d6ead61b29c7bdee5cccfb50076874187bd9f21f65d8991d46ec5cc90518f447387fb9f76ebae1fbbacf329e583e30') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

This will download the installer, verify it, run it and delete it. If everything went well, we should be able to run Composer from Termux.

Packages-2

Now that we have all of our tools installed, we should test if our PHP installation is running correctly. For that, let’s do a simple phpinfo() test. Let’s create a new folder and test our PHP installation.

mkdir test
cd test
echo "<?php phpinfo();" > index.php
php -S localhost:8080

This will create a new folder and then create an index.php file with the phpinfo() command inside of it. I’m echoing it directly into the file, but you can use Vim to do it. Finally, we are using the PHP server to serve it to our localhost. When accessing localhost:8080 in our browser we should see something like this:

PHPInfo

We now have Composer for dependency management, and git for version control. But I know what you’re thinking: “we just made a simple phpinfo test, what about the rest?”.

Can we Install Laravel on an Android device?

At this point we have everything we need to install and run Laravel on our Android device. To create a new Laravel project, we need to run the following command:

php composer.phar create-project --prefer-dist laravel/laravel new_project

This will create a new Laravel project in the new_project folder. The --prefer-dist option is well documented here. The installation will probably take a little while. After it’s complete, we can run our newly created project using Laravel’s own Artisan command line interface. Inside the new_project folder, we can run the command:

php artisan serve

Accessing the localhost:8000 URL in the browser should show us the Laravel home screen now.

Home screen on mobile

Success! Our Laravel installation is complete. We have successfully installed the tools we need to write and execute code. But no development environment is complete without a way of persisting data.

When it comes to Android devices, most of the time, memory and storage capacity are real issues. Because of that, Termux only offers sqlite as a way of persisting data. SQLite is a serverless, file-based database engine. It is lightweight and ideal for small amounts of data, as you can read here and in this beyond the basics post. First, we need to install it.

apt install sqlite

Next, we need to configure our Laravel project to use sqlite. In the root of our project we have a .env file. This is the environment configuration file and is the first file we need to edit. Use your editor of choice to edit the following lines:

DB_CONNECTION=sqlite
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

Then go to the config/database.php file and change the following line from:

'default' => env('DB_CONNECTION', 'mysql'),

to

'default' => env('DB_CONNECTION', 'sqlite'),

This will make sqlite the default connection from the connections array.

'connections' => [
        'sqlite' => [
            'driver' => 'sqlite',
            'database' => env('DB_DATABASE', database_path('database.sqlite')),
            'prefix' => '',
        ],
        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],
        'pgsql' => [
            'driver' => 'pgsql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '5432'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
            'schema' => 'public',
            'sslmode' => 'prefer',
        ],
    ],

The database path points to a database.sqlite file. This file does not exist yet, so we need to create it.

touch database/database.sqlite

This is all the configuration we need to tell Laravel to use SQLite – we are now ready to test it. We’ll use Laravel’s pre-built authentication system. To create the scaffold we need to run the following command:

php artisan make:auth

After this, we will run the migrations to build our database schema. This will create the users and password_reset tables.

php artisan migrate

If we run php artisan serve again we’ll see that now we have the option to register and log in. Our authentication CRUD has been successfully created!

Home screen with register option

Conclusion

I did all this on my Android phone. This setup is great for small development tasks, as it has all the tools necessary for you to start developing on smaller devices, without the need to carry your laptop with you everywhere.

While not exactly the pinnacle of productivity, it could easily come in handy when an urgent fix needs to be done, or when you want to see how much PHP performance you can milk out of an Android device.

Give it a try and tells us what you think, and if you built something interesting in PHP on Android, sell us the concept and we’ll write about it!


*proper meaning proper for the context. An Android phone won’t have MySQL running, and won’t be able to run intricate test suites, especially e2e tests, but other things might just work well enough to get some work done.

Sponsors