The authors chooseOpen Sourcing Mental Illness LtdAs aWrite for DOnationsPart of the program accepts donations.

Introduction to the

PHP is a popular server scripting language known for creating dynamic and interactive web pages. Using the language of your choice and getting started is the first step to learning to program.

This tutorial will guide you through installing PHP 7.4 on Ubuntu and setting up a native programming environment from the command line. You will also install a dependency manager, Composer, and test the installation by running a script.

The premise condition

To complete this tutorial, you need a local or virtual machine with Ubuntu 18.04 installed, administrative rights and an Internet connection to the machine. You can download the operating system from the Ubuntu distribution page.

Step 1 – Set up PHP 7.4

You’ll install and set up on the command line, a non-graphical way to interact with your computer. That is, instead of clicking a button, you type in text and receive feedback from the computer through that text.

The command line, also known as a shell or terminal, helps you modify and automate many of the tasks you do on your computer every day and is an important tool for software developers. There are many terminal commands to learn that will enable you to do more powerful things. The article “Introduction to Linux Terminals” will give you a better grasp of terminals.

On Ubuntu, you can find terminal programs by clicking the Ubuntu icon in the upper left corner of the screen and typing Terminal in the search bar. Click on the terminal application icon to open it. Alternatively, you can hit CTRL, ALT and T keys on the keyboard at the same time to automatically open the terminal application.

Avoid relying on the default VERSION of PHP, which can change depending on where your code is running. You may also want to install a different version to match the application you are using, or upgrade to a newer version, such as PHP 8.

Run the following command to update apt-get itself, which ensures you can access the latest version of whatever you want to install.

sudo apt-get update
Copy the code

Next, install software-properties-common, which adds management of other software sources.

sudo apt -y install software-properties-common
Copy the code

The -Y flag will automatically approve the installation. Without this, you will receive a prompt in the terminal window every time you install.

Next, install the version library PPA :ondrej/ PHP, which will give you all the VERSIONS of PHP.

sudo add-apt-repository ppa:ondrej/php
Copy the code

Finally, update apt-get again so that your package manager can see the newly listed packages.

sudo apt-get update
Copy the code

Now you are ready to install PHP 7.4 with the following command.

Sudo apt-y install php7.4Copy the code

Check the installed version.

php -v
Copy the code

You will receive something similar to the following.

OutputPHP 7.4.0beta4 (cli) (built: Aug 28 2019 11:41:49) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0-dev, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.0beta4, Copyright (c), by Zend Technologies
Copy the code

In addition to PHP itself, you may want to install some additional PHP modules. You can use this command to install additional modules, replacing PACKAGE_NAME with the package you want to install.

Sudo apt - get the install php7.4 - PACKAGE_NAMECopy the code

You can also install multiple packages at once. Here are some suggestions for the most common modules you are most likely to install.

Sudo apt-get install -y php7.4-cli php7.4-json php7.4-common php7.4-mysql php7.4-zip php7.4-gd php7.4-mbstring Php7.4 - bcmath php7.4 - curl php7.4 - XMLCopy the code

This command will install the following modules.

  • Php7.4 cli -– Command interpreter, useful for testing PHP scripts from the shell or performing general shell scripting tasks
  • Php7.4 - json– Used to process JSON data
  • Php7.4 - common– PHP documentation, examples, and common modules
  • Php7.4 - mysql– Used to process MySQL database work
  • Php7.4 - zip– Used to process compressed files
  • Php7.4 - gd– For processing images
  • Php7.4 - mbstring– Used to manage non-ASCII strings
  • Php7.4 - curl– Lets you make HTTP requests in PHP
  • Php7.4 - XML– Used to process XML data
  • Php7.4 - bcmath– Used to handle precise floating point numbers

Associated with Apache, PHP configuration is stored in the/etc/PHP / 7.4 / apache2 / PHP ini. You can list all the loaded PHP modules with the following command.

php -m
Copy the code

You have installed PHP and verified the version you are running. Any REQUIRED PHP modules are also installed and the ability to list modules that have been loaded.

You can start using PHP now, but you’ll probably want to use a variety of libraries to quickly build PHP applications. Before testing your PHP environment, first set up a dependency manager for your project.

Step 2 – Set Composer for dependency management (optional)

A library is a collection of code that helps you solve common problems without having to write everything yourself. With many libraries available, using a dependency manager will help you manage multiple libraries as you gain experience writing PHP.

Composer is a dependency management tool in PHP. It allows you to declare the libraries that your project depends on and will manage the installation and update of these packages.

Despite similarities, Composer is not a package manager like YUM or APT. It handles “packages” or libraries, but it manages them on a project-by-project basis, installing them in a directory in your project (e.g., Vendor). By default, it does not install anything globally. Therefore, it is a _ dependency manager _. However, it does support a global project to facilitate the global command.

The idea is not new; Composer is strongly inspired by Node’s NPM and Ruby’s Bundler.

The hypothesis.

  • You have a project that relies on several libraries.
  • Some of these libraries depend on others.

Composer.

  • Enables you to declare libraries that you depend on.
  • Find out which versions of software packages are available and need to be installed, and install them into your project by downloading them.
  • Enables you to update all your dependencies in one command.
  • Enables you to see more details about declared dependencies in the Basic Usage chapter.

