How to Install Composer on Ubuntu: A Painless Installation Guide linux

When building software, be it a simple or complex project, it is almost given that the software will depend on some external libraries, modules, or components to function properly. These external resources needed for software to work properly are known as dependencies.

Effectively managing dependencies in any project is a crucial process. Luckily, there are different tools designed to automate dependency management and make it easier for developers. As a developer, your tool for dependency management will be influenced by the language and ecosystem you’re working in.

In case you’re building your projects using PHP, you should look no further than Composer as your tool of choice to manage dependencies in your projects.

What is Composer?

Composer is a popular tool that is designed and built for managing dependencies in PHP. Composer, which is greatly inspired by node’s npm and ruby’s bundler, allows you to declare external libraries that your PHP project depends on, and it will manage them for you.

<img alt="What-is-Composer-1" data- data-src="https://kirelos.com/wp-content/uploads/2023/11/echo/What-is-Composer-1.png" data- decoding="async" height="607" src="data:image/svg xml,” width=”602″>

This means that Composer will install all the dependencies needed by your project and also keep them updated.

It is also important to note that Composer is not a package manager like Apt or Yum. As much as it deals with libraries, these libraries are, by default, not installed globally on a machine. Instead, Composer manages dependencies on a per-project basis, and thus it only installs the dependencies locally in a directory inside the PHP project you’re building.

Composer also allows you to find out which versions of which packages can and need to be installed in your project, and it installs them for you. It also provides a single command which you can use to update all the dependencies in your project.

Composer is an essential tool when building projects using PHP. Composer allows for efficient dependency management, and it also provides a repository called Packagist which provides a vast selection of PHP packages and libraries that you can add to your PHP projects as dependencies.

Proper dependency management using a tool such as Composer allows for modular development of software, seamless version control, and enhanced collaboration among teams. Additionally, it helps avoid compatibility issues in software and helps in making projects more maintainable.

Let us now look at how you can install Composer in Ubuntu. But first, let us look at the prerequisites you need to fulfill before you can install Ubuntu.

Prerequisites for Installing Composer in Ubuntu

To install Composer in Ubuntu:

1. Make sure you can access the terminal on your machine and execute commands as the root user.

2. Have PHP version 7.2.5 and above installed on your machine. You can check if you have PHP installed on your machine and its version by opening the terminal and executing

php -version

In case you have PHP installed, you should get such an output:

<img alt="php-version-number" data- data-src="https://kirelos.com/wp-content/uploads/2023/11/echo/php-version-number.png" data- decoding="async" height="181" src="data:image/svg xml,” width=”826″>

In case PHP is not installed, execute the following command to install it:

sudo apt install php

3. Have a version control system. Git is an excellent choice for this. Confirm you have Git installed by running:

git --version

In case Git is installed you’ll get a version number like so:

<img alt="git-version" data- data-src="https://kirelos.com/wp-content/uploads/2023/11/echo/git-version.png" data- decoding="async" height="158" src="data:image/svg xml,” width=”830″>

In case Git is not installed, run the following command to install it

sudo apt install git

4. Have an archive manager installed on your machine to be used in decompressing files. For this, tar will be sufficient. Check if tar is installed on your machine by running:

tar --version
<img alt="tar-version" data- data-src="https://kirelos.com/wp-content/uploads/2023/11/echo/tar-version.png" data- decoding="async" height="236" src="data:image/svg xml,” width=”830″>

In case you don’t have tar, you can install it by running:

sudo apt-get install tar

Once you’ve satisfied the above prerequisites, you’re ready to install Composer.

How To Install Composer on Ubuntu

When installing Composer, you can install it globally on your machine or locally in the context of a PHP installation. A global installation allows you to access and call Composer from any directory on your machine, unlike a local installation.

Installing Composer globally is the recommended way to install Composer, and it is what we will do.

Installing Composer on Ubuntu through the terminal involves running scripts found on Composer’s download page.

Follow the following steps to install Composer globally on Ubuntu:

1. Open the terminal and execute the following command to download Composer’s installer to the current directory.

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"

2. Execute the following command to verify the installer SHA-384.

php -r "if (hash_file('sha384', 'composer-setup.php') === 'e21205b207c3ff031906575712edab6f13eb0b361f2085f1f1237b7126d785e826a450292b6cfd1d64d92e6563bbde02') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

The installer SHA-384 is a cryptographic hash value that is used to verify the integrity of the Composer installer. In other words, it is a security measure to ensure that the installer you downloaded has not been tampered with or corrupted.

