Installing a specific version of Python on Linux can be required for various reasons like compatibility with a particular application or getting new features.
In this article, we will discuss how to go through the process of installing a specific Python version on Linux using different methods and managing multiple Python versions on our system.
[lwptoc]
Prerequisites
Before proceeding, ensure that you have root or sudo access to your Linux system. Additionally, it is recommended to update your system to the latest packages by running the following commands:
sudo apt-get update && sudo apt-get upgrade # For Debian-based systems
sudo yum update -y # For RHEL-based systems
sudo dnf upgrade --refresh # For Fedora-based systems
Methods to Install Python on Linux
There are two primary methods for installing Python on Linux: using package managers and installing from the source.
Using Package Managers
We will discuss how to install Python using package managers:
APT – Advanced Package Tool
Debian and Ubuntu users can use the Advanced Package Tool (APT) to install a specific Python version. For example, to install Python 3.9, run:
sudo apt-get update
sudo apt-get install python3.9
YUM – Yellowdog Updater Modified
Red Hat Enterprise Linux (RHEL) and CentOS users can use the Yellowdog Updater Modified (YUM) package manager. To install Python 3.9, enable the EPEL repository and run:
sudo yum install epel-release
sudo yum install python39
DNF – Dandified YUM
Fedora users can use the Dandified YUM (DNF) package manager to install Python. To install Python 3.9, run:
sudo dnf install python3.9
Installing Python from Source
To install a specific Python version from the source, follow these steps:
- Download the desired Python version from the official Python website: https://www.python.org/downloads/source/
- Extract the downloaded archive:
tar xvf Python-X.Y.Z.tar.xz
Replace X.Y.Z
with the desired Python version.
- Change to the extracted directory and configure the build:
cd Python-X.Y.Z
./configure
- Compile and install Python:
make
sudo make install
This process will install the specified Python version on your system.
Verifying Python Installation
To verify that the specific Python version has been installed correctly, run the following command:
pythonX.Y --version
Replace X.Y
with the desired Python version. The output should display the installed Python version.
Setting up a Virtual Environment
Working with virtual environments allows you to manage different Python versions and dependencies for individual projects without affecting the system-wide Python installation.
Install Virtualenv
To set up a virtual environment, first, install the virtualenv
package:
pip install virtualenv
Create and Activate a Virtual Environment
- Navigate to your project directory and create a virtual environment with the specific Python version:
cd my_project
virtualenv -p pythonX.Y venv
Replace X.Y
with the desired Python version.
- Activate the virtual environment:
source venv/bin/activate
Your command prompt should now indicate that you are in the virtual environment. You can now install packages and manage dependencies specific to your project.
Managing Multiple Python Versions
By using the following method you can manage multiple versions of Python in Linux:
Using Pyenv
Pyenv is a useful tool for managing multiple Python versions on your system. To install Pyenv, follow these steps:
- Install the required dependencies:
sudo apt-get install -y build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python-openssl git
- Install Pyenv using the Pyenv installer script:
curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash
- Add the following lines to your
.bashrc
or.zshrc
file:
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
- Restart your terminal or run
source ~/.bashrc
orsource ~/.zshrc
.
- Install a specific Python version using Pyenv:
pyenv install X.Y.Z
Replace X.Y.Z
with the desired Python version.
- Set the global Python version:
pyenv global X.Y.Z
Replace X.Y.Z
with the desired Python version.
Conclusion
In this article, we have explored different methods to install a specific Python version on Linux, set up a virtual environment, and manage multiple Python versions using Pyenv. By following these steps, you can ensure that your Python projects run smoothly with the required Python version and dependencies.
Leave a Reply