How to Install and Use pip on Ubuntu: Complete Guide to Python Package Management

1. How to Install pip on Ubuntu

In Ubuntu, pip is an essential package management tool for Python. With pip, you can easily manage Python libraries and modules, significantly improving development efficiency. This section explains how to install pip on Ubuntu.

1.1 How to Install pip for Python 3

Although Ubuntu comes with Python 3 preinstalled, pip needs to be added manually. Follow the steps below to install it.

  1. Update the package list
   sudo apt update

This command retrieves the latest package list and updates the system package information.

  1. Install pip
   sudo apt install python3-pip

This installs pip on your system.

  1. Verify the installation
   pip3 --version

Use this command to confirm that pip has been installed correctly.

1.2 How to Install pip for Python 2

Although Python 2 is no longer supported, you can install it in specific environments by following the steps below.

  1. Enable the Universe repository
   sudo add-apt-repository universe
   sudo apt update
  1. Install Python 2 and pip
   sudo apt install python2
   curl https://bootstrap.pypa.io/get-pip.py --output get-pip.py
   sudo python2 get-pip.py

2. What Is pip? Overview of Python Package Management

pip is a tool that allows you to easily install Python libraries and modules from the Python Package Index (PyPI). By handling complex dependencies automatically, pip improves development efficiency.

2.1 Basic Functions of pip

With pip, you can perform operations such as:

  • Installing packages
   pip install <package-name>
  • Uninstalling packages
   pip uninstall <package-name>
  • Upgrading packages
   pip install --upgrade <package-name>

2.2 Benefits of pip

  • Dependency resolution: pip automatically handles inter-package dependencies and manages multiple libraries efficiently.
  • Easily fetch the latest libraries: You can instantly install the latest libraries listed on PyPI.

3. Important Notes When Using pip on Ubuntu

When using pip in an Ubuntu environment, it may conflict with the system package manager (apt). If you want to avoid affecting the entire system, it is recommended to install packages at the user level using the --user option.

3.1 Installing packages with the --user option

pip install --user <package-name>

This installs packages into the user’s home directory without affecting the entire system.

3.2 Handling pip install errors

On Ubuntu 23.04 and later, errors may occur when using pip outside a virtual environment. You can resolve this by creating a virtual environment and installing packages inside it, or by using pipx to install applications.

4. Setting Up Virtual Environments and Using pip

When working on multiple projects that require different libraries, using virtual environments prevents library conflicts. Virtual environments allow each project to maintain separate dependencies, keeping your development environment organized.

4.1 Creating a virtual environment

First, install the venv module and create a virtual environment.

sudo apt install python3-venv
python3 -m venv myenv

4.2 Activating the virtual environment

Run the following command to activate the virtual environment.

source myenv/bin/activate

Once activated, the terminal prompt will display the environment name.

4.3 Managing packages inside the virtual environment

You can install packages inside the virtual environment using the regular pip command.

pip install <package-name>

4.4 Deactivating the virtual environment

Run the following command to exit the virtual environment.

deactivate

5. Troubleshooting: Resolving Issues with pip and Virtual Environments

While using pip and virtual environments, you may encounter certain issues. This section introduces common problems and their solutions.

5.1 If the virtual environment cannot be activated

If activation fails, ensure you are in the correct directory. Use the following command to check whether the activate script exists.

ls /path/to/your/environment/bin

5.2 If packages are not installed correctly

If the virtual environment is not activated, packages may be installed system-wide. Activate the virtual environment and try again.

5.3 Resolving pip installation errors

If you encounter the “externally managed environment” error on Ubuntu 23.04 or later, use a virtual environment or install applications using pipx to resolve the issue.

年収訴求