Laravel is a highly popular PHP framework that follows the MVC (Model-View-Controller) architectural pattern. It simplifies the process of web application development with its elegant syntax, modular packaging, and overall convenience.
This article will provide a step-by-step guide on how to install Laravel on Ubuntu 20.04. We will also install all the necessary prerequisites such as PHP, Composer, and other PHP extensions.
Step 1: Install Apache Web Server and PHP
To host our Laravel application, we need a web server. We will use Apache for this purpose. We also need to install PHP because Laravel is a PHP framework. The latest Laravel version required PHP 8.1 or higher version.
To install Apache and PHP, open your terminal and type the following commands:
sudo apt update
sudo add-apt-repository ppa:ondrej/php
sudo apt install apache2 php libapache2-mod-php php-mbstring php-xmlrpc php-soap php-gd php-xml php-cli php-zip
This command will install Apache, PHP, and some additional PHP extensions that Laravel requires.
Step 2: Install MySQL Server (Optional)
Most Laravel applications need a database. We will install MySQL, but you can choose any database system you prefer.
To install MySQL, type the following commands:
sudo apt install mysql-server
sudo mysql_secure_installation
The second command will guide you through a series of prompts to secure your MySQL installation.
Step 3: Install Composer
Composer is a dependency manager for PHP. Laravel requires Composer for managing all its dependencies.
To install Composer, use the following commands:
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
The first command downloads the Composer installer and runs it. The second command moves the composer.phar file to a directory that is in your PATH so you can access it globally.
Step 4: Install Laravel
Now that we have all the prerequisites installed, we can proceed to install Laravel. We’ll install Laravel globally so it can be used on any project.
First, we need to move to the directory where we want our Laravel project to be installed. This is typically in the /var/www/html
directory:
cd /var/www/html
Then, install Laravel using Composer:
sudo composer global require laravel/installer
Step 5: Create a New Laravel Project
Now, you can create a new Laravel project with the following command:
composer create-project --prefer-dist laravel/laravel myProject
Replace “myProject” with the name you want to give to your project.
This command will create a new folder in your current directory with the name of your project, and it will install a fresh Laravel installation in that folder.
Step 6: Configure Apache
In order for Apache to serve your Laravel application, you need to modify its configuration.
First, create a new configuration file:
sudo nano /etc/apache2/sites-available/laravel.conf
Add the following content to the file:
<VirtualHost *:80>
ServerName laravel.example.com
ServerAdmin [email protected]
DocumentRoot /var/www/html/myProject/public
<Directory /var/www/html/myProject>
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Replace “laravel.example.com” with your actual domain, and set the correct Document root.
Then, enable the new configuration and disable the default one:
sudo a2ensite laravel.conf
sudo a2dissite 000-default.conf
To make use of Laravel’s .htaccess file to handle some web server settings, you’ll need to enable Apache’s rewrite module:
sudo a2enmod rewrite
Finally, restart Apache to apply the changes:
sudo systemctl restart apache2
Step 7: Configure Environment Settings
In your project directory, there’s a .env
file which holds all the settings that Laravel needs to run, such as database information, application key, and more. Make sure to update this file according to your environment.
You can make a copy of .env.example and generate secret key with the following commands:
cp .env.example .env
php artisan key:generate
Step 8: Access Laravel Application
You have successfully configured the Laravel PHP framework on your system. Access Laravel application in your favorite web browser
Conclusion
That’s it! You’ve successfully installed Laravel on Ubuntu 20.04. You can now start building your Laravel application. Make sure to check out Laravel’s official documentation to get familiar with what you can do with Laravel.
22 Comments
Thanks for this step by step…It was really helpful to me.
First time installing laravel on my localhost and it went smooth
Step 2 โ Installing Composer.
Why such a difficult decision?
Can be made easier: sudo apt install composer
These commands ensures to install latest composer version on your system.
Thank you very much
Thank you for posting the tutorial. Despite following the instructions accurately, I receive an error when trying to install Laravel from GIT:
fatal: could not create work tree dir ‘laravel’: Permission denied
I tried:
sudo git clone https://github.com/laravel/laravel.git
But this creates other problems (Apache stops running.)
Hi John,
Verify the Apache configuration with command:
sudo apachectl configtest
hello, just change the permission on your newly created laravel directory. If you are using any linux distribution, just move to /var/www then run the commond “chmod 777 .”
From there you should be able to clone laravel from git
*UPDATE*
On the above comment I meant change permission of you /var/www directory and not laravel, coz at this point you dont have laravel directory yet
In PackageManifest.php line 122:
Undefined index: name
Script php artisan optimize handling the post-update-cmd event returned with error code 1
hi, i have the same problem : “UnexpectedValueException
The stream or file โ/var/www/laravel/storage/logs/laravel.logโ could not be opened: failed to open stream: Permission denied” … i use
sudo chmod -R 777 /var/www/laravel/* . then it’s works fine… but i don’t know if using a wildcard “*” is correct. ยฟCan you tell me?
Congrats, greetings from Chile .
Set 777 permission on storage directory not the entire project. Try below commands.
chmod -R 755 /var/www/laravel
chmod -R 777 /var/www/laravel/storage
[Composer\Downloader\TransportException]
The “http://repo.packagist.org/p/fideloper/proxy%243c476aa5bc1f7be4de12519b02c7d32e5
271ecf43f8ad9119472305e52cc7d85.json” file could not be downloaded: failed to open s
tream: HTTP request failed!
help
Thanks a lot! Very useful guide ๐
One note: When installing on Ubuntu 20.* on AWS an additional step can be set as optional:
“apt install zip unzip php-zip”
https://stackoverflow.com/questions/41274829/php-error-the-zip-extension-and-unzip-command-are-both-missing-skipping
Thanks Robert, I have included your suggestion in tutorial.
UnexpectedValueException
The stream or file “/var/www/laravel/storage/logs/laravel.log” could not be opened: failed to open stream: Permission denied
Try: chmod -R 777 /var/www/laravel/storage/logs/
What about chmod 755 ?
After running:
sudo composer install
I was told to not run composer as a super-user. Your documentation may be in need of updates.
Try running the command without, it might say you don’t have the permission. in which case you would need to be root.
Apache mode is not completed in this tutorial, it just opens the default apache page.
I got the Laravel page.
COngratulations from Brasil!!!! VEry good and thanks