Welcome to our tutorial on how to install Laravel PHP framework on Ubuntu 20.04. Laravel is a free, open-source PHP web framework which provides expressive, elegant syntax to web artisans. Laravel is one of the best choices for building modern, full-stack web applications.
Laravel is accessible, powerful, and provides tools required for large, robust applications. Some of its features include;
- Simple, fast routing engine.
- Powerful dependency injection container.
- Multiple back-ends for session and cache storage.
- Expressive, intuitive database ORM.
- Database agnostic schema migrations.
- Robust background job processing.
- Real-time event broadcasting.
Installing Laravel PHP Framework On Ubuntu 20.04
Prerequisites
In order to install Laravel PHP framework, there are a few system requirements that have to be installed and setup.
- Web Server (Apache or Nginx)
- PHP (latest version recommended) and some modules
- Database (such as MariaDB or MySQL)
- Composer PHP package manager
Basically, you need either a LEMP or LAMP stack to run Laravel. You can follow the links below to install LEMP/LAMP stack on Ubuntu 20.04.
Install LAMP Stack on Ubuntu 20.04
Install and Setup LEMP Stack on Ubuntu 20.04
In this setup, however, we will install Laravel with LAMP stack.
Install Other Required PHP Extensions
Apart from the default PHP extensions that gets installed alongside PHP package, there are other extensions that you need to install. Run the command below to install them;
apt install php-bcmath php-gd php-mbstring php-xml php-zip php-tokenizer -y
Just to confirm, we are using PHP 7.4;
php -v
PHP 7.4.3 (cli) (built: Oct 6 2020 15:47:56) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies
Create Laravel Database and Database User
Login to your database backend and create a database and database user for your application. Be sure to replace the database name and user appropriately.
create database demoapp;
grant all on demoapp.* to demoadmin@localhost identified by 'changeme';
Reload database privilege tables;
flush privileges;
quit
Install Composer PHP Package Manager on Ubuntu 20.04
Assuming you already have the Apache HTTP server, PHP and extensions and Database (MariaDB or MySQL), proceed to install PHP Composer.
Run the script below to install Composer programmatically on Ubuntu;
vim install-composer.sh
#!/bin/sh
EXPECTED_CHECKSUM="$(wget -q -O - https://composer.github.io/installer.sig)"
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
ACTUAL_CHECKSUM="$(php -r "echo hash_file('sha384', 'composer-setup.php');")"
if [ "$EXPECTED_CHECKSUM" != "$ACTUAL_CHECKSUM" ]
then
>&2 echo 'ERROR: Invalid installer checksum'
rm composer-setup.php
exit 1
fi
php composer-setup.php --install-dir=/usr/local/bin --filename=composer --quiet
RESULT=$?
rm composer-setup.php
exit $RESULT
Save and exit the script.
Execute it;
bash install-composer.sh
Check the exit status of the script. It will exit with 1 in case of failure, or 0 on success, and is quiet if no error occurs.
echo $?
Verify composer is installed;
which compoer
/usr/local/bin/composer
You can as well check version of installed composer;
composer --version
Install Laravel PHP Framework
There are two ways in which you can install Laravel PHP framework;
Install Laravel PHP Framework Using Laravel Installer
Create Non root user to run composer as (it is strongly advised to avoid running Composer as super-user/root).
useradd -m -d /var/www/html/laravel -s /usr/bin/bash -g www-data laravel
In the command above, we created a non root user called laravel whose home directory is set to /var/www/html/laravel
. We will be using this directory as our default Laravel directory for our Apps.
Switch to the user and run the Laravel live installer as follows;
su - laravel
composer global require laravel/installer
Changed current directory to /var/www/html/laravel/.config/composer
Using version ^4.1 for laravel/installer
./composer.json has been created
Running composer update laravel/installer
Loading composer repositories with package information
Updating dependencies
Lock file operations: 12 installs, 0 updates, 0 removals
- Locking laravel/installer (v4.1.1)
- Locking psr/container (1.0.0)
- Locking symfony/console (v5.2.1)
- Locking symfony/polyfill-ctype (v1.20.0)
- Locking symfony/polyfill-intl-grapheme (v1.20.0)
- Locking symfony/polyfill-intl-normalizer (v1.20.0)
- Locking symfony/polyfill-mbstring (v1.20.0)
- Locking symfony/polyfill-php73 (v1.20.0)
- Locking symfony/polyfill-php80 (v1.20.0)
- Locking symfony/process (v5.2.1)
- Locking symfony/service-contracts (v2.2.0)
- Locking symfony/string (v5.2.1)
Writing lock file
Installing dependencies from lock file (including require-dev)
...
All the necessary packages are installed to /var/www/html/laravel/.config/composer
directory. As a result, you need to update the PATH environment variable with Laravel binary configuration directory, /var/www/html/laravel/.config/composer/vendor/bin/.
You ca simply run the command below to update the path.
echo 'export PATH="$HOME/.config/composer/vendor/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
Create PHP demo App using Laravel
Create demo app with the default Laravel settings;
laravel new demoapp
_ _
| | | |
| | __ _ _ __ __ ___ _____| |
| | / _` | '__/ _` \ \ / / _ \ |
| |___| (_| | | | (_| |\ V / __/ |
|______\__,_|_| \__,_| \_/ \___|_|
Creating a "laravel/laravel" project at "./demoapp"
Installing laravel/laravel (v8.5.7)
- Installing laravel/laravel (v8.5.7): Extracting archive
Created project in /var/www/html/laravel/demoapp
> @php -r "file_exists('.env') || copy('.env.example', '.env');"
Loading composer repositories with package information
Updating dependencies
Lock file operations: 105 installs, 0 updates, 0 removals
- Locking asm89/stack-cors (v2.0.2)
- Locking brick/math (0.9.1)
...
Package manifest generated successfully.
73 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
> @php artisan key:generate --ansi
Application key set successfully.
Application ready! Build something amazing.
To verify that the Laravel components are working fine, navigate to application directory and run php artisan
command.
cd demoapp
php artisan
Laravel Framework 8.21.0
Usage:
command [options] [arguments]
Options:
-h, --help Display help for the given command. When no command is given display help for the list command
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi Force ANSI output
--no-ansi Disable ANSI output
-n, --no-interaction Do not ask any interactive question
--env[=ENV] The environment the command should run under
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
Available commands:
...
Install Laravel PHP Framework using Composer
You can similarly install Laravel PHP framework on Ubuntu using Composer, PHP package manager.
Navigate to the Laravel directory, which in our case is /var/www/html/laravel
.
su - laravel
pwd
/var/www/html/laravel
Install Laravel using composer;
composer create-project --prefer-dist laravel/laravel demoapp
Similarly, you can run run php artisan
command.
Configure Laravel Application Environment Variables
Once the installation is done, adjust the application environment variables (APP_NAME, APP_ENV, APP_KEY, APP_DEBUG, APP_URL
) appropriately;
vim demoapp/.env
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:1nzhBMNqoyGP+DYbPM1Pq33MAPTQ+LGpTHwxuADV1v4=
APP_DEBUG=true
APP_URL=http://demoapp.kifarunix-demo.com
...
Define the Laravel application database connection settings as created above;
...
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=demoapp
DB_USERNAME=demoadmin
DB_PASSWORD=changeme
...
Create Apache Site configuration file for Laravel Application
Once you are done with installation and configuration of Laravel, create the specific app Apache site configuration file to enable you access it externally from browser.
The commands below are as root user.
For example, to create Apache site configuration for demoapp Laravel app;
vim /etc/apache2/sites-available/demoapp.conf
<VirtualHost *:80>
ServerName demoapp.kifarunix-demo.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/laravel/demoapp/public
<Directory /var/www/html/laravel/demoapp>
Options Indexes MultiViews
AllowOverride None
Require all granted
</Directory>
ErrorLog /var/log/apache2/demoapp.error.log
CustomLog /var/log/apache2/demoapp.access.log combined
</VirtualHost>
Save and exit the file and run syntax check;
apachectl -t
Disable the default Apache site;
a2dissite 000-default.conf
Enable demoapp site configuration;
a2ensite demoapp.conf
Set the proper ownership of the demoapp web root directory;
chown -R :www-data /var/www/html/laravel/demoapp/
Restart Apache;
systemctl restart apache2
Accessing Laravel App from Browser
To allow external access to your apps, open HTTP(S) port on firewall if it is running;
ufw allow "Apache Full"
Then navigate to browser and access Laravel App using the URL, http://APP-URL
, for example, http://demoapp.kifarunix-demo.com
.
And that confirms that Laravel is installed and working as expected. You can now start building your web applications using Laravel PHP framework.
Reference and Further Reading
Other Tutorials
Install and Use Mendeley in Ubuntu 20.04
Install NetBeans IDE on Debian 10
Install NetBeans IDE on Ubuntu 20.04