The flexibility and versatility of Python language than can be leveraged for inso many cases. Scripting, data analysis, backend development, machine learning, automation, are just some cases where one can use the language. With its easy set up process and compatibility and gives it a upper hand over other languages. Its also a very beginner friendly language since its code syntax is so straight forward making it gradual and smooth to learn.
In this guide, we’ll install Python 3 and set up a development environment on an Ubuntu 18.04 server.
Programming on a server has many advantages and supports collaboration across development project.
*The general principles of this tutorial will apply to any distribution of Debian Linux.
A server or virtual machine runnig Ubuntu 18.04 Server
Ubuntu 18.04 and other versions of Debian Linux ship with both Python 3 and Python 2 pre-installed. To make sure that our versions are up-to-date, let’s update and upgrade the system with the
apt command to work with Ubuntu’s Advanced Packaging Tool:
By default python 3 comes installed in Ubuntu 18.04
Run the command below to see installed version
To manage software packages for Python, install pip, a tool that will install and manage programming packages we may want to use in our development projects.
sudo apt install -y python3-pip
After the installation is complete, you can download packages by running
pip3 install package_name
package_name can refer to any Python package or library, such as Django for web development or NumPy for scientific computing. So if you would like to install Scipy, you can do so with the command
pip3 install scipy.
To ensure you have a robust setup for your development environment, install a few more packages with this command.
sudo apt install build-essential libssl-dev libffi-dev python3-dev
Once Python is set up, and pip and other tools are installed, you can set up a virtual environment for your development projects.
Virtual environments enable you to have an isolated space on your server for Python projects, ensuring that each of your projects can have its own set of dependencies that won’t disrupt any of your other projects.
Setting up a programming environment provides you with greater control over your Python projects and over how different versions of packages are handled. 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 basically a directory or folder on your server that has a few scripts in it to make it act as an environment.
While there are a few ways to achieve a programming environment in Python, we’ll be using the venv module here, which is part of the standard Python 3 library. Install venv by typing:
sudo apt install -y python3-venvUpon completion, you are now ready to create a virtual environment.
To create the virtual environment, run:
Once you are in the directory where you would like the environments to live, you can create an environment by running the following command:
python3.6 -m venv sample
pyvenv sets up a new directory that contains a few items which we can view with the
bin include lib lib64 pyvenv.cfg share
Together, these files work to make sure that your projects are isolated from the broader context of your local machine, so that system files and project files don’t mix. This is good practice for version control and to ensure that each of your projects has access to the particular packages that it needs. Python Wheels, a built-package format for Python that can speed up your software production by reducing the number of times you need to compile, will be in the Ubuntu 18.04
To use this environment, you need to activate it, which you can achieve by typing the following command that calls the activate script:
Your command prompt will now be prefixed with the name of your environment, in this case it is called my_env. Depending on what version of Debian Linux you are running, your prefix may appear somewhat differently, but the name of your environment in parentheses should be the first thing you see on your line:
(sample) [email protected]:~/environments$
Now that we have our virtual environment set up, let’s create a traditional “Hello, World!” program. This will let us test our environment and provides us with the opportunity to become more familiar with Python if we aren’t already.
To do this, we’ll open up a command-line text editor such as nano and create a new file:
Once the text file opens up in the terminal window we’ll type out our program:
Exit nano by typing the
X keys, and when prompted to save the file press
Once you exit out of nano and return to your shell, let’s run the program:
hello.py program that you just created should cause your terminal to produce the following output:
To leave the environment, simply type the command
deactivate and you will return to your original directory.