In short, there are two ways to install Composer: locally as part of your project, or globally as a system-wide executable. Either way, you’ll start with a local installation.

The local installation

To quickly install Composer in your current directory, run the script on your terminal.

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '756890a4488ce9024fc62c56153228907f1545c228516cbf63f885e036d37e9a59d27d63f46af1d4d07ee0f76181c7d3') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"
Copy the code

The installation script checks some php.ini Settings, warns if they are incorrect, and then downloads the latest composer. Phar in the current directory. The four lines will follow each other.

  • Download the installer to the current directory
  • Verify the installer’s SHA-384. You can also cross-check here
  • Run setup
  • Delete setup

The installer will check some PHP Settings and then download composer. Phar to your working directory. This file is a binary of Composer. It’s a PHAR (PHP archive), which is an archive format for PHP that can be run on the command line, among other things.

To run Composer, you use PHP Composer. Phar. As an example, run this command to see the version of Composer you have installed.

php composer.phar --version
Copy the code

To use Composer locally, you want your Composer. Phar file to be in the root directory of your project. You can start in your project directory before installing Composer. You can also move the file after installation. You can also install Composer to a specific directory using the –install-dir option and (rename) it using the –filename option.

Since Composer is something to use across projects, it is recommended that you proceed to the next section and set Composer to run globally.

On a global scale

You can place the Composer PHAR anywhere you wish. If you put it in a $PATH that belongs to you, you can access it globally. You can even make it run on Ubuntu (and other Unix systems) without directly using the PHP interpreter.

After installing locally, run this command to move composer. Phar to a directory in your path.

sudo mv composer.phar /usr/local/bin/composer
Copy the code

If you want to install it only for your users and avoid the need for root permissions, you can use ~/.local/bin, which is available by default on some Linux distributions.

mv composer.phar ~/.local/bin/composer
Copy the code

Now to run Composer, use Composer instead of PHP Composer. Phar. To check your version of Composer, run.

composer --version
Copy the code

As a final step, you can choose to initialize your project with Composer init. This will create a composer. Json file to manage your project dependencies. Initializing a project also lets you define project details, such as authors and licenses, and use Composer’s autoload feature. You can define dependencies now or add them later.

Run this command to initialize a project.

composer init
Copy the code

Running this command launches the setup wizard. The details you ENTER in the wizard can be updated later, so feel free to leave the defaults and just press ENTER. If you are not ready to install any dependencies, you can choose No. Enter your details at each prompt.

OutputThis command will guide you through creating your composer.json config. Package name (sammy/php_install): sammy/project1 Description []: Author [Sammy <[email protected]>, n to skip]: Minimum Stability []: Package Type (e.g. library, project, metapackage, composer-plugin) []: project License []: Define your dependencies. Would you like to define your dependencies (require) interactively [yes]? no Would you like to  define your dev dependencies (require-dev) interactively [yes]? no { "name": "sammy/project1", "type": "project", "authors": [ { "name": "Sammy", "email": "[email protected]" } ], "require": {} } Do you confirm generation [yes]? yesCopy the code

Before you confirm the generation, you will see a sample of the composer. Json file that the wizard will create. If everything looks good, you can confirm the default yes. If you need to start over, select No.

The first time you define any dependencies, Composer will create a vendor folder. All dependencies are installed in this vendor folder. Composer also creates a Composer. Lock file. This file specifies the exact version of each dependency and subdependency used in the project. This ensures that your program will use the exact same version of the software package when running on any machine.

Note: The Vendor folder should not be committed to your version control system (VCS). The Vendor folder contains only packages you have installed from other vendors. Those individual vendors will maintain their own code in their own version control system. You should only track the code you write. You don’t need to submit the vendor folder, just your composer. Json and composer. Lock files. You can learn more about ignoring specific files in How to Use Git. Learn more about ignoring specific files in A Reference Guide.

Now that YOU have PHP installed and are using Composer to manage your project’s dependencies, you are ready to test your environment.

Step 3 – Test the PHP environment

To test that your system has configured PHP correctly, you can create and run a basic PHP script. Call this script hello.php.

sudo nano hello.php
Copy the code

This will open a blank file. Put the following text in the file, which is valid PHP code.

hello.php


      
echo 'Hello World! ';
? >
Copy the code

Once you have added the text, save and close the file. You can do this by holding the CTRL key and pressing the X key. Then select y and press ENTER.

Now you can test it out and make sure PHP handles your script correctly. Type PHP to tell PHP to process the file, followed by the name of the file.

php hello.php
Copy the code

If PHP handles this properly, you will only see the characters inside quotes.

OutputHello World!
Copy the code

PHP has successfully handled the script, which means your PHP environment has been successfully installed and you can continue your programming journey.

conclusion

At this point, you have set up a PHP 7.4 programming environment on your local Ubuntu machine and are ready to start a coding project.

Before you start coding, you may want to set up an integrated development environment (IDE). While there are many ides to choose from, VS Code is a popular choice because it offers powerful features such as graphical interfaces, syntax highlighting, and debugging.

When your local machine is ready for software development, you can continue to learn more about PHP coding by learning how to Use Strings in PHP.