Running the above command should generate an output that says Installer verified as shown below. Proceed when you get such an output confirming the integrity of your installer.

<img alt="verify-installer" data- data-src="https://kirelos.com/wp-content/uploads/2023/11/echo/verify-installer.png" data- decoding="async" height="211" src="data:image/svg xml,” width=”829″>

3. Execute the following command to run the installer

php composer-setup.php

You should get such an output confirming that Composer was successfully installed.

<img alt="composer-installed" data- data-src="https://kirelos.com/wp-content/uploads/2023/11/echo/composer-installed.png" data- decoding="async" height="352" src="data:image/svg xml,” width=”826″>

Notice composer.phar, which is the executable file for Composer was added to the directory you installed it on.

4. Since Composer has been successfully installed, run the following command to remove the installer you downloaded earlier as you no longer need it.

php -r "unlink('composer-setup.php');"

5. To ensure that Composer is installed globally and you can call it from any directory, you want to put the composer.phar file into a directory on your PATH. To do this, execute the following command and provide a password when prompted

sudo mv composer.phar /usr/local/bin/composer

6. Verify that Composer was successfully installed by executing the following command:

composer

If Composer was successfully installed, you should get such an output.

<img alt="composer-install-success" data- data-src="https://kirelos.com/wp-content/uploads/2023/11/echo/composer-install-success.png" data- decoding="async" height="516" src="data:image/svg xml,” width=”916″>

How To Use Composer

With Composer installed, the next step is to use it to manage dependencies. As mentioned earlier, Composer has a repository called Packagist which has packages that can be installed as dependencies using Composer.

To see how to use Composer, we are going to install a package called http-message which is an interface that describes HTTP messages.

<img alt="http-message-package" data- data-src="https://kirelos.com/wp-content/uploads/2023/11/echo/http-message-package.png" data- decoding="async" height="567" src="data:image/svg xml,” width=”1007″>

To use Composer to install http-message:

1. Create a new folder called composerDemo and navigate inside it by executing the following commands in the terminal, respectively

mkdir composerDemo
cd composerDemo

2. Create a .gitignore file in the directory by executing:

touch .gitignore

This file allows you to specify which files should not be tracked by Git.

3. Generate a composer.json file for your project by executing:

composer init

A composer.json is a central configuration file used by Composer. In it, you specify metadata that describes your project and also specify the dependencies that your project relies on so that the composer can automatically manage them for you.

The above command leads to the output below:

<img alt="composer-init" data- data-src="https://kirelos.com/wp-content/uploads/2023/11/echo/composer-init.png" data- decoding="async" height="516" src="data:image/svg xml,” width=”916″>

The guide will take you through creating a composer.json file. In the first prompt, press Enter to accept the suggested project name.

4. In the next prompt, provide a short description of the Project and press Enter. You can paste in the short description given below and press Enter.

A demonstration of how to use PHP Composer

5. The next prompt asks for an author; press Enter to use the default.

6. Next, you’ll be asked to specify the Minimum Stability. For that, you can leave it blank by pressing Enter to move to the next prompt.

7. The next prompt requires you to specify a Package Type. For that, enter the project, and press Enter as shown below:

<img alt="composer-package-type" data- data-src="https://kirelos.com/wp-content/uploads/2023/11/echo/composer-package-type.jpg" data- decoding="async" height="405" src="data:image/svg xml,” width=”720″>

8. In the prompt asking for a License, press Enter to leave it blank and move to the next

9. You’ll then be asked whether you’d like to specify your dependencies; for this, press Enter to select Yes. This will then ask you to search for a package.

10. In the search for a package prompt, enter http-message and press enter. This will search for the specified package and return the search results as shown below:

<img alt="package-search-composer" data- data-src="https://kirelos.com/wp-content/uploads/2023/11/echo/package-search-composer.png" data- decoding="async" height="515" src="data:image/svg xml,” width=”916″>

The package we are interested in is the first package called psr/http-message. To select it, enter the number associated with it and press Enter. Therefore, enter 0 (zero) and press Enter.

11. The next prompt is to enter the version constraint to require (or leave blank to use the latest version). For this, we need to specify the version we want to use or leave it blank to use the latest version.

To check the package’s latest version, go to its page on Packagist. The latest version is version 2.0, as shown below:

<img alt="http-message-version" data- data-src="https://kirelos.com/wp-content/uploads/2023/11/echo/http-message-version.png" data- decoding="async" height="446" src="data:image/svg xml,” width=”792″>

