How to Run a Python Script in Linux VPS?

admin-img By Manvinder Singh

Python is a famous tool for web scraping due to its user-friendliness and extensive selection of libraries. It provides a variety of advantages over other scripting languages, including early access to HTML source code, the ability to easily navigate complex websites, and help to write functions for automation. Scalability and performance when working with HTML documents. In this blog, we will explore how you can run a Python script in Linux VPS.

Why Run a Python Script on Linux VPS?

Running Python script on Linux VPS provides various benefits:

1. Pre-Installed Python Environment:

To run Python Script in Linux, most of the Linux distributor comes with python pre-installed, making it easier to start coding without additional set up.

2. Scripting and Automation

Linux is mostly used for scripting and automation. Python integrates well shell commands and is widely utilized for automating administrative tasks.

3. Performance and Stability

Linux systems are known for their stability and efficiency, making them ideal for long-running python scripts like servers, batch processing or data analysis tasks.

4. Developer-Friendly Environment

Linux supports a wide range of development tools, package managers and version control systems, all of which are python-compatible, Popular tools like pip, virtual view, and Jupyternotebook run seamlessly on Linux.

5. Better Control Over system

Linux gives direct access to system resources and networking which is helpful when developing network applications, deploying servers or working with low-level system interactions.

How to Run Python Script in Linux VPS?

Step 1: Updating your Linux Server

To run python on Linux, First, you have to update your Linux Server using the below commands. you should consider that different versions of linux require different commands for updating the Linux server. Moreover, if you are logged in with a root account, you don’t need to utilize Sudo for running commands, as you have all the vital access to execute the different commands.
  • Ubuntu & Debian
  • #sudo apt update
    Copied!
  • CentOS/Almalinux&Rocky linux
  • #sudo yum install python3
    Copied!

    Step 2: Install Python 3.x

    To run Python program in Linux, you can type the following commands to install Python on different distributions of Linux VPS.
  • Ubuntu & Debian Copy
  • #sudo apt install python3
    Copied!
  • CentOS/Almalinux&Rocky linux
  • #sudo yum install python3
    Copied!

    Step 3: Confirm Python installation

    Now that you have installed Python on your VPS Server, you should make sure that it is correctly installed by running the following command on the terminal: Python3 If you are Virtual Private Server supervisor, a software developer or a user, you may need to install pip on your system. PiP allows you to install various software packages written with Python programming Languas as a robust package management system. Let’s understand what PiP and how you can install PiP on VPS.

    Understanding PiP

    As most installations of pythons are preinstalled, you may sometimes require to manually install pip on your machine. You can install pip on all top operating systems, including most linux distributions, we will explore on how you can install and test PiP on Windows 10, Centos and Ubuntu. Therefore, if you are utilizing Linux, you should first login through SSH.

    Installing pip on CentOS

    Step 1: Before installing pip, you must add the EPEL Repo. Step 2: Then, you should update your machine using the following command:
    yum -y update
    Copied!
    Step 3: Now you can install pip via the below command:
    yum -y install python-pip
    Copied!
    Step 4: We can type some simple commands to test whether the pip installation is successful or not. getting a list of commands available in pip:
    pip --help
    Copied!
    Checking the version of pip:
    pip -v
    Copied!
    Installing pip on Ubuntu

    You will have to use the apt package manager to install pip on Ubuntu.

    Step 1: As mentioned in previous sections, First, you need to update your Ubuntu machine:
    sudo apt update
    Copied!
    Step 2: now install pip on your machine using the following command:
    sudo apt install python-pip
    Copied!
    Step 3: Finally, you can add this command to ensure that pip is installed appropriately.
    pip –version
    Copied!

    Summing Up

    Running Python Linux is straightforward and efficient. Start by ensuring python is installed utilizing a simple command like ‘python3---version Once confirmed navigate to the directory containing your script utilizing the cd command. Therefore, utilizing linux for Python scripting is highly beneficial for automation, server-side development and smoother access to system resources, making it an ideal environment for both development and production.

    Frequently Asked Questions

    Q 1. How do I connect to my Linux VPS?

    Ans. You can connect to your Linux VPS using SSH. Open a terminal and run `ssh username@your_vps_ip`, replacing `username` with your VPS user and `your_vps_ip` with the VPS's IP address.

    Q 2. How do I check if Python is installed on my VPS?

    Ans. Run `python --version` or `python3 --version` in the terminal. If Python is installed, the command will return the version number.

    Q 3. How do I upload my Python script to the VPS?

    Ans. You can use SCP or SFTP. For SCP, use the command: `scp /path/to/local/script.py username@your_vps_ip:/path/to/remote/`.

    Q 4. How do I run my Python script?

    Ans. Navigate to the directory containing the script using `cd /path/to/your/script`, then run `python script.py` or `python3 script.py`, depending on your Python version.

    Q 5. What if I get a "permission denied" error?

    Ans. Ensure your script has execute permissions. You can set this using `chmod +x script.py`. If the error persists, you may need to check the ownership of the file.

    Q 6. How can I run my script in the background?

    Ans. Use the `nohup` command followed by your script: `nohup python script.py &`. This allows the script to continue running even after you log out of the SSH session.

    autor-img

    By Manvinder Singh

    Manvinder Singh is the Founder and CEO of HostingSeekers, an award-winning go-to-directory for all things hosting. Our team conducts extensive research to filter the top solution providers, enabling visitors to effortlessly pick the one that perfectly suits their needs. We are one of the fastest growing web directories, with 500+ global companies currently listed on our platform.