
Python is a versatile programming language used for a variety of development and scripting tasks. Pip, a package installer for Python, helps manage libraries not included in the standard Python library.
This article covers how to install Python and Pip on Ubuntu 22.04. You'll learn to install the latest Python version using a PPA, manage multiple Python versions, and use virtualenv
to create isolated environments for your development projects.
Prerequisites
Before you begin:
Have an Ubuntu 22.04 server.
Access the server using SSH as a non-root user with sudo privileges.
Install Python on Ubuntu 22.04
Python is available in the default APT repositories on Ubuntu 22.04 but may not be the latest version. To install the latest or specific Python version, you can use the deadsnakes
PPA. This allows you to install custom Python versions on your server. Follow the steps below to install the latest version using the PPA.
Add the
deadsnakes
PPA to your server sources.console$ sudo add-apt-repository ppa:deadsnakes/ppa
Update the server package index.
console$ sudo apt update
Install the latest Python version on your server.
console$ sudo apt install -y python3
Run the following command to install a specific version such as
3.8
.console$ sudo apt install -y python3.8
View the installed Python version on your server.
console$ python3 --version
Output:
Python 3.10.12
Install Pip on Ubuntu 22.04
Run the following command to install Pip module.
console$ sudo apt install -y python3-pip
View the installed Pip version on your server.
console$ pip3 --version
Output:
pip 22.0.2 from /usr/lib/python3/dist-packages/pip (python 3.10)
Test Python
Access the Python Shell.
console$ python3
Enter the following code to test your Python installation.
python>>> print("Hello, Python!")
Output:
Hello, Python!
Exit the Python Shell.
python>>> exit()
Install Multiple Python Versions
Python supports multiple versions on your server, and you can manage them using the update-alternatives
utility. This allows you to switch between different Python versions for different project environments. Follow the steps below to install and manage multiple Python versions on your server.
Install another version of Python such as
Python3.9
.console$ sudo apt install -y python3.9
Run the following
update-alternatives
commands to enable multiple Python versions in the/usr/bin/
location. Replacepython3.10
with your active Python version andpython3.9
with the newly installed version.console$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 1 $ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2
View all available Python versions you can switch between on your server.
console$ sudo update-alternatives --config python3
Output:
Selection Path Priority Status ------------------------------------------------------------ * 0 /usr/bin/python3.9 2 auto mode 1 /usr/bin/python3.10 1 manual mode 2 /usr/bin/python3.9 2 manual mode Press <enter> to keep the current choice[*], or type selection number:
Enter your desired Python version (e.g., python3.9 or python3.10) to activate in your server environment and press Enter to apply the changes. When working with virtual environments, the selected version is only activated within that specific environment.
Create Virtual Environments
The Python virtualenv
module allows you to create isolated environments for managing dependencies in different Python projects, preventing package conflicts on your server. Follow the steps below to create and use a Python virtual environment on your server.
Install the Python
virtualenv
module.console$ sudo apt install -y python3-virtualenv
Create a new sample virtual environment such as
newenv
.console$ virtualenv newenv
Output:
created virtual environment CPython3.9.21.final.0-64 in 279ms creator CPython3Posix(dest=/home/linuxuser/newenv, clear=False, no_vcs_ignore=False, global=False) ...
Activate the virtual environment.
console$ source newenv/bin/activate
Verify that your terminal environment changes to the new Python virtual environment similar to the output below.
(newenv) linuxuser@server:~#
Run the following command to deactivate the virtual environment.
console$ deactivate
Conclusion
In this article, you installed Python and Pip on your Ubuntu 22.04 Vultr server, enabled multiple Python versions, and set up virtual environments for managing dependencies across different projects. For more information and usage options, please visit the Python documentation.
No comments yet.