Python has become a versatile programming language nowadays. Also python is widely used in machine learning language.
In this article we will how to Install Python on Ubuntu 16.04 server.
Python was initially developed in 1991 and named after the Comedy group Monty Python.
The creators of python Made it very easy to code.
Even newbie can understand and start coding within short time.
This has got very grace towards the Python language for all developers.
Since machine learning and many other web application purposes, the python is used at the back end.
Especially many programmers use ubuntu operating system for their programming.
This made us give a guide for people to install python on ubuntu 16.04 server.
Let us the requirement for python installation on ubuntu 16.04 server.
- Create sudo non root user and initial server setup as mentioned in this guide.
Get 50$ Credit on Vulture Clouds Here and Test the Instruction for free.
That’s all. Now, let us look into the procedure.
- First, we will install python on ubuntu
- We will create virtual environment for python
- Then, we will create a python program for testing purpose.
1. Install Python Ubuntu 16.04
Ubuntu 16.04 has come with Python 2 and python 3 in its package repository. So, it is easy to install them with
basic command lines.
Before that, we will once again ensure that our system package is up to date.
$ sudo apt get update
Now, check whether the system has python 3 installed with the version check command.
$ python3 -V
You should get these following output.
Output Python 3.5.2
We need to manage the python software package, for that we will install pip using the below command.
$ sudo apt-get install -y python3-pip
When you work on a project, you will need many python packages to install on the go. For that, you can use pip to install the python packages using simple commands.
$ pip3 install your_package_name
Just replace the above command with your required package name.
For example, we can install the Numpy package using the below command.
$ sudo apt-get install build-essential libssl-dev libffi-dev python3-dev
The next step is to set up a virtual environment for python.
2. Setup Python Virtual Environment
Python Virtual environment is one essential thing for developers.
Because a developer can work on multiple projects on the same server.
Each of the projects requires different packages and dependencies to work on.
We can keep using many dependencies for all the projects. It will make the software creation more complex.
So, here we can set up a virtual environment for each of our projects.
Each of this virtual environment only contains the required dependencies for the project.
These virtual environment simple directories and act as an unique environment for each project due to the script inside those folders.
Those scripts only call and use those dependencies for those projects.
We need pyenev commands to create the virtual environment. For that, we have to install the venv module from the python 3 package library.
To install pyenev, use this below command.
$ sudo apt-get install -y python3-venv
After the installation, our next step is to create the directories for the Python programming environment. To do this, we will create environments directory using the mkdir command.
Use the below command to create the directories.
$ mkdir environments $ cd environments
We have changed the current location of our directory to create an environment inside the directory.
To create an environment, use this below command.
$ pyvenv my_env
The pyvenv will create a new directory. To view that, use ls command.
$ ls my_env
You should get the following output.
bin include lib lib64 pyvenv.cfg share
These files are required to keep the project files and dependencies from the server local environment.
This way, we can avoid conflicts and achieve version control over the projects.
Also, all of the projects will have corresponding dependencies to work on.
Ubuntu 16.04 has a share directory for Python Wheels.
The python wheels reduce the number of time software has to be compiled for the execution.
This way the production can be increased and resource utilization is achieved.
To enable use the python wheel in a shared library, use this below command.
$ source my_env/bin/activate
Now, you will be switched to the my_env and you can see the prefix of your environment name in your prompt.
the prompt will look like this.
Now, whatever the program you develop under this environment will only use packages, settings, and dependencies inside this environment.
3. Create a Test Program for Python
To make sure that the environment is working, we will create a small program.
Use the below command to create a new file to write a program in it.
(my_env) sammy@ubuntu:~/environments$ nano hello.py
Add the below line in the file and save.
This program will give output Hello World when executing it.
Press Ctrl+X and press Y to save and close.
To run the program, use the below command.
(my_env) sammy@ubuntu:~/environments$ python hello.py
You should get the following output now.
Output Hello, World!
Once, you finish the task, you can return to your home environment by typing deactivate.
If you have errors, questions, commands, then leave them in the commands.
Share this article and make sure to subscribe to the tutorial newsletter.