Python

Effectively Use Alternatives command for Python

Share on Social Media

In this Linux tutorial, you will learn how to use Alternatives command to set Python Versions in Linux. #centlinux #linux #python

Problem Statement:

We have two versions of Python Programming Language are already installed on our Rocky Linux 9 server.

Python 3.9 is by default installed on all Red Hat Enterprise Linux 9 operating systems including Minimal installations.

Install Python on Linux:

We have installed Python 3.10 on this machine, and can also install the same by following our previous article: How to install Python 3.10 on Rocky Linux 9

Verify the existance of Python versions by executing following commands.

# python3.9 -V
Python 3.9.14

# python3.10 -V
Python 3.10.4

Alternatives Command Usage:

Linux operating system provides alternatives command to set an active link to a specific command, among all the installed versions of that command.

Use the following command to set an active version of Python.

# alternatives --config python

If the above command returns no output, check whether python is available in commands list known to alternatives.

# alternatives --list
libnssckbi.so.x86_64    auto    /usr/lib64/pkcs11/p11-kit-trust.so
soelim                  auto    /usr/bin/soelim.groff
iptables                auto    /usr/sbin/iptables-nft
ebtables                auto    /usr/sbin/ebtables-nft
arptables               auto    /usr/sbin/arptables-nft
cifs-idmap-plugin       auto    /usr/lib64/cifs-utils/cifs_idmap_sss.so
man                     auto    /usr/bin/man.man-db
ld                      auto    /usr/bin/ld.bfd

You can see that, there is no python command in list. Therefore, you have to add Python in alternatives info base.

Check the location of Python executable files.

# whereis python3.9
python3.9: /usr/bin/python3.9 /usr/lib/python3.9 /usr/lib64/python3.9 /usr/include/python3.9 /usr/share/man/man1/python3.9.1.gz

# whereis python3.10
python3.10: /usr/local/bin/python3.10 /usr/local/lib/python3.10

Note down the location of Python executables from above output and then execute following commands to define Python alternatives.

# alternatives --install /usr/bin/python python /usr/bin/python3.9 1
# alternatives --install /usr/bin/python python /usr/local/bin/python3.10 2

You can now execute following command to set an active version of Python command.

# alternatives --config python

There are 2 programs which provide 'python'.

  Selection    Command
-----------------------------------------------
   1           /usr/bin/python3.9
*+ 2           /usr/local/bin/python3.10

Enter to keep the current selection[+], or type selection number:

Currently, Python3.10 is set as active Python command.

You can verify it by executing following command at Linux bash prompt.

# python -V
Python 3.10.4

Now, change the active version of Python command to 3.9 as follows.

# alternatives --config python

There are 2 programs which provide 'python'.

  Selection    Command
-----------------------------------------------
   1           /usr/bin/python3.9
*+ 2           /usr/local/bin/python3.10

Enter to keep the current selection[+], or type selection number: 1

Check the version of Python command again.

# python -V
Python 3.9.14

Video of Alternatives Command Example:

Conclusion:

In this Linux tutorial, you will learn how to use Alternatives command to set Python Versions in Linux.

Alaric Bird

Alaric Bird is a seasoned Linux System Administrator with over a decade of experience in managing and optimizing Linux-based servers and infrastructure. Known for his expertise in server deployment, security hardening, and performance tuning, Alaric has a deep understanding of various Linux distributions, including Ubuntu, CentOS, and Red Hat Enterprise Linux. His skills extend to cloud platforms like AWS, where he effectively manages virtual private servers and services. Alaric is also proficient in scripting languages such as Bash and Python, which he uses to automate routine tasks, enhancing efficiency and reliability. With a strong commitment to continuous learning, he stays updated with the latest developments in open-source technologies and best practices. His problem-solving abilities, combined with excellent communication skills, make him a valuable asset to any IT team. In addition to his technical expertise, Alaric is passionate about mentoring junior administrators and fostering a collaborative environment.

Share
Published by
Alaric Bird

Recent Posts

Change Apache Document Root in Linux

Learn how to change Apache document root in Linux by following this step-by-step guide. Adjust…

2 weeks ago

How to Change Apache Port in Linux

Discover how to change Apache port in Linux easily. Follow our simple guide to modify…

2 weeks ago

How to Create Virtual Host in Apache Server

Learn how to create a virtual host in Apache Server with this comprehensive guide. Set…

3 weeks ago

10 Practical Tasks for RHCSA Exam with Solutions

Discover 10 practical tasks for the RHCSA exam with step-by-step solutions. Boost your Linux skills…

3 weeks ago

Ultimate Fail2ban Configuration Guide

Discover the ultimate Fail2ban configuration guide. Learn how to set up, customize, and optimize Fail2ban…

3 weeks ago

VPS Server: The Ultimate Guide to Virtual Private Servers

Explore the ultimate guide to VPS servers and learn everything about Virtual Private Servers, including…

4 weeks ago

This website uses cookies.