如何在 Ubuntu 上安装 pip
pip Python
**`pip` 是一个命令行工具，允许你安装 Python 编写的软件包。 学习如何在 Ubuntu 上安装 `pip` 以及如何使用它来安装 Python 应用程序。**


有许多方法可以[在 Ubuntu 上安装软件](https://itsfoss.com/how-to-add-remove-programs-in-ubuntu/)。 你可以从软件中心安装应用程序，也可以从下载的 DEB 文件、PPA（LCTT 译注：PPA 即 Personal Package Archives，个人软件包集）、[Snap 软件包](https://itsfoss.com/use-snap-packages-ubuntu-16-04/)，也可以使用 [Flatpak](https://itsfoss.com/flatpak-guide/)、使用 [AppImage](https://itsfoss.com/use-appimage-linux/)，甚至用旧的源代码安装方式。


还有一种方法可以在 [Ubuntu](https://www.ubuntu.com/) 中安装软件包。 它被称为 `pip`，你可以使用它来安装基于 Python 的应用程序。


### 什么是 pip


[pip](https://en.wikipedia.org/wiki/pip_(package_manager)) 代表 “pip Installs Packages”。 [pip](https://pypi.org/project/pip/) 是一个基于命令行的包管理系统。 用于安装和管理 [Python 语言](https://www.python.org/)编写的软件。


你可以使用 `pip` 来安装 Python 包索引（[PyPI](https://pypi.org/)）中列出的包。


作为软件开发人员，你可以使用 `pip` 为你自己的 Python 项目安装各种 Python 模块和包。


作为最终用户，你可能需要使用 `pip` 来安装一些 Python 开发的并且可以使用 `pip` 轻松安装的应用程序。 一个这样的例子是 [Stress Terminal](https://itsfoss.com/stress-terminal-ui/) 应用程序，你可以使用 `pip` 轻松安装。


让我们看看如何在 Ubuntu 和其他基于 Ubuntu 的发行版上安装 `pip`。


### 如何在 Ubuntu 上安装 pip


![Install pip on Ubuntu Linux](/data/attachment/album/201810/13/110003y07b0mimehuw727z.png)


默认情况下，`pip` 未安装在 Ubuntu 上。 你必须首先安装它才能使用。 在 Ubuntu 上安装 `pip` 非常简单。 我马上展示给你。


Ubuntu 18.04 默认安装了 Python 2 和 Python 3。 因此，你应该为两个 Python 版本安装 `pip`。


`pip`，默认情况下是指 Python 2。`pip3` 代表 Python 3 中的 pip。


注意：我在本教程中使用的是 Ubuntu 18.04。 但是这里的教程应该适用于其他版本，如Ubuntu 16.04、18.10 等。你也可以在基于 Ubuntu 的其他 Linux 发行版上使用相同的命令，如 Linux Mint、Linux Lite、Xubuntu、Kubuntu 等。


#### 为 Python 2 安装 pip


首先，确保已经安装了 Python 2。 在 Ubuntu 上，可以使用以下命令进行验证。



```
python2 --version
```

如果没有错误并且显示了 Python 版本的有效输出，则说明安装了 Python 2。 所以现在你可以使用这个命令为 Python 2 安装 `pip`：



```
sudo apt install python-pip
```

这将安装 `pip` 和它的许多其他依赖项。 安装完成后，请确认你已正确安装了 `pip`。



```
pip --version
```

它应该显示一个版本号，如下所示：



```
pip 9.0.1 from /usr/lib/python2.7/dist-packages (python 2.7)
```

这意味着你已经成功在 Ubuntu 上安装了 `pip`。


#### 为 Python 3 安装 pip


你必须确保在 Ubuntu 上安装了 Python 3。 可以使用以下命令检查一下：



```
python3 --version
```

如果显示了像 Python 3.6.6 这样的数字，则说明 Python 3 在你的 Linux 系统上安装好了。


现在，你可以使用以下命令安装 `pip3`：



```
sudo apt install python3-pip
```

你应该使用以下命令验证 `pip3` 是否已正确安装：



```
pip3 --version
```

它应该显示一个这样的数字：



```
pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.6)
```

这意味着 `pip3` 已成功安装在你的系统上。


### 如何使用 pip 命令


现在你已经安装了 `pip`，让我们快速看一些基本的 `pip` 命令。 这些命令将帮助你使用 `pip` 命令来搜索、安装和删除 Python 包。


要从 Python 包索引 PyPI 中搜索包，可以使用以下 `pip` 命令：



```
pip search <search_string>
```

例如，如果你搜索“stress”这个词，将会显示名称或描述中包含字符串“stress”的所有包。



```
pip search stress
stress (1.0.0) - A trivial utility for consuming system resources.
s-tui (0.8.2) - Stress Terminal UI stress test and monitoring tool
stressypy (0.0.12) - A simple program for calling stress and/or stress-ng from python
fuzzing (0.3.2) - Tools for stress testing applications.
stressant (0.4.1) - Simple stress-test tool
stressberry (0.1.7) - Stress tests for the Raspberry Pi
mobbage (0.2) - A HTTP stress test and benchmark tool
stresser (0.2.1) - A large-scale stress testing framework.
cyanide (1.3.0) - Celery stress testing and integration test support.
pysle (1.5.7) - An interface to ISLEX, a pronunciation dictionary with stress markings.
ggf (0.3.2) - global geometric factors and corresponding stresses of the optical stretcher
pathod (0.17) - A pathological HTTP/S daemon for testing and stressing clients.
MatPy (1.0) - A toolbox for intelligent material design, and automatic yield stress determination
netblow (0.1.2) - Vendor agnostic network testing framework to stress network failures
russtress (0.1.3) - Package that helps you to put lexical stress in russian text
switchy (0.1.0a1) - A fast FreeSWITCH control library purpose-built on traffic theory and stress testing.
nx4_selenium_test (0.1) - Provides a Python class and apps which monitor and/or stress-test the NoMachine NX4 web interface
physical_dualism (1.0.0) - Python library that approximates the natural frequency from stress via physical dualism, and vice versa.
fsm_effective_stress (1.0.0) - Python library that uses the rheological-dynamical analogy (RDA) to compute damage and effective buckling stress in prismatic shell structures.
processpathway (0.3.11) - A nifty little toolkit to create stress-free, frustrationless image processing pathways from your webcam for computer vision experiments. Or observing your cat.
```

如果要使用 `pip` 安装应用程序，可以按以下方式使用它：



```
pip install <package_name>
```

`pip` 不支持使用 tab 键补全包名，因此包名称需要准确指定。 它将下载所有必需的文件并安装该软件包。


如果要删除通过 `pip` 安装的 Python 包，可以使用 `pip` 中的 `uninstall` 选项。



```
pip uninstall <installed_package_name>
```

你可以在上面的命令中使用 `pip3` 代替 `pip`。


我希望这个快速提示可以帮助你在 Ubuntu 上安装 `pip`。 如果你有任何问题或建议，请在下面的评论部分告诉我。




---


via: <https://itsfoss.com/install-pip-ubuntu/>


作者：[Abhishek Prakash](https://itsfoss.com/author/abhishek/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[Flowsnow](https://github.com/Flowsnow) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
![Warp Terminal](/assets/images/warp-terminal.webp)


![Warp Terminal](/assets/images/warp-terminal.webp)

**Summary**: To install PIP on Ubuntu, you should make sure to enable universe repository and then install

`python3-pip`

package like this:**sudo add-apt-repository universe**

**sudo apt install python3-pip**

There are numerous ways to [install software on Ubuntu](https://itsfoss.com/remove-install-software-ubuntu/). You can install applications from the software center, from downloaded deb files, from PPAs, from [Snap packages](https://itsfoss.com/use-snap-packages-ubuntu-16-04/), [using Flatpak](https://itsfoss.com/flatpak-guide/), using [AppImage](https://itsfoss.com/use-appimage-linux/) and even from the good old source code.

Here’s another way to install packages on [Ubuntu](https://www.ubuntu.com/?ref=itsfoss.com). It’s called PIP and you can use it to install Python-based applications.

## What is pip?

[Pip](https://en.wikipedia.org/wiki/Pip_(package_manager)?ref=itsfoss.com) stands for “Pip Installs Packages”. [Pip](https://pypi.org/project/pip/?ref=itsfoss.com) is a command-line based package management system. It’s used to install and manage software written in the [Python language](https://www.python.org/?ref=itsfoss.com). You can use pip to install packages listed in the Python Package Index ([PyPI](https://pypi.org/?ref=itsfoss.com)).

**As a software developer**, you can use pip to install various Python modules and packages for your own Python projects.

**As an end user**, you may need pip for installing some applications that are developed using Python and can be installed easily using pip. One such example is the [Stress Terminal](https://itsfoss.com/stress-terminal-ui/) application, which you can easily install with pip.

Let’s see how you can install pip on Ubuntu and other Ubuntu-based distributions.

## How to install pip on Ubuntu, Linux Mint and other Ubuntu-based distributions

First, make sure that Python 3 is installed on Ubuntu. To check that, use this command:

`python3 --version`


If it shows you a number like Python 3.x.y, Python 3 is installed on your Linux system.

![Checking the version of Python Installed on Ubuntu](https://itsfoss.com/content/images/2023/05/python3-version-in-Ubuntu.png)

Now you can install pip3 using the command below:

`sudo apt install python3-pip`


You should verify that pip3 has been installed correctly using this command:

`pip3 --version`


It should show you a number like this:

`pip 22.0.2 from /usr/lib/python3/dist-packages/pip (python 3.10)`


This means that pip3 is successfully installed on your system.

`pip`

command defaults to pip3 in Ubuntu 20.04 and above.## Installing Python packages [Recommended Way]

Recently, a change has been done on distributions like Ubuntu 23.04 and upcoming Debian 12, regarding the [installation of python packages](https://itsfoss.com/externally-managed-environment/).

From now on, you should install Python packages either from native repositories, install in a virtual environment or use `pipx`

.

[Externally Managed Environment Error With Pip in UbuntuSeeing an “externally managed environment” error while using Pip in Ubuntu 23.04? Here’s what you can do about this error.](https://itsfoss.com/externally-managed-environment/)![](https://itsfoss.com/content/images/2023/05/handling-externally-managed-env-pip.png)


![](https://itsfoss.com/content/images/2023/05/handling-externally-managed-env-pip.png)

This was implemented to avoid the conflict between OS package managers and Python-specific package management tools like pip. These conflicts include both Python-level API incompatibilities and conflicts over file ownership.

## Using pip commands

Now that you’ve installed pip, let’s quickly see some basic pip commands. These commands will help you use pip commands for searching, installing and removing Python packages.

### Install a package with pip

There are two ways to install a package with PIP. You either install it for the currently logged-in user, or you install system-wide.

If you use `--user`

option, it installs the package for the logged-in user, i.e., you, without needing sudo access. The installed python software is available only for you. Other users on your system (if any) cannot use it.

`pip3 install --user python_package_name`


If you remove the `--user`

option, the package will be installed system-wide, and it will be available for all the users on your system. You’ll need sudo access in this case.

`sudo pip3 install python_package_name`


PIP doesn’t support tab completion by default. So you need to know the exact package name that you want to install. How do you get that? I show that to you in the next section.

### Search for packages in PyPI

To search for packages in the Python Package Index, you can go to their official package search website.

For example, if you search on ‘stress’, it will show all the packages that have the string ‘stress’ in their name or description.

![PyPI website package search results](https://itsfoss.com/content/images/2023/05/pypi-search.png)

Pip had provided a command line search option, which was disabled due to excessive web traffic issues. So, if you try to use `pip search package-name`

, you will come across an error, as shown in the screenshot below.

![Searching for PyPI packages using "pip search" command in terminal will result in an error, since that service has been disabled.](https://itsfoss.com/content/images/2023/05/pip-search-error.png)

`pip search`

errorSo, use the PyPI website instead, as mentioned above.

### Upgrade Packages installed via pip

To [upgrade packages installed via pip](https://itsfoss.com/upgrade-pip-packages/), use the command below:

`pip3 install --upgrade <package-name>`


### Remove packages installed via pip

If you want to remove a Python package installed via pip, you can use the remove option.

`pip3 uninstall <installed_package_name>`


## Uninstall Pip from Ubuntu

To remove pip from Ubuntu, open a terminal and run:

```
sudo apt remove python3-pip
sudo apt autoremove
```


## Pipx is better! Start using it instead of Pip

Actually, if you want to use Pip for installing Python-based GUI applications, you should use Pipx. It complies with the new Python guidelines.

[Using Pipx](https://itsfoss.com/install-pipx-ubuntu/) is similar to Pip so it should feel familiar.

[Install and Use pipx in Ubuntu & Other LinuxPipx addresses the shortcomings of the popular pip tool. Learn to install and use Pipx in Linux.](https://itsfoss.com/install-pipx-ubuntu/)![](https://itsfoss.com/content/images/2023/05/install-pipx-ubuntu.png)


![](https://itsfoss.com/content/images/2023/05/install-pipx-ubuntu.png)

I hope you like this tutorial on installing and using Pip on Ubuntu and hopefully on other distributions, too. Let me know if you have questions or suggestions.