pip
is the default package manager for Python, allowing you to install and manage additional libraries and dependencies not included in the standard Python library. By default, it installs packages from the Python Package Index (PyPI), giving you access to a vast repository of Python software.
While Ubuntu's default package manager, apt
, can install Python packages, it may not always have the most recent versions or a comprehensive selection. Using pip
ensures you have the latest packages and a broader range to choose from.
Installing Pip on Ubuntu
Step 1: Open your terminal. You can do this by pressing Ctrl + Alt + T
or by searching for "Terminal" in the applications menu.
Step 2: Update your package list to ensure all repositories are up to date:
sudo apt update
Step 3: Install pip
for Python 3 with the following command:
sudo apt install python3-pip
Note: If you need pip
for Python 2, use the command:
sudo apt install python-pip
For Ubuntu versions older than 14.04, replace apt
with apt-get
in the commands above.
Step 4: Verify the installation by checking the pip
version:
pip3 --version
This command should display the version of pip
installed on your system.
Ubuntu's repositories might not always have the latest version of pip
. To upgrade pip
to the newest version, execute the following commands:
# For Python 3
sudo pip3 install --upgrade pip
# For Python 2
sudo pip install --upgrade pip
📝 Important Note on pip
and pip3
:
The command pip
is associated with Python 2 packages, while pip3
is used for Python 3 packages. In the examples below, we'll use pip3
. If you're working with Python 2, substitute pip3
with pip
.
Managing Python Packages with Pip
Step 1: To list all Python packages installed on your system, run:
pip3 list
This will display all installed packages along with their versions.
Step 2: To view detailed information about a specific package, use the command:
pip3 show package_name
Replace package_name
with the name of the package you're interested in.
Step 3: To search for packages in the Python Package Index using a keyword, run:
pip3 search <keyword>
Replace <keyword>
with the term you're searching for. This will list all packages that match the keyword.
For example, searching for "blinker" would produce results like this:
Installing and Upgrading Packages with Pip
Step 1: To install a new package, use the command:
pip3 install package_name
Replace package_name
with the name of the package you want to install.
Step 2: To upgrade an existing package to its latest version, run:
pip3 install --upgrade package_name
This command will also update any dependencies associated with the package. Make sure to replace package_name
with the actual name of the package you wish to upgrade.
Uninstalling Packages with Pip
Step 1: To remove a package from your system, execute the command:
pip3 uninstall package_name
Again, replace package_name
with the name of the package you wish to uninstall.
Step 2: If you want to uninstall a package without the confirmation prompt, add the -y
flag:
pip3 uninstall -y package_name
By mastering these pip
commands, you can efficiently manage Python packages on your Ubuntu system, ensuring you have the tools you need for your projects.
Member discussion