How to Install Python and Pip on Rocky Linux 9
Introduction
Python is a high-level programming language for developing modern applications. Python has many use cases including data science and machine learning. Pip is a package manager for Python that allows you to install and manage Python packages that are not part of the standard Python library.
This article explains how to install Pip on Rocky Linux 9 for Python Package Management. You'll install multiple Python versions and create a virtual environment using the virtualenv
module on your server.
Prerequisites
Before you begin:
- Deploy a Rocky Linux 9 instance on Vultr.
- Access the instance using SSH as a non-root user with sudo privileges.
Install Python3 on Rocky Linux 9
By default, Python version is available on Rocky Linux 9. Follow the steps below to view the default version and install the latest Python package using the DNF package manager.
View the default Python version.
console$ python3 --version
Output:
Python 3.9.16
Search all Python packages available in the default
DNF
repositories.console$ sudo dnf search python | grep ^python | grep -i interpreter
Output:
Last metadata expiration check: 0:00:09 ago on Sat Aug 3 09:58:50 2024. python3.x86_64 : Python 3.9 interpreter python3.11.x86_64 : Version 3.11 of the Python interpreter python3.12.x86_64 : Version 3.12 of the Python interpreter
Install the latest Python version based on the above repository results. For example, run the following command to install Python
3.12
.console$ sudo dnf install python3.12
List all available Python versions.
console$ ls -l /usr/bin | grep python
Output:
python3 -> python3.9 python3.12 python3.9
Based on the above output, the
python3
command is a symbolic link to the defaultpython3.9
. Runningpython3 --version
displays this version as the default unless you change it using thealternatives
command.console$ python3 --version
Output:
Python 3.9.16
Verify the new Python version.
console$ python3.12 --version
Output:
Python 3.12.1
Install Pip on Rocky Linux 9
Pip allows you to install and manage Python packages. Follow the steps below to install a specific Pip version based on the Python version on your server.
Install Pip on Rocky Linux 9. For example, Pip for
python3.12
.console$ sudo dnf install -y python3.12-pip
View the Pip version.
console$ pip3.12 --version
Output:
pip 23.2.1 from /usr/lib/python3.12/site-packages/pip (python 3.12)
If you're using Ubuntu, check out our detailed guide on how to install Pip on Ubuntu for managing Python packages efficiently. Follow the steps to set up Pip and streamline your development workflow.
Test Python
Follow the steps below to access the Python shell and create a basic program to test your installation.
Open the Python shell.
console$ python3.12
Print a new message in the Python console.
python>>> print("Hello world! Greetings from Vultr")
Output:
Hello world! Greetings from Vultr
Exit the Python shell.
python>>> exit()
Install Multiple Python Versions
Python allows multiple versions to run on the same server and you can switch between them or choose a preferred default version using the alternatives
command. Multiple Python versions allow you to work on different projects requiring specific versions. Follow the steps below to install multiple Python versions and set the default version on your server.
Install another Python version, such as
3.11
.console$ sudo dnf install python3.11
Verify the available Python versions on the server.
console$ ls /usr/bin/ | grep python
Output:
python3 -> python3.9 python3.11 python3.12 python3.9
Use the
alternatives
command to enable multiple Python versions and set the default version. Replacepython3.12
with your preferred default version andpython3.11
with the other installed version.console$ sudo alternatives --install /usr/bin/python3 python3 /usr/bin/python3.12 1 $ sudo alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 2
List all Python versions you can switch between on the server.
console$ sudo alternatives --config python3
Output:
There are 2 programs which provide 'python3'. Selection Command ----------------------------------------------- 1 /usr/bin/python3.12 *+ 2 /usr/bin/python3.11 Enter to keep the current selection[+], or type selection number:
Choose the Python version you want to set as the default and type your choice by specifying a number. Then, press Enter to apply the changes.
Create a Virtual Environment
A virtual environment in Python allows you to work on multiple isolated projects that require different dependencies. Isolated environments remove dependency conflicts between projects. Follow the steps below to create a Python virtual environment.
Install the
virtualenv
module.console$ sudo pip install virtualenv
Create a sample virtual environment, such as
test
.console$ virtualenv test
Output:
created virtual environment CPython3.9.18.final.0-64 in 473ms creator CPython3Posix(dest=/root/test, clear=False, no_vcs_ignore=False, global=False) ...
Activate the virtual environment.
console$ source test/bin/activate
Verify that your terminal prompt changes to the new virtual environment.
console(test) linuxuser@server:~$
Deactivate the virtual environment with the following command.
console$ deactivate
Conclusion
You have installed Pip on Rocky Linux 9 and enabled multiple Python versions on your server. You also created a virtual environment to isolate your Python project and avoid conflicts when installing dependencies. For more information and configuration options, please visit the Python documentation.