Setting up Laravel application with MongoDB on Linux OS

Shantanu Bajaj
Ortigan
Published in
4 min readFeb 2, 2024

Note: This article is written with respect to Ubuntu OS and Laravel 10 considering you have developed the application on Windows and setting it up on Ubuntu

INTRODUCTION

Hello and Welcome! In this article we’ll learn how to integrate MongoDB database with Laravel Applications in Linux environment. We’ve already seen how to integrate MongoDB with Laravel in Windows environment. To know more about it visit my blog — Laravel MongoDB for Windows

Unlike Windows, it’s a very easy and straightforward process to set up MongoDB in Linux. We’ll see the complete process shortly later in this article. We’ll be using an Ubuntu OS to demonstrate the process.

PREREQUISITES

Before we dive into the integration process, there are a few prerequisites that you need to take care of:

  • You have an Ubuntu OS ready either locally or on any cloud platform(AWS, GCP, DigitalOcean, etc)
  • You have git repository of your Laravel Application up to date unless you are developing on Ubuntu OS🫡

INTEGRATION STEPS

Step-1: Creating a directory for laravel application

Once we’ve logged into our ubuntu terminal, we’ll go to the “var” directory using the following command -

cd /var

Once we’re inside the var folder, we’ll create a new directory named “www” and clone our laravel application inside the “www” folder

mkdir www

If you encounter an error saying “permission denied” make sure you’re running the command as a root user, if not switch the user to root and run the above command again. You can switch to root user by running —

sudo -i

Go to the www folder and clone your laravel application. It is preferred to use ssh keys for cloning instead of https because in future if we want to apply GitHub runner we’ll have to use ssh connection.

Now the question may arise, why to clone the laravel application in /var/www directory only. Well most people use this directory because it’s a standard practice that have been followed since our web server whether nginx or apache, uses this path in their configuration by default. We can use a different directory if we want to but we’ll have to change the server configuration as well.

Now that we’re in our required folder we’ll clone the repository.

git clone git@github.com:Shantanu-Bajaj/blog-content.git

Step-2: Installing required packages and setting up mongodb

We are setting up Laravel 10 with MongoDB, we need php and MongoDB packages and since we have cloned the git repository we have the required packages in our composer.json file so there’s no need to install php separately.

To run the composer file we need to install composer first. We’ll do that by running -

apt-get install composer

If we run composer install first we will get the following error-

To avoid the above error we will first install MongoDB and enable the extension and then proceed with composer.

To install MongoDB we will need the “pecl” command which can be install by running -

apt-get install php-dev php-pear

Then, run the command-

pecl install mongodb

When we run the above command, it asks us for some options. We’ll proceed with the default and install.

After the installation is complete go to the php folder and enable mongodb extension in php.ini file

cd /etc/php/<your-php-version-folder/cli

nano php.ini

At the end of the ini file add line-

extension=mongodb.so

Save and exit the ini file.

Make sure you have the php curl extension installed, if not you’ll get the error while running composer install.

To install php curl extension, run -

apt-get install php-curl

Now go to your laravel application folder and run composer install

cd /folder-name

composer install

If there’s an error saying that lock file is not compatible with the packages, just remove the lock file and run command again

All packages should get installed successfully :)

CONCLUSION

These are the few steps that are required for the integration of MongoDB Eloquent ORM with Laravel 10 in Ubuntu.

If you have any queries or issues feel free to leave a comment.

--

--

Shantanu Bajaj
Ortigan
Writer for

Backend Engineer experienced in handling Laravel Applications