Composer automatically updates packages once updates are available. However, you don’t want it to update to a version that is not backward compatible with the version you’re currently using in your project, as it will break your PHP code. To avoid this, enter the following to specify the version of the package and press Enter:

^2.0

This tells Composer to install version 2.0 or higher as long as that higher version is backward compatible.

12. You’ll then be asked to search for another package; simply Press Enter to move to the next prompt and not search for another package.

13. When asked to define your dev dependencies, press Enter. On the search for a package prompt, press Enter to skip it, as our dev dependencies will be the same as our Live dependencies.

14. The next prompt will ask you to add PSR-4 autoload mapping. Press Enter to skip it.

15. This will give you a preview of the composer.json file you just generated, as shown below:

<img alt="confirm-composer-json-file" data- data-src="https://kirelos.com/wp-content/uploads/2023/11/echo/confirm-composer-json-file.png" data- decoding="async" height="475" src="data:image/svg xml,” width=”794″>

Press Enter to confirm the generation.

16. The next prompt asks whether you want to install the dependencies you specified. For this, press Enter to install them as shown below:

<img alt="Screenshot-from-2023-11-07-14-15-58" data- data-src="https://kirelos.com/wp-content/uploads/2023/11/echo/Screenshot-from-2023-11-07-14-15-58.png" data- decoding="async" height="515" src="data:image/svg xml,” width=”915″>

17. In the terminal, enter the following command to open the project you just created in a code editor

code .

The result is shown below:

<img alt="compose-json" data- data-src="https://kirelos.com/wp-content/uploads/2023/11/echo/compose-json.png" data- decoding="async" height="617" src="data:image/svg xml,” width=”1097″>

Notice that a composer.json file was generated, and our dependencies are specified under the key of require. The dependencies installed are installed in a directory called vendor.

18. To avoid tracking the vendor directory, which has all the dependencies, open the .gitignore file and paste the following line.

/vendor/

It is recommended that you don’t track this file because it can get very large, and dependencies can easily be installed automatically from the composer.json file.

19. After creating a composure.json file, you can easily add new dependencies by editing the required section in the composure.json file. For instance, to add a package called log, add it to the required section as shown below:

"require": {
        "psr/http-message": "^2.0",
        "psr/log": "^3.0.0"
    },

20. To install that new dependency in your project, go back to the terminal and execute:

composer update

The result is shown below:

<img alt="composer-update" data- data-src="https://kirelos.com/wp-content/uploads/2023/11/echo/composer-update.png" data- decoding="async" height="480" src="data:image/svg xml,” width=”854″>

This installs the package you just added to your composure.json file.

With that, you’re now ready to start working on your PHP projects and use Composer to manage dependencies in your project.

How To Optimize Composer for Better Package Management

To allow Composer to work more efficiently, ensure you specify version constraints for dependencies accurately. An example of this was when we did “^2.0”.

Avoid very broad version constraints such as “*” which means that Composer can install any version. This can lead to compatibility issues and stability concerns in your project as you can’t predict what version Composer will install.

Additionally, avoid adding excessive custom scripts to your composer.json file. Only add the necessary scripts. Excessive scripts often lead to a huge overhead during dependency management.

To optimize the operation speed of Composer, enable PHP curl extension. You can also use Composer plugins such as hirak/prestissimo, which parallelize Composer operations which in turn speeds up the installation of packages.

You can also clean Composer’s cache to remove old and unused packages to optimize its performance. Additionally, remember not to track and commit the vendor directory where packages are installed by Composer. This will help in speeding up the cloning and updating of your projects’ repositories.

How To Uninstall Composer

If you no longer want to use Composer in Ubuntu, you can uninstall it by executing the following command:

sudo rm -rf /usr/local/bin/composer

You can confirm Composer is successfully uninstalled by executing the following command:

composer

If completely uninstalled, you should get such an output:

<img alt="uinstalled-composer" data- data-src="https://kirelos.com/wp-content/uploads/2023/11/echo/uinstalled-composer.png" data- decoding="async" height="148" src="data:image/svg xml,” width=”855″>

Conclusion

Dependency management is a crucial process when building projects. Efficient dependency management ensures you avoid compatibility issues in your projects and have more maintainable projects.

Additionally, it allows for modular software development and enhanced collaboration among developer teams. If you are building projects in PHP, consider using Composer efficiently and easily manage dependencies in your project.

Next, you may also read about what is Ubuntu PPA and how to install it.