Home » Installation » How to Install Laravel in Ubuntu 22.04

How to Install Laravel in Ubuntu 22.04

Frameworks and libraries did programming faster and easier. There are a lot of frameworks for PHP, but Laravel is most popular now. It has a fast request handler, database query builder, powerful CLI utility, comfortable wrappers for standard library functions, queue and caching mechanisms, and many thrid-party packages from thousands of developers.

In this article I will explain how to install Laravel in Ubuntu 22.04 with Composer package manager. When working with this framework you must have installed any database, for example – MariaDB or MySQL, as well as a web server, for example, Nginx or Apache. Also, if you want to build JS assets, you should have installed NodeJS.

How to Install Laravel in Ubuntu 22.04

1. Install Composer

If you have not installed Composer, you should install it using the command below:

sudo apt install composer

Also you can get the newest version of the composer from the official site using commands:

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" php composer-setup.php sudo mv ./composer.phar /usr/local/bin/composer

After that you can check the Composer version:

composer --version

2. Install PHP Extensions

I am assuming you already have PHP installed but you should install a few PHP extensions for Laravel:

sudo apt install php-ctype php-fileinfo php-mysql php-mbstring php-tokenizer php-xml php-json php-common php-curl

3. Create Project

Laravel not only has library packages but project boilerplate code. You should use it to create a new project. First, create a project folder, for example in /var/www/:

sudo mkdir /var/www/laraproject

Then, make it the current working directory:

cd /var/www/laraproject

Install the boilerplate and all Laravel dependencies in the current directory using the command below:

composer create-project --prefer-dist laravel/laravel ./

Also you can specify a directory name for a new project. For example, blog:

composer create-project --prefer-dist laravel/laravel blog

By default, you will get latest Laravel version. At the moment this is version 9.2. If you want to install a specific version, use colon syntax after the package name. You can find available versions at packagist.org. For example, for version 8.0, run:

composer create-project --prefer-dist laravel/laravel:9:0 ./

4. Configuring Encryption Key

Laravel uses encryption to secure sensitive data, and you can use encryption in Eloquent query builder. You should generate an encryption key to get it working:

php artisan key:generate --ansi

5. Permissions

Laravel needs permissions for writing in the ./storage directory. If you want to use Laravel from multiple users would be better change permissions to 777:

sudo chmod 777 -R ./storage

Also, you should change permission for log files. Open config/logging.php and find the channels section. Then add a permission attribute for all file channels. For example:

vi config/logging.php return [ //... "channels" => [ [ "driver" => "single", "path" => storage_path("logs/laravel.log"), "permission" => 0777, ], //..... ], ];

6. Configuring Database

All environment-specific Laravel settings are locating in the .env file. You should make a few changes in this file before you start using Laravel. First, you must configure the database. First, create a user and database in MySQL CLI:

sudo mysql -u root -p CREATE DATABASE laravel; CREATE USER `laravel_user`@`localhost` IDENTIFIED BY 'password'; GRANT ALL ON laravel.* TO `laravel_user`@`localhost`;

Then, open the .env file and find lines below and fill them with database auth data:

vi .env DB_CONNECTION=mysql DB_HOST= DB_PORT=3306 DB_DATABASE=laravel DB_USERNAME=laravel-user DB_PASSWORD=password

After this, you can run migrations:

php artisan migrate

7. Generate Auth

If you want to use the default auth scaffolding in Laravel, you should install the laravel/ui package using Сomposer:

composer require laravel/ui

Then generate authentication pages and routes:

php artisan ui vue --auth

After that you should generate js asserts with NodeJS using the command:

npm install && npm run dev

Next, add auth routes handler in routes/web.php file:

vi routes/web.php Auth::routes();

8. Serve Laravel site

Laravel has a lightweight built-in web server, that can be used for development purposes. You can run it using the command:

php artisan serve

After that, you can open the site in the browser:

If you want to serve a Laravel website with Nginx, you need to have Nginx and PHP-FPM installed. Create the virtual host config in /etc/nginx/conf.d/laravelapp.conf and paste the lines shown below:

sudo vi /etc/nginx/conf.d/laravelapp.conf server { listen 8080; listen [::]:8080; server_name localhost root /var/www/laraproject/public; index index.php; location / { try_files $uri $uri/ /index.php?$query_string; } location ~ \.php$ { fastcgi_pass; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include /etc/nginx/fastcgi_params; } }

Feel free to change server_name, port, root, and fastcgi_pass to suit your needs. Note, that the value of the root parameter must point to the public folder. Also, for Nginx, all files in the project folder must be owned by user www-data. This is the default user that Nginx is ran as.

sudo chown www-data:www-data -R /var/www/laraproject

Then restart Nginx:

sudo systemctl restart nginx

After this, you can open the site in your browser. In this example Nginx will listen for this site at and port 8080:

Wrapping Up

In this article we explained how to install Laravel in Ubuntu 22.04. Now you can enjoy your new Laravel project. After this instruction you can register or log in on your site. Have any questions? Use comment form below.

Your Reaction

Leave a Comment