How to install Laravel on Ubuntu 22.04

To Install Laravel On Ubuntu 22.04

Introduction

Laravel is a free and open-source PHP web framework designed to facilitate the development of robust and scalable web applications. It builds upon the widely-used Symfony framework and offers a straightforward, user-friendly approach to coding. As a backend framework, Laravel encompasses a comprehensive set of features necessary for developing modern web applications, including routing, validation, caching, queuing, file storage, and additional functionalities.

Procedure

Step1: Check the OS version using the below command

root@linuxhelp:~# lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 22.04.4 LTS
Release:	22.04
Codename:	jammy

Step 2: Check for the updates using the below command

root@linuxhelp:~# sudo apt update
Hit:1 http://in.archive.ubuntu.com/ubuntu jammy InRelease
Get:2 http://in.archive.ubuntu.com/ubuntu jammy-updates InRelease [128 kB]
Get:3 http://security.ubuntu.com/ubuntu jammy-security InRelease [129 kB]
Hit:4 http://in.archive.ubuntu.com/ubuntu jammy-backports InRelease 
Fetched 14.2 MB in 4s (3,402 kB/s)                           
Reading package lists... Done
Building dependency tree... Done
 
Reading state information... Done
10 packages can be upgraded. Run 'apt list --upgradable' to see them.

Step 3: Upgrade the package list using the below command

root@linuxhelp:~# sudo apt upgrade
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Calculating upgrade... Done
The following packages were automatically installed and are no longer required:
  libflashrom1 libftdi1-2 libllvm13 libwpe-1.0-1 libwpebackend-fdo-1.0-1
Use 'sudo apt autoremove' to remove them.
The following packages have been kept back:
  python3-update-manager update-manager update-manager-core
The following packages will be upgraded:
  libpython3.10 libpython3.10-minimal libpython3.10-stdlib libssl3 openssl python3.10 python3.10-minimal
7 upgraded, 0 newly installed, 0 to remove and 3 not upgraded.
7 standard LTS security updates
Need to get 10.5 MB of archives.
After this operation, 8,192 B of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://in.archive.ubuntu.com/ubuntu jammy-updates/main amd64 libpython3.10 amd64 3.10.12-1~22.04.5 [1,950 kB]
Setting up libpython3.10-stdlib:amd64 (3.10.12-1~22.04.5) ...
Setting up libpython3.10:amd64 (3.10.12-1~22.04.5) ...
Setting up python3.10 (3.10.12-1~22.04.5) ...
Processing triggers for libc-bin (2.35-0ubuntu3.8) ...
Processing triggers for man-db (2.10.2-1) ...
Processing triggers for mailcap (3.70+nmu1ubuntu1) ...
Processing triggers for desktop-file-utils (0.26-1ubuntu3) ...
Processing triggers for gnome-menus (3.36.0-1ubuntu3) ...

Step 4: For Laravel to work we need Apache , to install Apache use the below command

root@linuxhelp:~# sudo apt install apache2
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libflashrom1 libftdi1-2 libllvm13 libwpe-1.0-1 libwpebackend-fdo-1.0-1
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
  apache2-bin apache2-data apache2-utils libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap
Suggested packages:
  apache2-doc apache2-suexec-pristine | apache2-suexec-custom
Created symlink /etc/systemd/system/multi-user.target.wants/apache2.service → /lib/systemd/system/apache2.service.
Created symlink /etc/systemd/system/multi-user.target.wants/apache-htcacheclean.service → /lib/systemd/system/apache-htcacheclean.service.
Processing triggers for ufw (0.36.1-4ubuntu0.1) ...
Rules updated for profile 'Samba'

Processing triggers for man-db (2.10.2-1) ...
Processing triggers for libc-bin (2.35-0ubuntu3.8) ...

Step 5: Start the Apache using the below command

root@linuxhelp:~# systemctl start apache2

Step 6: Enable the Apache using the below command

root@linuxhelp:~# systemctl enable apache2
Synchronizing state of apache2.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable apache2

Step 7: Laravel relies on PHP, and composer is dependency manager for PHP , install PHP with dependencies using the below command

