Complete Guide to Installing pip and Using Virtual Environments on Ubuntu | Step-by-Step for Beginners

1. How to Install pip on Ubuntu

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

1.1 Installing pip for Python 3

Although Python 3 comes pre-installed on Ubuntu, pip must be installed manually. Follow these steps to install it:

  1. Update the package list
   sudo apt update

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

  1. Install pip
   sudo apt install python3-pip

This will install pip.

  1. Verify the installation
   pip3 --version

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

1.2 Installing pip for Python 2

Python 2 has reached the end of its support, but if you still need it for specific environments, you can install it using the following steps:

  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’s Package Management Tool

pip is a tool that allows you to easily install Python libraries and modules from PyPI (Python Package Index). It simplifies dependency management, improving development efficiency.

2.1 Basic Functions of pip

With pip, you can perform the following operations:

  • Install a package
   pip install <package-name>
  • Uninstall a package
   pip uninstall <package-name>
  • Upgrade a package
   pip install --upgrade <package-name>

2.2 Benefits of Using pip

  • Dependency Resolution: pip automatically handles package dependencies, allowing you to manage multiple libraries efficiently.
  • Easy Access to the Latest Libraries: You can quickly install the latest libraries available on PyPI.

3. Important Considerations When Using pip on Ubuntu

When using pip on Ubuntu, there is a possibility of conflicts with the system’s package manager (apt). To prevent system-wide changes, it is recommended to use the --user option for user-level installations.

3.1 Installing Packages with the --user Option

pip install --user <package-name>

This ensures that packages are installed in the user’s home directory instead of affecting the entire system.

3.2 Troubleshooting pip install Errors

On Ubuntu 23.04 and later, using pip outside of a virtual environment may result in errors. You can resolve this issue by creating a virtual environment or using pipx to install applications.

4. Setting Up a Virtual Environment and Using pip

When working on multiple projects with different libraries, using virtual environments can help prevent conflicts between dependencies. Virtual environments allow each project to maintain its own 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

To activate the virtual environment, run the following command:

source myenv/bin/activate

Once activated, the virtual environment’s name will appear in the terminal prompt.

4.3 Managing Packages Inside a Virtual Environment

You can use the regular pip commands to install packages within a virtual environment.

pip install <package-name>

4.4 Deactivating the Virtual Environment

To exit the virtual environment, use the following command:

deactivate

5. Troubleshooting: Resolving Issues with pip and Virtual Environments

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

5.1 When the Virtual Environment Fails to Activate

If you are unable to activate the virtual environment, first check if you are in the correct directory. You can verify the presence of the activate script with the following command:

ls /path/to/your/environment/bin

5.2 When Packages Are Not Installed Correctly

If packages are not installed properly, it may be because the virtual environment is not activated. Try activating the virtual environment and reinstalling the package.

5.3 Resolving Errors When Installing pip

On Ubuntu 23.04 and later, you may encounter an “externally managed environment” error. This issue can be resolved by using a virtual environment or installing applications via pipx.

侍エンジニア塾