How to install Python 3 and set up a programming environment on Ubuntu Server

0 Shares
0
0
0
0

Introduction

The Python programming language is increasingly popular among beginners and professional developers. With its flexibility and versatility, Python is very strong in the areas of scripting, automation, data analysis, machine learning, and back-end development. The language was first released in 1991 and its name is inspired by the British comedy troupe Monty Python. The development team aimed to make Python a programming language that was fun to use.

This tutorial will help you set up a Python 3 development environment on your Ubuntu 22.04 server. Programming on a server has many advantages and supports collaboration on development projects. The general principles of this tutorial can be applied to any Debian Linux distribution.

Prerequisites

To follow this tutorial, you will need a non-root user with sudo privileges on an Ubuntu 22.04 server.

With the server and user set up, you're ready to get started.

Step 1 — Setting up Python 3

Ubuntu 22.04 and other Debian Linux versions come with Python 3 installed by default. To ensure that versions are up to date, update your local package list:

sudo apt update

Then update the packages installed on the system to use the latest versions:

sudo apt -y upgrade

The -y flag confirms that you agree to install everything, but depending on your Linux distribution, you may need to confirm additional prompts when updating and upgrading the system.

Once the process is complete, check the version of Python 3 installed on the system by running the following command:

python3 -V

The output in the terminal window will show you the version number. While this number may vary, the output will be similar to the following:

Python 3.10.4

To manage Python software packages, let's install pip. Pip is a tool that installs and manages programming packages that we may need in our development projects. To learn more about the modules or packages that you can install with pip, see the article How to import modules in Python 3 Read.

sudo apt install -y python3-pip

Python packages can be installed with the following command:

pip3 install package_name

Here package_name It can refer to any Python package or library, such as Django for web development or NumPy for scientific computing. So if you want to install NumPy, you can do so with the following command:

pip3 install numpy

Several other packages and development tools should also be installed to ensure you have a robust development environment:

sudo apt install -y build-essential libssl-dev libffi-dev python3-dev

After setting up Python and installing pip and other tools, you can set up a virtual environment for your development projects.

Step 2 — Setting up the virtual environment

Virtual environments allow you to have an isolated space on your server for Python projects so that each project can have a set of dependencies that won't interfere with other projects.

Setting up a development environment gives you more control over your Python projects and how you manage different versions of packages. This is especially important when working with third-party packages.

You can set up as many Python programming environments as you want. Each environment is essentially a directory or folder on your server that contains several scripts to make it an environment.

While there are various ways to create a programming environment in Python, here we will use the venv module, which is part of the Python 3 standard library. To install venv, run the following command:

sudo apt install -y python3-venv

With this tool installed, you are ready to create virtual environments. First, you need to choose which directory to place your development environments in, or create a new directory using the mkdir command:

mkdir environments

Then go to the directory where you will place your development environments:

cd environments

Once you have navigated to the desired directory, you can create a virtual environment:

python3 -m venv my_env

In fact, pyvenv creates a new directory with several items in it, which you can view with the ls command:

ls my_env

The output may be as follows:

bin include lib lib64 pyvenv.cfg

These files together ensure that your projects are isolated from the rest of the system, preventing system and project files from mixing. This is the best way to control versions and also ensures that each of your projects has access to its own packages.

To use this environment, you need to enable it. Do this by running the following command, which calls the activation script:

source my_env/bin/activate

Your command prompt will now be previewed with the name of your environment. In this example, the environment name is my_env. Depending on your Debian Linux version, the preview may be slightly different, but your environment name in parentheses is the first thing you'll see on the command line:

This preview informs us that the my_env environment is now active, meaning that when we create applications in this environment, they will only use the settings and packages from that environment.

Note: Inside the virtual environment you can use the python command instead of python3 and pip instead of pip3 if you prefer. If you are using Python 3 outside the virtual environment, you should use the python3 and pip3 commands exclusively.

After following these steps, your virtual environment is ready to use.

Step 3 — Create a “Hello, World” program”

Now that we have our virtual environment set up, let's create a traditional "Hello, World!" program. This program will allow us to test our environment and provide an opportunity to become more familiar with Python.

To do this, open a terminal text editor like nano and create a new file:

nano hello.py

After the file opens, write the program as follows:

print("Hello, World!")

Save the file and exit nano. To do this, press CTRL + X, then Y, then ENTER.

After exiting the editor and returning to the shell, you can run the program:

python hello.py

The hello.py program you created should produce the following output in the terminal:

Hello, World!

To exit the virtual environment, type the deactivate command and you will be returned to your home directory.

Result

Now you have a Python 3 development environment set up on your Ubuntu Linux server and can start your coding projects!

Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like