Installing packages in any Linux-based Operating system is like a task that happens on a daily basis, and it is seen that we often need to install some packages for a brief period. However, the packages required for very little time stay inside our system and consume the disk space.

In a system where we have fewer resources in terms of disk space, the disk space stays almost full, and we have to keep check on unwanted applications and packages and remove them from time to time.

For such scenarios where you have to install and use some packages for a short period, it is a better practice and recommended to install the packages in some specific or temporary directory. The directory will stay separate from all other package clusters, and you can easily remove the packages.

Yum vs. DNF

Yum, an abbreviation of “Yellow Updater modified,” is a primary package manager for installing packages in CentOS or any RHEL-based operating system. While DNF, shortened for “Dandified Yum,” is the latest and feature-enriched package manager compared to Yum.

In the latest versions of the RHEL-based operating system, it is recommended to use DNF instead of Yum. This post will provide a detailed guide on installing a package to a specific directory using Yum and DNF package manager.

The syntax for installing a package:

Usually, we install a package using the syntax provided below:

$ sudo dnf <options> install <package-name>

The option we can use for installing a package to a specific directory is:

In the option mentioned above, we can provide a specific directory path where we want to install the package.

Let’s try to install a package to demonstrate how to install a package to a specific directory.

Example:

For example, we want to install git in some specific directory like /opt/temp-packages.

Let’s first create a “temp-packages” directory inside the /opt directory in which we want to install Git.

$ sudo mkdir temp-packages

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image6-35.png" data-lazy- height="49" src="data:image/svg xml,” width=”479″>

To install git in the /opt/temp-packages directory, we will provide the path to the –installroot option in the yum install command as shown below:

$ sudo dnf –installroot=/opt/temp-packages –releasever=/ install git

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image8-23.png" data-lazy- height="147" src="data:image/svg xml,” width=”727″>

In the above command, we also need to use the –releasever option while creating installroot. Otherwise, we will have to face an error as shown in the screenshot attached below:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image7-28.png" data-lazy- height="132" src="data:image/svg xml,” width=”725″>

So, execute the installation command with the –releasever=/ option.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image10-15.png" data-lazy- height="398" src="data:image/svg xml,” width=”722″>

Type ‘y’ for granting additional disk space usage and begin the installation process of the package you want to install.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image9-21.png" data-lazy- height="279" src="data:image/svg xml,” width=”368″>

In the above screenshot attached, you can witness the installation of Git in the /opt/temp-packages directory.

Once you are done with the installation of the package, head to the /opt/temp-packages directory and execute the ls command to view the files and directories in the /opt/temp-packages directory:

$ cd /opt/temp-packages


$ ls

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image2-44.png" data-lazy- height="77" src="data:image/svg xml,” width=”507″>

You can see that a completely separate environment is created inside the /opt/temp-packages directory.

To use any package installed in a specific directory, we can move to the /usr/bin directory and run it because we all know that the packages are installed in the /usr/bin directory. So, navigate to the usr/bin directory, which is inside the /opt/temp-packages, using the command given below:

$ cd /opt/temp-packages/usr/bin

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image1-43.png" data-lazy- height="37" src="data:image/svg xml,” width=”621″>

In the bin directory, execute the ls and grep command to verify if the desired package is installed or not. The command for finding and verifying the package installation will go like this:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image4-43.png" data-lazy- height="131" src="data:image/svg xml,” width=”408″>

To verify and know the installed version of Git, you can type the command provided below in the bin directory:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image3-44.png" data-lazy- height="58" src="data:image/svg xml,” width=”388″>

Or you can also verify the installation of Git by executing the simple ./git command in the /opt/temp-packages/usr/bin directory:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image5-38.png" data-lazy- height="338" src="data:image/svg xml,” width=”718″>

You can witness that git is installed successfully in the /opt/temp-packages directory and working perfectly fine as we want it to work.

Conclusion

This post contains a brief and detailed explanation on how to install a package to a specific directory using the Yum or DNF package manager by using the –installroot option. This post also demonstrates the whole process of installing a package to a specific directory and using that package. As an example, we installed Git in the “/opt” directory using the –installroot option and learned how to start using it.

About the author

<img alt="" data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/7011409B3A384F43A8417D1DAC68D179.jpg60cd1575926a9.jpg" height="112" src="data:image/svg xml,” width=”112″>

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.