root@linuxhelp:~# sudo apt install php8.1-fpm php8.1-mysql php8.1-gd php8.1-bcmath php8.1-cli php8.1-curl php8.1-mbstring php8.1-zip  php8.1-opcache php8.1-xml
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libflashrom1 libftdi1-2 libllvm13 libwpe-1.0-1 libwpebackend-fdo-1.0-1
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
  libonig5 libzip4 php-common php8.1-common php8.1-readline
Suggested packages:
  php-pear
Creating config file /etc/php/8.1/fpm/php.ini with new version
NOTICE: Not enabling PHP 8.1 FPM by default.
NOTICE: To enable PHP 8.1 FPM in Apache2 do:
NOTICE: a2enmod proxy_fcgi setenvif
NOTICE: a2enconf php8.1-fpm
NOTICE: You are seeing this message because you have apache2 package installed.
Created symlink /etc/systemd/system/multi-user.target.wants/php8.1-fpm.service → /lib/systemd/system/php8.1-fpm.service.
Processing triggers for man-db (2.10.2-1) ...
Processing triggers for libc-bin (2.35-0ubuntu3.8) ...
Processing triggers for php8.1-cli (8.1.2-1ubuntu2.18) ...
Processing triggers for php8.1-fpm (8.1.2-1ubuntu2.18) ...
NOTICE: Not enabling PHP 8.1 FPM by default.
NOTICE: To enable PHP 8.1 FPM in Apache2 do:
NOTICE: a2enmod proxy_fcgi setenvif
NOTICE: a2enconf php8.1-fpm
NOTICE: You are seeing this message because you have apache2 package installed.

Step8: Composer is a vital PHP dependency manager designed to simplify the process of downloading PHP libraries for our project , use the below command

root@linuxhelp:~# curl -ss https://getcomposer.org/installer | php -- --install-dir=/usr/bin --filename=composer
All settings correct for using Composer
Downloading...

Composer (version 2.7.7) successfully installed to: /usr/bin/composer

Step9: Check composer installation by using the below command

root@linuxhelp:~# composer
Do not run Composer as root/super user! See https://getcomposer.org/root for details
Continue as root/super user [yes]? yes
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 2.7.7 2024-06-10 22:11:12

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|--no-ansi           Force (or disable --no-ansi) ANSI output
  -n, --no-interaction           Do not ask any interactive question
      --profile                  Display timing and memory usage information
      --no-plugins               Whether to disable plugins.
      --no-scripts               Skips the execution of all scripts defined in composer.json file.
  -d, --working-dir=WORKING-DIR  If specified, use the given directory as working directory.
      --no-cache                 Prevent use of the cache
  -v|vv|vvv, --verbose           Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Available commands:
  
  self-update          [selfupdate] Updates composer.phar to the latest version
  show                 [info] Shows information about packages
  status               Shows a list of locally modified packages
  suggests             Shows package suggestions
  update               [u|upgrade] Updates your dependencies to the latest version according to composer.json, and updates the composer.lock file
  validate             Validates a composer.json and composer.lock

Step10: Go to document root of the website, in this we utilize default Apache document root using the below command

root@linuxhelp:~# cd /var/www/html

Step11: Create project using the below command and give yes

root@linuxhelp:/var/www/html# composer create-project laravel/laravel linuxhelp-project
Do not run Composer as root/super user! See https://getcomposer.org/root for details
Continue as root/super user [yes]? yes
Creating a "laravel/laravel" project at "./linuxhelp-project"
Cannot use laravel/laravel's latest version v11.1.4 as it requires php ^8.2 which is not satisfied by your platform.
Installing laravel/laravel (v10.3.3)
  - Downloading laravel/laravel (v10.3.3)
  - Installing laravel/laravel (v10.3.3): Extracting archive
Created project in /var/www/html/linuxhelp-project
> @php -r "file_exists('.env') || copy('.env.example', '.env');"
Loading composer repositories with package information
Updating dependencies
Lock file operations: 112 installs, 0 updates, 0 removals
  - Locking brick/math (0.12.1)
  - Locking carbonphp/carbon-doctrine-types (2.1.0)
  - Locking dflydev/dot-access-data (v3.0.3)
  - Locking doctrine/inflector (2.0.10)
  - Locking doctrine/lexer (3.0.1)
  - Locking dragonmantank/cron-expression (v3.3.3)
  - Locking egulias/email-validator (4.0.2)
  - Locking fakerphp/faker (v1.23.1)
  - Locking filp/whoops (2.15.4)
  - Locking fruitcake/php-cors (v1.3.0)
  - Locking graham-campbell/result-type (v1.1.3)
  - Locking guzzlehttp/guzzle (7.9.2)
  - Locking guzzlehttp/promises (2.0.3)
  - Locking guzzlehttp/psr7 (2.7.0)
  - Locking guzzlehttp/uri-template (v1.0.3)
  - Locking hamcrest/hamcrest-php (v2.0.1)
  - Locking laravel/framework (v10.48.18)
  - Locking laravel/pint (v1.17.0)
  - Locking laravel/prompts (v0.1.24)
