Developers know that you can't spell LAMP (Linux, Apache, MySQL, Perl/PHP/Python) on many systems without Python.
Although you are probably well aware that LAMP is the server stack powering today's internet, you might be wondering why you should pay attention to Python.
Let’s discuss the benefits of using Python and how to both install and configure it on your hosted server.
Go from idea to online in minutes with GoDaddy Airo™
Get started now.
What is Python?
Among all three aforementioned programming languages, Python is king. Why? Straightforward programs, code readability, and convenience (read: fewer lines of code).
Python simplifies a developer's job because, once written, this general-purpose language can run on any computer without requiring you to change the program.
Python is key to essential internet programs such as the GNU Mailman mailing-list manager; DNF, the Red Hat Enterprise Linux (RHEL) family's package manager; and the Zope content-management system (CMS) application server.
How to check whether Python is already installed
Chances are Python is already installed on your hosted Linux server. If you're unsure, follow these easy steps:
- Run the following from your SSH terminal:
$ ls -la /usr/bin/python
Note: Keep in mind /usr/bin/python is the default location for the Python executables.
- Use the following command to see which version of Python you're working with:
$ python -V
How to check which version of Python you are running
Checking which version of Python you're running depends on which Linux distribution you're using and the patches that have been applied to it.
Many hosted servers run distributions like Ubuntu 24.04 LTS, Debian 13, or CentOS Stream 10, which come with Python 3.x pre-installed.
However, it's best to double-check.
If you'd rather use a newer version of Python, such as Python 3.12.3 (as of August 2024), that version might already be installed.
Which version of Python to use
The vast majority of Python-based apps now run on Python 3.x. While Python 2 has reached its end of life, Python 3 continues to evolve, with the latest version being Python 3.12.3 (as of August 2024).
The Python community fully supports Python 3.x, and it's recommended to use the latest version for improved features and security.
If you prefer to use Python 3.x, refer to the step-by-step installation instructions below.
How to install Python 3
If you're ready, you can install Python 3 yourself by following these steps:
- Download the compressed version of the files to your server:
$ wget https://www.python.org/ftp/python/3.12.3/Python-3.12.3.tgz
- Decompress the files with the following command:
$ tar xvzf Python-3.12.3.tgz
Note: This will create a directory named Python-3.12.3 and decompress all of Python's files into it.
- Navigate into that directory with:
$ cd Python-3.12.3
- Configure the installation of your new version of Python.
$ ./configure --prefix=$HOME/.local
Note: This step sets up your configuration and prepares the necessary files.
- Compile the source code with:
$ make
- Install Python with:
$ make install
- After installation, ensure that pip is installed by running:
$ python3 -m ensurepip --upgrade
Add Python to your PATH
Next, you must add Python to your PATH environment variable. Use a pure text editor like vi, Emacs, or nano.
Do not use a word processor like Microsoft Word or LibreOffice Writer, as they add formatting codes that can interfere with configuration files.
- Open your Bash profile configuration file:
$ cd $HOME
$ vi .bash_profile
- Add the following lines, then save the file:
# Python 3
export PATH="$HOME/.local/bin:$PATH"
- Update your environment:
$ source ~/.bash_profile
- Verify the Python installation:
$ python3 -V
- You should see Python 3.12.3.
Congratulations, you now have Python 3 installed!
Why not just run “python?”
If you just run “python,” the executable will show the version number for the default Python for your version of Linux—not your newly installed version of Python.
You will now have two versions of Python on your server.
The best way to handle this is to use Python's virtual environment module to create a virtual Python environment. Inside this environment, you can use your newer version of Python—such as Python 3.12.3—without interfering with the preexisting Python.
How to create a virtual Python environment
The easiest way to create a virtual Python environment is to use the built-in venv module.
- Create a directory for your new environment:
$ mkdir PythonTest
- Enter the new directory:
$ cd PythonTest
- Create a virtual environment named `env` using the venv module:
$ python3 -m venv env
- Activate the new virtual environment with:
$ source env/bin/activate
Your shell prompt should now include `env`, indicating that the virtual environment is active.
You're now ready to start programming with Python.
How to install packages in your virtual environment
With your virtual environment activated, you can install packages specific to your project. To install new libraries, simply use pip, which is included in your virtual environment.
For example, to install the `numpy` library, run:
$ pip install numpy
This command installs the `numpy` library in your virtual environment without affecting your system-wide Python installations.
You can list all installed packages in your virtual environment with:
$ pip list
To generate a requirements file listing all your project's dependencies, run:
$ pip freeze > requirements.txt
This `requirements.txt` file can be shared to replicate your development environment easily.
How to deactivate a virtual Python environment
To deactivate your virtual environment, simply type `deactivate` in your terminal:
$ deactivate
This will exit the virtual environment and return you to your system's default Python interpreter.
Next steps
For more on the basics of using virtual environments, see Python Guide's Virtual Environments.
If you're new to Python, refer to the Beginner's Guide to Python.