How To Install Python 3 and Create Virtual Environment in Centos, Redhat and Amazon Linux
- By : Mydatahack
- Category : Infrastructure, Installation and Configuration
- Tags: installation, Linux, Python, Virtual Environment
The scope of this post is to install Python 3 for a user and create a virtual environment in Centos, Redhat or Amazone Linux. Linux comes with Python 2.7 and the best practice is not to mess with. This is because Linux OS has some dependency and upgrading it to 3 may cause problems. If you are developing with Python 3, I recommend to install it for a user. Knowing how to create virtual environments becomes handy for serverless microservice development, too.
If you want to do the same thing with Ubuntu, check out the post here
Let’s get started.
(1) Download Python Installation Package
You first need to get the installation link (https://www.python.org/downloads/release/python-364/). Today, python-3.6.4 is the latest version. Let’s create a directory called python3 in your home directory and download it there and unzip it.
cd python3
wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tar.xz
tar xf Python-3.*
cd Python-3*
(2) Install it with Pip
We first need to install Development Tools for Linux. Then, configure the installation path and include pip upgrade.
./configure --prefix=/home/ec2-user/pythonfin --with-ensurepip=upgrade
(3) Build
You first need to install openssl-devel otherwise you will get pip requires SSL/TSL error. Then you can build it.
make
(4) Install for the user
When you don’t want to overwrite the python executable (safer, at least on some distros yum needs python to be 2.x, such as for RHEL6), you can install python3.* as a concurrent instance to the system default with an altinstall as below
(5) Create Shortcut For Installed Python 3
This makes it easier for the creation of virtual environments.
export pip3=/home/ec2-user/pythonfin/bin/pip3.6
(6) Install virtualenv and Create Virtual Environment
You can use the environment variables created in the previous step. In the command below, you are creating it in the pythonfin folder where Python 3 is installed.
$python3 -m venv lambda (this creates environment in the pythonfin folder)
(7) Activate Environment
Once you activate the environment, any installation with pip will go there. The python command uses the python binary for the environment.
You are ready to go!
Bonus Section
To apply above knowledge, let’s bootstrap EC2 instance with Python 3 with Tensorflow and Keras installed at user level. You can save the script below as a text file and use it as user-data when you launch the instance.
To bootstrap EC2 from a text file with AWS CLI, you need to have #!/bin/bash at the start to trigger the script. Make sure to have -y for auto-installation. You do not need to add sudo as the script is executed by root.
yum update -y
yum install zlib-devel bzip2-devel sqlite sqlite-devel openssl-devel -y
yum groups install "Development Tools" -y
mkdir /home/ec2-user/python3
cd /home/ec2-user/python3
wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tar.xz
tar xf Python-3.*
cd /home/ec2-user/python3/Python-3.6.4
./configure --prefix=/home/ec2-user/python3 --with-ensurepip=upgrade
make
make altinstall
/home/ec2-user/python3/bin/pip3.6 install h5py tensorflow tflearn keras boto3