The author selected mở cửa Sourcing Mental Illness Ltd lớn receive a donation as part of the Write for DOnations program. Bạn đang xem: How to install php 7
Introduction
PHP is a popular hệ thống scripting language known for creating dynamic and interactive website pages. Getting up và running with your language of choice is the first step in learning lớn program.
This tutorial will guide you through installing PHP 7.4 on Ubuntu and setting up a local programming environment via the command line. You will also install a dependency manager, Composer, and test your installation by running a script.
Prerequisites
To complete this tutorial, you will need a local or virtual machine with Ubuntu 20.04 installed và have administrative access and an internet connection to that machine. You can download this operating system via the Ubuntu releases page.
Step 1 — Setting Up PHP 7.4
You’ll be completing your installation and setup on the command line, which is a non-graphical way to interact with your computer. That is, instead of clicking on buttons, you’ll be typing in text and receiving feedback from your computer through text as well.
The command line, also known as a shell or terminal, can help you modify & automate many of the tasks you vị on a computer every day and is an essential tool for software developers. There are many terminal commands khổng lồ learn that can enable you to vì chưng more powerful things. The article An Introduction khổng lồ the Linux Terminal can get you better oriented with the terminal.
On Ubuntu, you can find the Terminal application by clicking on the Ubuntu icon in the upper-left-hand corner of your screen and typing terminal into the tìm kiếm bar. Click on the Terminal application icon to open it. Alternatively, you can hit the CTRL, ALT, & T keys on your keyboard at the same time to xuất hiện the Terminal application automatically.

Note: Ubuntu 20.04 ships with PHP 7.4 in its upstream repositories. This means that if you attempt khổng lồ install PHP without a specified version, it will use 7.4.
You will want to lớn avoid relying on the mặc định version of PHP because that default version could change depending on where you are running your code. You may also wish lớn install a different version khổng lồ match an application you are using or to upgrade to a newer version, such as PHP 8.
Run the following command to update apt-get itself, which ensures that you have access to lớn the latest versions of anything you want to install:
sudo apt-get updateNext, install software-properties-common, which adds management for additional software sources:
sudo apt -y install software-properties-commonThe -y flag will automatically agree to lớn the installation. Without that, you would receive a prompt in your terminal window for each installation.
Next, install the repository ppa:ondrej/php, which will give you all your versions of PHP:
sudo add-apt-repository ppa:ondrej/phpFinally, you update apt-get again so your package manager can see the newly listed packages:
sudo apt-get updateNow you’re ready lớn install PHP 7.4 using the following command:
sudo apt -y install php7.4Check the version installed:
php -vYou will receive something similar to the following:
Output
sudo apt-get install php7.4-PACKAGE_NAMEYou can also install more than one package at a time. Here are a few suggestions of the most common modules you will most likely want 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-curl php7.4-xml php7.4-bcmathThis command will install the following modules:
php7.4-cli - command interpreter, useful for testing PHP scripts from a shell or performing general shell scripting tasksphp7.4-json - for working with JSON dataphp7.4-common - documentation, examples, & common modules for PHPphp7.4-mysql - for working with MySQL databasesphp7.4-zip - for working with compressed filesphp7.4-gd - for working with imagesphp7.4-mbstring - used lớn manage non-ASCII stringsphp7.4-curl - lets you make HTTP requests in PHPphp7.4-xml - for working with XML dataphp7.4-bcmath - used when working with precision floatsPHP configurations related khổng lồ Apache are stored in /etc/php/7.4/apache2/php.ini. You can list all loaded PHP modules with the following command:
php -mYou have installed PHP & verified the version you have running. You also installed any required PHP modules và were able to list the modules that you have loaded.
You could start using PHP right now, but you will likely want to use various libraries to lớn build PHP applications quickly. Before you thử nghiệm your PHP environment, first mix up a dependency manager for your projects.
Xem thêm: Liệu Sóng Wifi Có Ảnh Hưởng Đến Sự Phát Triển Của Trẻ Sơ Sinh?
Step 2 — Setting Up Composer for Dependency Management (Optional)
Libraries are a collection of code that can help you solve common problems without needing khổng lồ write everything yourself. Since there are many libraries available, using a dependency manager will help you manage multiple libraries as you become more experienced in writing PHP.
Composer is a tool for dependency management in PHP. It allows you to lớn declare the libraries your project depends on and will manage installing và updating these packages.
Although similar, Composer is not a package manager in the same sense as yum or apt. It giao dịch with “packages” or libraries, but it manages them on a per-project basis, installing them in a directory (e.g. Vendor) inside your project. By default, it does not install anything globally. Thus, it is a dependency manager. It does, however, tư vấn a global project for convenience via the global command.
This idea is not new, và Composer is strongly inspired by Node’s npm và Ruby’s bundler.
Suppose:
You have a project that depends on several libraries.Some of those libraries depend on other libraries.Composer:
Enables you to declare the libraries you depend on.Finds out which versions of which packages can & need khổng lồ be installed & installs them by downloading them into your project.Enables you lớn update all your dependencies in one command.Enables you to lớn see the Basic Usage chapter for more details on declaring dependencies.There are, in short, two ways to lớn install Composer: locally as part of your project or globally as a system-wide executable. Either way, you will start with the local install.
Locally
To quickly install Composer in the current directory, run this script in 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.phpphp -r "unlink("composer-setup.php");"This installer script will kiểm tra some php.ini settings, warn you if they are phối incorrectly, và then download the latest composer.phar in the current directory. The four lines will, in order:
Download the installer lớn the current directoryRun the installerRemove the installerThe installer will check a few PHP settings and then download composer.phar to lớn your working directory. This file is the Composer binary. It is a PHAR (PHP archive), which is an archive format for PHP that can be run on the command line, amongst other things.
In order lớn run Composer, you use php composer.phar. As an example, run this command lớn see the version of Composer you have installed:
php composer.phar --versionTo use Composer locally, you will want your composer.phar file to be in your project’s root directory. You can start in your project directory before installing Composer. You can also move the file after installation. You can also install Composer khổng lồ a specific directory by using the --install-dir option và additionally (re)name it using the --filename option.
Since Composer is something used across projects, it’s recommended that you continue to the next portion and set Composer lớn run globally.
Globally
You can place the Composer PHAR anywhere you wish. If you put it in a directory that is part of your $PATH, you can access it globally. You can even make it executable on Ubuntu (and other Unix systems) & invoke it without directly using the PHP interpreter.
After installing locally, run this command khổng lồ move composer.phar to lớn a directory that is in your path:
sudo mv composer.phar /usr/local/bin/composerIf you’d like to install it only for your user và avoid requiring root permissions, you can use ~/.local/bin instead, which is available by default on some Linux distributions:
mv composer.phar ~/.local/bin/composerNow lớn run Composer, use composer instead of php composer.phar. To check for your Composer version, run:
composer --versionAs a final step, you may optionally initialize your project with composer init. This will create the composer.json tệp tin that will manage your project dependencies. Initializing the project will also let you define project details such as Author & License, and use Composer’s autoload functionality. You can define dependencies now or add them later.
Run this command to lớn initialize a project:
composer initRunning this command will start the thiết đặt wizard. Details that you enter in the wizard can be updated later, so feel không tính tiền to leave the defaults and just press ENTER. If you aren’t ready lớn install any dependencies, you can choose no. Enter in your details at each prompt: