Create Python Virtual Environment on AWS EC2

Creating virtual environments when developing applications in Python is one of the most common requirements. While on my Windows laptop I used Anaconda to do the same, for development on my AWS EC2, I set this up using a few simple commands. Here I show how to set up a virtual environment on a linux EC2 instance on AWS.

Step1: ssh into EC2

The first step obviously is to create an EC2 instance on AWS with required image. Assuming we already have an EC2 instance up and running already, we log into it using ssh. The pem file obtained during the key exchange process should be given appropriate permissions for the ssh to work.

$ chmod 600 /Users/Nitya/ec2_instance1.pem
$ ssh -i /Users/Nitya/ec2_instance1.pem ubuntu@xxxxxxx.compute-1.amazonaws.com

Step2: Install Virtual Env

By default we will land in the /home/ubuntu directory. We create a sub-directory “venv” within the home directory. Once inside this sub-directory, we will install virtualenv. The default install command will install the virtualenv within /usr/bin/virtualenv. I have it already installed, so running this command again for me below shows it is already the newest version.

After running the install command, we can test the installation by running a command to get the version installed. If the installation went fine, we will get back the version.

$ pwd
/home/ubuntu
$ mkdir venv
$ cd venv/
venv$ sudo apt install virtualenv
Reading package lists... Done
Building dependency tree       
Reading state information... Done
virtualenv is already the newest version (15.1.0+ds-1.1).
0 upgraded, 0 newly installed, 0 to remove and 60 not upgraded.
venv$ /usr/bin/virtualenv --version 
15.1.0

Step3: Create a Virtual Environment

Once virtualenv is installed, we are ready to create our first virtual env. Since we are already within our sub-directory, the environment we will create will be within this.

Below I am creating an environment named test_env1. I already have python in this image and am using the python3 for my environment.

venv$ /usr/bin/virtualenv -p /usr/bin/python3 test_env1
Already using interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in /home/ubuntu/venv2/test_env1/bin/python3
Also creating executable in /home/ubuntu/venv2/test_env1/bin/python
Installing setuptools, pkg_resources, pip, wheel...done.

Step4: Source the Virtual Environment

Now that we have created our virtual environment, we need to source this environment to start using it. When we source an environment, we will start seeing the name of that environment within braces on our prompt. When working with multiple environments, this comes in really handy in ensuring we are in the correct environment.

venv$ ls
test_env1
venv$ source test_env1/bin/activate
(test_env1) ubuntu@ip-xxxxxxxx:~/venv$ 

Step5: Start using the Virtual Environment

We are not read to start using the environment we are in. Any installation we do from this point on will be within this environment only. In my case, I have created a file with all the libraries I want to install in this environment and running a pip install using this file.

Once the installation complete, we can run python and try importing a library we installed to test it out.

(test_env1) ubuntu@ip-xxxxxxxx:~/venv$ pip3 install -r /home/ubuntu/requirements.txt
(test_env1) ubuntu@ip-xxxxxxxx:~/venv$ python
Python 3.6.9 (default, Nov  7 2019, 10:44:02) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import keras
Using TensorFlow backend.
>>>  
(test_env1) ubuntu@ip-xxxxxxxx:~/venv$

Step6: Exit the Virtual Environment

To exit the virtual environment we are in, we can just run deactivate and we will be out of the environment. Once we are out, we will no longer see the environment on the command prompt, confirming we are not in the environment any more.

(test_env1) ubuntu@ip-xxxxxxxx:~/venv$ deactivate
ubuntu@ip-xxxxxxxx:~/venv$ 

Step7: Exit the EC2 Instance

To log out of the EC2 instance, simply run the command “exit”.

ubuntu@ip-xxxxxxxx:~/venv$ exit

Leave a Reply

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