> @php artisan package:discover --ansi

   INFO  Discovering packages.  

  laravel/sail ................................................................................................................................ DONE
  laravel/sanctum ............................................................................................................................. DONE
  laravel/tinker .............................................................................................................................. DONE
  nesbot/carbon ............................................................................................................................... DONE
  nunomaduro/collision ........................................................................................................................ DONE
  nunomaduro/termwind ......................................................................................................................... DONE
  spatie/laravel-ignition ..................................................................................................................... DONE

83 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
> @php artisan vendor:publish --tag=laravel-assets --ansi --force

   INFO  No publishable resources for tag [laravel-assets].  

No security vulnerability advisories found.
> @php artisan key:generate --ansi

   INFO  Application key set successfully.  

Step12: Switch to the linuxhelp-project using the below command

root@linuxhelp:/var/www/html# cd linuxhelp-project/

Step13: Set these permissions using the below commands

root@linuxhelp:/var/www/html/linuxhelp-project# sudo chown  -R www-data:www-data /var/www/html/linuxhelp-project
 
root@linuxhelp:/var/www/html/linuxhelp-project# sudo chmod -R 775 /var/www/html/linuxhelp-project/storage

Step14: Go to Apache2 site configuration location using the below command

root@linuxhelp:/var/www/html/linuxhelp-project# cd /etc/apache2/sites-available/

Step15: Create new configuration file named laravel.conf using the below command

root@linuxhelp:/etc/apache2/sites-available# touch laravel.conf

Step16: Edit the laravel configuration file using the below command,

root@linuxhelp:/etc/apache2/sites-available# vim laravel.conf

<VirtualHost *:80>
Servername 192.168.6.133
DocumentRoot /var/www/html/linuxhelp-project/public

<Directory /var/www/html/linuxhelp-project>
AllowOverride All
</Directory>

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>
 

Step17: Enable new virtualhost by running the below command

root@linuxhelp:/etc/apache2/sites-available# a2ensite laravel.conf
Enabling site laravel.
To activate the new configuration, you need to run:
  systemctl reload apache2

Step18: Check the syntax using the below command

root@linuxhelp:/etc/apache2/sites-available# apachectl -t
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
Syntax OK

Step19: Reload Apache using the below command

root@linuxhelp:/etc/apache2/sites-available# systemctl reload apache2

Conclusion:

We have reached the end of this article. In this guide, we have walked you through the steps required to install Laravel on Ubuntu 22.04. Your feedback is much welcome.

Tag : Laravel Ubuntu
FAQ
Q
What are Laravel's built-in caching mechanisms?
A
Laravel provides several caching mechanisms, including:
* Memcached
* Redis
* File-based caching (e.g., storing data in a file)
You can use the `Cache` facade to interact with these caching systems.
Q
How do I handle routing in Laravel?
A
In Laravel, routing is handled by the `Route` facade. You can define routes using the `get()`, `post()`,
`put()`, and `delete()` methods to specify HTTP request types and corresponding controller actions.
Q
How do I get started with Laravel?
A
* Install Composer (the PHP package manager) and run `composer create-project --prefer-dist laravel/laravel project-name` to set up a new Laravel project.
* Study the official Laravel documentation and tutorials.
* Start by building small projects to learn the framework.
Q
What are the key features of Laravel?
A
* Modular design with MVC architecture
* Routing system for handling HTTP requests
* ORM (Object-Relational Mapping) for interacting with databases
* Support for caching, queues, and sessions
* Built-in support for RESTful APIs
* Strong focus on security and best practices
Q
What is Laravel?
A
Laravel is an open-source PHP web framework that aims to make building robust and maintainable applications easier. It provides a set of tools and libraries to help developers build web applications quickly andefficiently.