How to Install Apache, PHP 8, MySQL and Laravel on Ubuntu with Vagrant in 2024

Sagardhiman
4 min readFeb 29, 2024
How to Install Apache, PHP 8, MySQL and Laravel on Ubuntu with Vagrant in 2024
Source: Google

In this tutorial, we will discuss how we install LAMP server and Laravel in Virtual Machine in Ubuntu.
Before installing the setup let’s discuss about pre required tools we need.

Oracle VM:

In simple terms, Oracle VM (Virtual Machine) is a software solution that allows you to run multiple operating systems on a single physical computer. It creates virtual machines, which are like separate computers within your actual computer.
These virtual machines can run different operating systems, independent of the host operating system.

Imagine having one physical computer, and on that computer, you can run multiple virtual computers, each with its operating system. Oracle VM helps with server consolidation, resource optimization, and easier management of multiple operating systems on a single machine.
It’s commonly used in data centers and enterprise environments to make better use of hardware resources and improve flexibility in running various applications.

So before starting firstly we need to install some tools as below:

Install Chocolatey Follow the below link instructions:

After installing chocolatey open the window power shell in Administrator mode and run the below commands:

choco install virtualbox --version=7.0.8 -y

choco install vagrant --version=2.3.7 -y

choco install git -y

choco install vscode -y

Go to a drive and create a folder through git bash named vagrant-vms(you can create a folder with some other name as well) and inside this folder create a folder named ubuntu because in this session we are installing a ubuntu image.

Firstly go to the Vagrant cloud

In the search box enter the image name “ubuntu/jammy64”, after that go to the folder created in the previous step, Let’s consider we create our VM inside D drive so the required path is D/vagrant-vms/ubuntu after that open the git bash and run the command

vagrant init "ubuntu/jammy64"
VM running

The above command creates a Vagrant file which consists of configuration code here we do not need to focus on it more, just need to remember about some change made in the file as discussed below, copy the below content into your own Vagrant File.

Vagrant File content

After that run the vagrant reload command it will refresh the system
then paste the below URL into the browser

http://192.168.56.14

It will show a page that confirm that Apache is installed in the Machine

The next step is to install PHP and MySQL, for this just run the below commands.

Firstly go to the folder where you install the Ubuntu machine using the git bash and after that run the vagrant SSH, which connect your local machine to the virtual machine.

After that run the below commands to install the MySQL and PHP

PHP:

 sudo apt-add-repository ppa:ondrej/php

sudo apt-get update

sudo apt-get install php8.2

and at last check the php version with the help of below command

php -v

Mysql installation:

 sudo apt-get install mysql-server php7.1-mysql

sudo mysql_secure_installation

sudo mysql -u root

CREATE DATABASE DBNAME;

CREATE USER admin@localhost IDENTIFIED BY '<your-password>';

GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,ALTER ON DBNAME.*admin@localhost;

FLUSH PRIVILEGES;

After that to test that your machine has successfully installed php just create a file in the machine and go to the http://192.168.56.14 URL in the browser and browse the php file you have created. For eg just create an index.php file and simply write an echo statement.

Sync Folders From Local Machine to Virtual Machine

At last just run the below command to install the composer in the VM.

curl -sS https://getcomposer.org/installer -o composer-setup.php

sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer

After that write composer in the git bash and it will show that composer is installed successfully at the end just install the Laravel through composer, it will create a laravel project in the VM.

Then edit the .env file and add the DB name, username and password of your database and hit the commands

php artisan serve --port=8001

After that your laravel project run at port.

 http://192.168.56.14:8001

Conclusion:

This tutorial is the brief introduction through which you are able to create a Virtual Machine through Vagrant and install LAMP server into it and also Install Laravel project into it.

If you have any doubts in the above tutorial and also learn about certain topics of Php, Laravel, Javascript, Mysql in detail, feel free to comment below and subscribe to the newsletter for latest updates.

🤝🏻 Connect with Me on LinkedIn, Do also Follow on Twitter

--

--