How to Install Python and Pip on Ubuntu 24.04
Introduction
Python is a modern general-purpose programming language suitable for various development and scripting tasks. Pip is a package installer for Python that allows you to install and manage additional libraries that are not part of the standard Python library on a server.
This article explains how to install Python and Pip on an Ubuntu 24.04 server and manage application processes on your server. You will install the latest Python version using a PPA, manage multiple versions and use the Python virtual environment package virtualenv
to create isolated development environments.
Prerequisites
Before you begin:
Deploy an Ubuntu 24.04 server instance on Vultr.
Access the server using SSH as a non-root user with sudo privileges.
Install Python
Python is available in the default APT repositories on Ubuntu 24.04 but may not be the latest version. Personal Package Archives (PPAs) enable the installation of custom application packages on a server, and you can use the deadsnakes
PPA to install the latest or specific Python version on your server. Follow the steps below to install the latest Python 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 python3
Run the following command to install a specific version such as
3.10
.console$ sudo apt install python3.10
View the installed Python version on your server.
console$ python3 --version
Output:
Python 3.12.3
Install Pip
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 24.0 from /usr/lib/python3/dist-packages/pip (python 3.12)
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 you can install and use on your server using the update-alternatives
utilities. This enables you to work with multiple project environments that match specific Python requirements on your server. Follow the steps below to install multiple Python versions for use on your server.
Install another version of Python such as
Python3.9
.console$ sudo apt install python3.9
Run the following
update-alternatives
commands to enable multiple versions in your Python binary/usr/bin/
location. Replacepython3.12
with your active Python version andpython3.9
with the newly installed version.console$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.12 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.12 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 to activate in your server environment and press Enter to apply changes. When working with virtual environments, the selected version is only activated in your environment.
Create Virtual Environments
The Python virtual environment module virtualenv
creates virtual environments that enable the management of dependencies for different Python projects to limit package conflicts on your server. Follow the steps below to create a Python virtual environment to use 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.10.x in 261ms creator CPython3Posix(dest=/home/user/project/venv, 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
You have installed Python and Pip on your Ubuntu 24.04 Vultr server. Then, you enabled multiple versions and set up virtual environments to use Python with different projects on your server. For more information and usage options, please visit the Python documentation.