How to Install lxml on Ubuntu

The lxml is the Python binding of the libxml2 and libxlt libraries used for processing HTML documents. It is helpful, especially in web scraping techniques to get the particular information, also, it can be used for creating and parsing HTML and XML documents. 

This post explained the lxml python binding installation on Ubuntu using the command line methods. 

What are the Installation Methods of lxml on Ubuntu?

Two different methods can be used for the installation of the lxml on Ubuntu:

  1. Using the Ubuntu’s Default Repository
  2. Using the Pip 

Method 1: Install lxml on Ubuntu Using the Ubuntu’s Default Repository

The recommended method for the installation of the packages on Ubuntu is from its default repository. Ubuntu comes with hundreds of packages in its repository including the lxml package that can be installed by following the steps. 

Step 1: Launch the Terminal

Start the computer and launch the terminal to execute the installation commands:

Step 2: Update the Packages

When the terminal is open, use the “update” option of the apt package manager to update all the packages:

$ sudo apt update

Step 3: Display the lxml Package

To confirm the availability of the lxml package in the default repository, display the package details with the “show” option of the apt package manager:

$ sudo apt show python3-lxml

Step 4: Install the lxml

Run the next command for installing the lxml on Ubuntu:

$ sudo apt install python3-lxml -y

Step 5: Confirm the Installation

Display the version of the installed lxml:

$ python3 -c "import lxml; print('lxml is installed successfully on Ubuntu and its version is', lxml.__version__)"

How to Uninstall the lxml on Ubuntu?

Use the “purge” option of the apt package manager to uninstall the “lxml” on Ubuntu along with all its configuration files:

$ sudo apt purge python3-lxml -y

Method 2: Install lxml on Ubuntu Using the Pip

Pip is the package manager for the Python packages that is used for the installation, upgradation, and removal of the Python packages. To use the pip package manager for installing the lxml on Ubuntu, first install the Pip package:

$ sudo apt install python3-pip -y

Then verify the installation of the Pip by displaying its version:

$ pip --version

Finally, install the lxml with the pip package manager:

$ pip install lxml

To verify the installation, again display the version of the installed lxml package:

$ python3 -c "import lxml; print('lxml is installed successfully on Ubuntu and its version is', lxml.__version__)"

How to Remove the lxml on Ubuntu?

To remove the installed lxml on Ubuntu with the pip package manager, use the command:

$ pip uninstall lxml

Both these packages can be used for the installation of the lxml on Ubuntu. 

Conclusion

The lxml can be installed on Ubuntu by running the command “sudo apt install lxml -y” in its terminal. Another way for installing the lxml on Ubuntu is by using the Pip Python package manager. This post explained the installation of lxml on Ubuntu with the both specified methods.