virtualenv allows you to create and manager virtual environments for python and its packages. It allows you to create an isolated environment for same/different python codebase. You can create separate virtual environments for development and production, allowing you to run your application in virtual environment without breaking anything. In this article, we will look at how to install virtualenv in Ubuntu.
How to Install Virtualenv in Ubuntu
Here are the steps to install virtualenv in Ubuntu.
1. Install pip
Open terminal and run the following command to install pip.
sudo apt-get install python3-pip
Also read : How to Change SSH port in Ubuntu
2. Install virtualenv
Install virtualenv with the following command
sudo pip3 install virtualenv
Also read : How to Set up automatic updates in Ubuntu
3. Create new virtual environment
Navigate to your project folder
$ cd $YOUR_PROJECT_DIRECTORY
Run the following command to create new virtual environment( e.g staging)
$ virtualenv .staging
Also read : How to Run Cron Job Every 5, 10, 15 minutes
4. Activate Virtual environment
Activate virtual environment staging
$ source .staging/bin/activate
You will see the name of your virtual environment in the prompt, indicating that your virtual environment is active.
Once your virtual environment is activated, you can install additional packages using pip command
$ pip install <package_name>
These packages will be available only inside the virtual environment where they are installed.
To deactivate a virtual environment, simply run the deactivate command.